<commit_before>'use strict';

/**
 * Created by aaronrussell on 9/29/16.
 */

/**
 * String.prototype extensions
 */
require('../datatype-ext')

const fs = require('fs'),
  http = require('http'),
  soap = require('soap'),
  chalk = require('chalk'),
  loopback = require('loopback'),
  builder = require('xmlbuilder'),
  uuid = require('node-uuid'),
  to_json = require('xmljson').to_json,
  schemas = require('./schema/index');

let app = require('../../../../server/server');

const server = http.createServer(function (request, response) {
  response.end('404: Not Found: ' + request.url);
}).listen(2188);


/** **/

class QBWebService {

  constructor(settings) {
    const self = this;
    this.settings = settings || {enableServiceLog: true};
    QBWebService.serviceLog('STARTED: QuickBooks Web Service server initialization.');
    this.queuedTransactions = new Map();
    this.errorCount = 0;
    this.syncedData = {};
    this.sentTransactionsCount = 0;

    QBWebService.serviceLog('STARTED: Building WSDL file.');
    const wsdl = fs.readFileSync(__dirname + '/qbws.wsdl', 'utf8').replace(/<soap:address\ +location=".*"\ +\/>/,
      '<soap:address location="' + 'http://localhost:2188/wsdl?wsdl' + '" />');
    QBWebService.serviceLog('COMPLETED: Building WSDL file.');

    QBWebService.serviceLog('STARTED: SOAP Server');
    this.soapServer = soap.listen(server, '/wsdl', {QBWebConnectorSvc: {QBWebConnectorSvcSoap: this}}, wsdl);
    QBWebService.serviceLog('COMPLETED: SOAP Server');

    QBWebService.serviceLog('STARTED: Creating log file');
    this.log = fs.readFile(__dirname + '/qbws-logs.log', 'utf8', function (err, file) {
      QBWebService.serviceLog('COMPLETED: Creating log file');
    });

    QBWebService.serviceLog('INITIALIZING: QuickBooks Data Models');
    self.reSync()
    QBWebService.serviceLog('COMPLETED: QuickBooks Web Service server initialization.');
  }

  /**
   * @function serviceLog
   *
   * @desc Writes a string to the console and log file.
   *
   * @param {String} data - The information to be logged.
   */
  static serviceLog(data) {
    //if (!self.settings.enableServiceLog) return;
    data = 'TIME:' + new Date().format('c') + ' - MSG: ' + data;
    console.log(data);

    fs.appendFile(__dirname + '/qbws-logs.log', data + '\n');
  }

  reSync() {
    const self = this || app.dataSources.quickbooksService.connector.webService;
    if (self.resyncingAll) return;
    self.resyncingAll = true;
    self.queueTransaction({modelName: 'Employee', method: 'GET'});
    self.queueTransaction({modelName: 'SalesOrder', method: 'GET'});
    self.queueTransaction({modelName: 'SalesTaxCode', method: 'GET'});
    self.queueTransaction({modelName: 'ShipMethod', method: 'GET'});
    self.queueTransaction({modelName: 'JobType', method: 'GET'});
    self.queueTransaction({modelName: 'Estimate', method: 'GET'});
    self.queueTransaction({modelName: 'Customer', method: 'GET'});
    self.queueTransaction({modelName: 'CustomerType', method: 'GET'});
    self.queueTransaction({modelName: 'PaymentMethod', method: 'GET'});
    self.queueTransaction({modelName: 'SalesRep', method: 'GET'});
    self.queueTransaction({modelName: 'ItemInventory', method: 'GET'});
    self.queueTransaction({modelName: 'UnitOfMeasureSet', method: 'GET'});
  }

