UNPKG

theendsoaper

Version:

Access Untill Tills with SOAP from Node.js and parse the results as objects, some extra useful functions in as well.

121 lines (93 loc) 3.85 kB
const b = require('./modules/soap-builder'); const soap = require('./modules/soap-client'); const extra = require('./modules/build-extras'); const parse = require('./modules/xmlparse'); const levenshtein = require('js-levenshtein'); const types = require('./modules/function-types'); const print = require('./modules/printobject'); /* Data Requests */ async function allArticles(username, password, url, type, token) { return await request(url, type, new extra().buildExtrasBasic(username, password, token)); } async function activeOrders(username, password, url, type, token) { return await request(url, type, new extra().buildExtrasBasic(username, password, token)); } async function activeTableInfo(username, password, url, type, tableIndex, tableLetter, token) { return await request(url, type, new extra().buildExtrasTable(username, password, tableIndex, tableLetter, token)); } async function cancelledItems(username, password, url, type, startDate, endDate, token) { return await request(url, type, new extra().buildExtrasDate(username, password, startDate, endDate, token)); } async function inOutCash(username, password, url, type, startDate, endDate, token) { return await request(url, type, new extra().buildExtrasDate(username, password, startDate, endDate, token)); } async function detailedTurnoverReport(username, password, url, type, startDate, endDate, token) { return await request(url, type, new extra().buildExtrasDate(username, password, startDate, endDate, token)); } async function orderDrinks(username, password, url, type, drinks, token) { return await rest(url, type, new extra().drinkOrder(username, password, uid, drinks, token)); } /* Data Management */ //Given a drink name string, finds the drink with closest matching name function findDrink(drink, drinks) { var possibles = []; drinks.forEach(val => { var score = isSimilar(String(val.articleName), String(drink)); if (score <= 1) { val.score = score; possibles.push(val); } }) return possibles; } function isSimilar(a, b) { a = a.toLowerCase().replace(/\W/g, ''); b = b.toLowerCase().replace(/\W/g, ''); return levenshtein(a, b); } function findArticle(turnoverReport, id) { if (turnoverReport.length === undefined) { return "oi, i want a list"; } var report = {}; turnoverReport.forEach(a => { a.bills.forEach(b => { if (b.billNumber === id) { report = a; } }) }) return report; } module.exports.findArticle = findArticle; module.exports.allArticles = allArticles; module.exports.activeOrders = activeOrders; module.exports.activeTableInfo = activeTableInfo; module.exports.cancelledItems = cancelledItems; module.exports.inOutCash = inOutCash; module.exports.detailedTurnoverReport = detailedTurnoverReport; module.exports.findDrink = findDrink; module.exports.orderDrinks = orderDrinks; async function request(url, type, extras) { var [response, error] = await soap.soapRequest(b.buildXML(type, extras), url).then((response) => { return [response, null] }).catch((error) => { return [null, error] }); if (error) { console.log("Error: " + error); throw new Error(error); } if (response === undefined) { throw new Error("No response"); } try { var parsed = new parse().parse(response, type); return parsed; } catch (err) { console.error("Error parsing: " + err); console.log(response); console.log("Type: " + type); throw new Error(err); } } async function rest(url, type, extras) { if (type === "OrderDrinks") { } }