-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.js
More file actions
36 lines (30 loc) · 1.25 KB
/
sync.js
File metadata and controls
36 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Web service that accepts an optional list of product orders and returns
* an updated products table. If orders were received then they are written
* to order header/detail tables.
*
* This code is designed to match what the offline mobile app's sync() function
* sends and expects to receive.
*/
function sync(req, res) {
// Process orders if any exist
if (req.body && req.body.orders) {
// Write order header record and then retrieve the order ID generated by the database
const result = pjs.query("SELECT OHID FROM FINAL TABLE(INSERT INTO ORDHDR VALUES(DEFAULT, DEFAULT) WITH NC)", null, 1);
// Write order detail records and update product quantities
const orderId = result.ohid;
req.body.orders.forEach(function (order) {
order.orid = orderId;
pjs.query("INSERT INTO ORDDTL SET ? WITH NC", order);
pjs.query("UPDATE PRODUCTSP SET PRQTY = PRQTY - ? WHERE PRID = ? WITH NC", [order.orqty, order.orprid]);
});
}
// Send back a current copy of the product file
let records = pjs.query("SELECT * FROM PRODUCTSP");
if (records == null) {
records = [];
}
const products = { products: records };
res.send(products);
}
exports.run = sync;