theendsoaper
Version:
Access Untill Tills with SOAP from Node.js and parse the results as objects, some extra useful functions in as well.
156 lines (138 loc) • 6.38 kB
JavaScript
const builder = require('xmlbuilder');
const Types = require('./function-types');
module.exports = class SoapBuilder {
//Builds the XML based on the task type
static buildXML(taskType, taskExtras) {
console.log('BUILDING: ' + taskType + " with properties: " + JSON.stringify(taskExtras));
var doc = builder.create('soapenv:Envelope')
.att('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
.att('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema')
.att('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/')
.att('xmlns:urn', 'urn:TPAPIPosIntfU-ITPAPIPOS')
.att('xmlns:soapenc', 'http://schemas.xmlsoap.org/soap/encoding/')
doc.ele('soapenv:Header')
doc.ele('soapenv:Body')
.ele('urn:' + taskType)
.att('soapenv:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/')
.ele('Request')
.att('xsi:type', 'urn:T' + taskType + 'Request')
.att('xmlns:urn', 'urn:TPAPIPosIntfU')
//adds the individual parts as an object
.ele(this.addExtras(taskType, taskExtras)).up()
.ele('Extra')
.att('xsi:type', 'urn:TExtraInfoArray')
.att('soapenc:arrayType', 'urn:TExtraInfo[1]')
.ele('item')
.att('xsi:type', 'urn:TExtraInfo')
.ele(this.getExtras(taskType));
//console.log(doc.toString({pretty: true}));
return doc.toString({ pretty: true });
}
//works out which object to make
static addExtras(taskType, taskExtras) {
var types = new Types();
//console.log('Adding Extras: ' + taskType + " with properties: " + JSON.stringify(taskExtras));
switch (taskType) {
case types.getActiveOrders():
return this.getActiveOrders(taskExtras);
case types.getActiveTableInfo():
return this.getActiveTableInfo(taskExtras);
case types.getArticlesInfo():
return this.getArticlesInfo(taskExtras);
case types.getCancelledItemsReport():
return this.getCancelledItemsReport(taskExtras);
case types.getDetailedTurnoverReport():
return this.getDetailedTurnoverReport(taskExtras);
case types.getInOutCashReport():
return this.getInOutCashReport(taskExtras);
}
}
//No Extras Required as of now
static getActiveOrders(properties) {
var obj = {
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token }
}
return obj;
}
//Addomg TableNum and TablePart
static getActiveTableInfo(properties) {
var obj = {
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
TableNumber: { '@xsi:type': 'xsd:int', '#text': properties.tableNumber },
TablePart: { '@xsi:type': 'xsd:string', '#text': properties.tablePart },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token }
}
return obj;
}
static getArticlesInfo(properties) {
var obj = {
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token },
Extra: {
'@xsi:type': 'urn1:TExtraInfoArray',
'@soapenc:arrayType': 'urn1:TExtraInfo[]',
'@xmlns:urn1': 'urn:TPAPIPosTypesU',
item: {
'@xsi:type': 'NS3:TExtraInfo',
Key: { '@xsi:type': 'xsd:string', '#text': 'DailyStock' },
Value: { '@xsi:type': 'xsd:long', '#text': 1 }
}
},
}
return obj;
}
static getCancelledItemsReport(properties) {
var obj = {
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
From: { '@xsi:type': 'xsd:dateTime', '#text': properties.from },
Till: { '@xsi:type': 'xsd:dateTime', '#text': properties.till },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token }
}
return obj;
}
static getDetailedTurnoverReport(properties) {
console.log("Getting detailed turnover report: ", properties);
var obj = {
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
From: { '@xsi:type': 'xsd:dateTime', '#text': properties.from },
Till: { '@xsi:type': 'xsd:dateTime', '#text': properties.till },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token }
}
return obj;
}
static getInOutCashReport(properties) {
var obj = {
UserName: { '@xsi:type': 'xsd:string', '#text': properties.username },
Password: { '@xsi:type': 'xsd:string', '#text': properties.password },
From: { '@xsi:type': 'xsd:dateTime', '#text': properties.from },
Till: { '@xsi:type': 'xsd:dateTime', '#text': properties.till },
AppToken: { '@xsi:type': 'xsd:string', '#text': properties.token }
}
return obj;
}
static getExtras(taskType) {
if (taskType) {
var obj = {
Value: { '@xsi:type': 'xsd:string' },
}
} else {
return {};
}
return obj;
var types = new Types();
if (taskType === types.getDetailedTurnoverReport()) {
var obj = {
Value: { '@xsi:type': 'xsd:string' }
}
return obj;
} else {
return {};
}
}
};