  /**
   * @function queueTransaction
   *
   * @desc Queues the transaction to sync with QuickBooks Web Connector and wraps the transaction in a QBWSTransaction class
   *
   * @param transaction
   * @param.arg {String} method
   */
  queueTransaction(transaction) {
    console.log(transaction)
    const self = this || app.dataSources.quickbooksService.connector.webService;
    //before doing anything we first insure that the modelName used for the transaction exists
    let Model = app.models[transaction.modelName];
    //if loopback couldn't find the Model then we create it dynamically
    if (!self.syncedData[transaction.modelName]) self.syncedData[transaction.modelName] = {};
    if (!Model) {
      Model = app.loopback.createModel(transaction.modelName, {}, {base: 'PersistedModel'});
      app.model(Model, {dataSource: app.dataSources.db});
      //noinspection JSCheckFunctionSignatures
      Model.observe('before save', function (instance, next) {
        let data = instance.instance && instance.instance.__data || instance.data;
        let method = !!data.ListID ? 'UPDATE' : 'POST';
        if (data.syncToQB) {
          if (data.FirstName && data.LastName) {
            data.Name = data.FirstName + ' ' + data.MiddleName + ' ' + data.LastName;
            data.ShipToAddress = {
              Name: data.Name
            }<commit_msg>added remaining QuickBooks data queries<commit_after>'use strict';

/**
 * Created by aaronrussell on 9/29/16.
 */

/**
 * String.prototype extensions
 */
require('../datatype-ext')

const fs = require('fs'),
  http = require('http'),
  soap = require('soap'),
  chalk = require('chalk'),
  loopback = require('loopback'),
  builder = require('xmlbuilder'),
  uuid = require('node-uuid'),
  to_json = require('xmljson').to_json,
  schemas = require('./schema/index');

let app = require('../../../../server/server');

const server = http.createServer(function (request, response) {
  response.end('404: Not Found: ' + request.url);
}).listen(2188);


/** **/

class QBWebService {

  constructor(settings) {
    const self = this;
    this.settings = settings || {enableServiceLog: true};
    QBWebService.serviceLog('STARTED: QuickBooks Web Service server initialization.');
    this.queuedTransactions = new Map();
    this.errorCount = 0;
    this.syncedData = {};
    this.sentTransactionsCount = 0;

    QBWebService.serviceLog('STARTED: Building WSDL file.');
    const wsdl = fs.readFileSync(__dirname + '/qbws.wsdl', 'utf8').replace(/<soap:address\ +location=".*"\ +\/>/,
      '<soap:address location="' + 'http://localhost:2188/wsdl?wsdl' + '" />');
    QBWebService.serviceLog('COMPLETED: Building WSDL file.');

    QBWebService.serviceLog('STARTED: SOAP Server');
    this.soapServer = soap.listen(server, '/wsdl', {QBWebConnectorSvc: {QBWebConnectorSvcSoap: this}}, wsdl);
    QBWebService.serviceLog('COMPLETED: SOAP Server');

    QBWebService.serviceLog('STARTED: Creating log file');
    this.log = fs.readFile(__dirname + '/qbws-logs.log', 'utf8', function (err, file) {
      QBWebService.serviceLog('COMPLETED: Creating log file');
    });

    QBWebService.serviceLog('INITIALIZING: QuickBooks Data Models');
    self.reSync()
    QBWebService.serviceLog('COMPLETED: QuickBooks Web Service server initialization.');
  }

  /**
   * @function serviceLog
   *
   * @desc Writes a string to the console and log file.
   *
   * @param {String} data - The information to be logged.
   */
  static serviceLog(data) {
    //if (!self.settings.enableServiceLog) return;
    data = 'TIME:' + new Date().format('c') + ' - MSG: ' + data;
    console.log(data);

    fs.appendFile(__dirname + '/qbws-logs.log', data + '\n');
  }

  reSync() {
    const self = this || app.dataSources.quickbooksService.connector.webService;
    if (self.resyncingAll) return;
    self.resyncingAll = true;
    self.queueTransaction({modelName: 'Employee', method: 'GET'});
    self.queueTransaction({modelName: 'JobType', method: 'GET'});
    self.queueTransaction({modelName: 'Estimate', method: 'GET'});
    self.queueTransaction({modelName: 'Customer', method: 'GET'});
    self.queueTransaction({modelName: 'CustomerType', method: 'GET'});
    self.queueTransaction({modelName: 'CustomerMsg', method: 'GET'});
    self.queueTransaction({modelName: 'ToDo', method: 'GET'});
    self.queueTransaction({modelName: 'PaymentMethod', method: 'GET'});
    self.queueTransaction({modelName: 'SalesRep', method: 'GET'});
    self.queueTransaction({modelName: 'SalesReceipt', method: 'GET'});
    self.queueTransaction({modelName: 'SalesOrder', method: 'GET'});
    self.queueTransaction({modelName: 'SalesTaxCode', method: 'GET'});
    self.queueTransaction({modelName: 'ShipMethod', method: 'GET'});
    self.queueTransaction({modelName: 'StandardTerms', method: 'GET'});
    self.queueTransaction({modelName: 'Terms', method: 'GET'});
    self.queueTransaction({modelName: 'Item', method: 'GET'});
    self.queueTransaction({modelName: 'ItemInventory', method: 'GET'});
    self.queueTransaction({modelName: 'ItemNonInventory', method: 'GET'});
    self.queueTransaction({modelName: 'ItemService', method: 'GET'});
    self.queueTransaction({modelName: 'ItemSalesTax', method: 'GET'});
    self.queueTransaction({modelName: 'ItemDiscount', method: 'GET'});
    self.queueTransaction({modelName: 'Vendor', method: 'GET'});
    self.queueTransaction({modelName: 'VendorType', method: 'GET'});
    self.queueTransaction({modelName: 'UnitOfMeasureSet', method: 'GET'});
  }

  /**
   * @function queueTransaction
   *
   * @desc Queues the transaction to sync with QuickBooks Web Connector and wraps the transaction in a QBWSTransaction class
   *
   * @param transaction
   * @param.arg {String} method
   */
  queueTransaction(transaction) {
    console.log(transaction)
    const self = this || app.dataSources.quickbooksService.connector.webService;
    //before doing anything we first insure that the modelName used for the transaction exists
    let Model = app.models[transaction.modelName];
    //if loopback couldn't find the Model then we create it dynamically
    if (!self.syncedData[transaction.modelName]) self.syncedData[transaction.modelName] = {};
    if (!Model) {
      Model = app.loopback.createModel(transaction.modelName, {}, {base: 'PersistedModel'});
      app.model(Model, {dataSource: app.dataSources.db});
      //noinspection JSCheckFunctionSignatures
      Model.observe('before save', function (instance, next) {
        let data = instance.instance && instance.instance.__data || instance.data;
        let method = !!data.ListID ? 'UPDATE' : 'POST';
        if (data.syncToQB) {
          if (data.FirstName && data.LastName) {
            data.Name = data.FirstName + ' ' + data.MiddleName + ' ' + data.LastName;
            data.ShipToAddress = {
              Name: data.Name
            }
{'fraction_non_alphanumeric': 0.15785335168075487, 'fraction_numerical': 0.003931590328287792, 'mean_word_length': 3.941719281204468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5853255', 'n_tokens_mistral': 3154, 'n_tokens_neox': 3076, 'n_words': 684}