diff --git a/lib/server.js b/lib/server.js index c7d94c5..d1b0f6c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -47,16 +47,6 @@ var fs = require('fs'); */ var soap = require('soap'); -/** - * An HTTP server that will be used - * to listen for SOAP requests. - */ -var server = http.createServer(function(req, res) { - res.end('404: Not Found: ' + req.url); -}); - -var port = process.env.QB_SOAP_PORT || 8000; - /** * A constant for the WSDL filename. * @type {string} @@ -83,18 +73,43 @@ function buildWsdl() { module.exports = Server; -function Server() { - this.wsdl = buildWsdl(); - this.webService = require('./web-service'); +function Server(config) { + this.server = http.createServer(function(req, res) { + res.end('404: Not Found: ' + req.url); + }); + + this.config; + if(!config) { + this.config = { + username: process.env.QB_USERNAME || '', + password: process.env.QB_PASSWORD || '', + company_file: process.env.QB_COMPANY_FILE || '', + soap_port: process.env.QB_SOAP_PORT || 8000 + } + } else { + this.config = config; + } + + this.wsdl = buildWsdl(); + this.webService = null; + this.soapServer = null; } Server.prototype.run = function() { - var soapServer; - server.listen(port); - soapServer = soap.listen(server, '/wsdl', this.webService.service, this.wsdl); - console.log('Quickbooks SOAP Server listening on port ' + port); + this.server.listen(this.config.soap_port); + this.soapServer = soap.listen(this.server, '/wsdl', this.webService.service, this.wsdl); + console.log('Quickbooks SOAP Server listening on port ' + this.config.soap_port); }; -Server.prototype.setQBXMLHandler = function(qbXMLHandler) { - this.webService.setQBXMLHandler(qbXMLHandler); -}; \ No newline at end of file +Server.prototype.restart = function() { + let _this = this; + _this.server.close(() => { + console.log('Quickbooks SOAP Server closed'); + _this.run(); + }); +} + +Server.prototype.setQBXMLHandler = function(qbXMLHandler, config) { + this.webService = require('./web-service'); + this.webService.setQBXMLHandler(qbXMLHandler, config); +}; diff --git a/lib/web-service.js b/lib/web-service.js index bf2c119..5e7c364 100644 --- a/lib/web-service.js +++ b/lib/web-service.js @@ -36,9 +36,8 @@ var semver = require('semver'); /** * A library to generate UUIDs * - * https://github.com/broofa/node-uuid */ -var uuid = require('node-uuid'); +var uuid = require('uuid/v1'); /** * A constant for the minimum supported @@ -176,7 +175,7 @@ webService.QBWebConnectorSvc.QBWebConnectorSvcSoap.clientVersion = function(args */ webService.QBWebConnectorSvc.QBWebConnectorSvcSoap.authenticate = function(args, callback) { var authReturn = []; - authReturn[0] = uuid.v1(); + authReturn[0] = uuid(); if (args.strUserName.trim() === username && args.strPassword.trim() === password) { @@ -330,9 +329,12 @@ webService.QBWebConnectorSvc.QBWebConnectorSvcSoap.closeConnection = function(ar module.exports = { service: webService, - setQBXMLHandler: function(xmlHandler) { + setQBXMLHandler: function(xmlHandler, config) { qbXMLHandler = xmlHandler; + if(config) { + username = config.username; + password = config.password; + companyFile = config.company_file; + } } }; - - diff --git a/package.json b/package.json index 566dae5..d6f48f0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "quickbooks-js", - "version": "1.0.0", - "author": "Rappid Development LLC", + "name": "uh-quickbooks-js", + "version": "1.0.3", + "author": "uh-sem-blee", "description": "A SOAP service implemented in Node.js that communicates with QuickBook's Web Connector", "repository": { "type": "git", @@ -24,9 +24,9 @@ "license": "MIT", "dependencies": { "data2xml": "^1.2.5", - "node-uuid": "1.4.3", "semver": "^5.3.0", - "soap": "0.9.1" + "soap": "0.9.1", + "uuid": "^3.1.0" }, "devDependencies": { "chai": "^3.5.0", diff --git a/test/client/client.js b/test/client/client.js index c2c7870..b8e5973 100755 --- a/test/client/client.js +++ b/test/client/client.js @@ -13,7 +13,6 @@ // Private var soap = require('soap'); -var url = 'http://localhost:8000/wsdl?wsdl'; // Public module.exports = Client; @@ -23,8 +22,12 @@ module.exports = Client; * This acts as a mock quickbooks web connector. * @constructor */ -function Client() { - this.client = null; +function Client(port) { + this.url = 'http://localhost:8000/wsdl?wsdl'; + if(port) { + this.url = `http://localhost:${port}/wsdl?wsdl`; + } + this.client = null; } /** @@ -33,7 +36,7 @@ function Client() { */ Client.prototype.createClient = function(callback) { var that = this; - soap.createClient(url, function(err, client) { + soap.createClient(this.url, function(err, client) { if (err) return callback(err); that.client = client; return callback(null); diff --git a/test/test.js b/test/test.js index d2f6bd0..85876ba 100644 --- a/test/test.js +++ b/test/test.js @@ -16,8 +16,13 @@ var assert = chai.assert; var Client = require('./client/client'); var Server = require('../index'); -var soapClient = new Client(); -var soapServer = new Server(); +var soapClient = new Client(8123); +var soapServer = new Server({ + username: 'jjosef', + password: '12345', + company_file: '/company_file', + soap_port: 8123 +}); // Start soap Server soapServer.run(); @@ -130,4 +135,4 @@ describe('Soap Client', function() { done(); }); }); -}); \ No newline at end of file +});