Skip to content

Commit 45c5903

Browse files
committed
feat(orderdesk): inventory update skipping
1 parent 4fe13b5 commit 45c5903

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/foxy/DataStoreBase.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
const config = require("../../config.js");
2+
13
class DataStoreBase {
24

5+
skipUpdate = {
6+
inventory: []
7+
}
8+
39
constructor() {
410
const abstractMethods = [
511
'fetchItems',
@@ -12,6 +18,20 @@ class DataStoreBase {
1218
if (unimplemented.length) {
1319
throw new TypeError(unimplemented.join(',') + " must be overriden");
1420
}
21+
this.skipFromEnv();
22+
}
23+
24+
/**
25+
* Autoconfigures the instance to skip the validation of prices and inventory
26+
* of items with codes listed in the configured environment variables.
27+
*/
28+
skipFromEnv() {
29+
if (config.datastore.skipUpdate.inventory === '__ALL__') {
30+
this.skipUpdate.inventory.all = true;
31+
}
32+
this.skipUpdate.inventory.concat(
33+
(config.datastore.skipUpdate.inventory || '').split(',')
34+
);
1535
}
1636

1737
/**

src/functions/datastore-integration-orderdesk/DataStore.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ class DataStore extends DataStoreBase {
101101
/**
102102
* Update inventory items in OrderDesk
103103
*
104-
* @param {Array<OrderDeskItem>} items to be updated
104+
* It won't update any items if the skip update inventory option is set to __ALL__.
105+
* It won't update any items whose codes are set in skip update inventory option.
106+
*
107+
* @param {Array<OrderDeskItem>} items to be updated.
108+
* @returns {Object} the OrderDesk response.
105109
*/
106110
async updateInventoryItems(items) {
111+
if (this.skipUpdate.inventory.all) {
112+
return {};
113+
}
114+
items = items.filter(i => !this.skipUpdate.inventory.includes(i.code));
107115
const invalid = items.filter((i) => !this.validateInventoryItem(i));
108116
if (invalid.length) {
109117
throw new Error("Invalid inventory items for update", invalid.join(','));

src/functions/datastore-integration-orderdesk/webhook.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,26 @@ async function prePayment(body) {
4141
* Process a transaction/created Foxy Webhook.
4242
*
4343
* @param {Object} body the parsed body of the Foxy request.
44-
* @returns {}
44+
* @returns {Response}
4545
*/
4646
async function transactionCreated(body) {
47-
const datastore = getDataStore();
48-
const pairs = await buildPairs(body, FoxyWebhook, datastore);
49-
const updated = pairs.map(p => ({...p[1],
50-
stock: Number(p[1].inventory) - Number(p[0].quantity), // Notice that OrderDesk inventory field is "stock"
51-
}));
52-
const result = await datastore.updateInventoryItems(updated);
53-
if (result.status === 'success') {
47+
if (config.datastore.skipUpdate.inventory === '__ALL__' ) {
5448
return response();
55-
} else {
49+
}
50+
try {
51+
const datastore = getDataStore();
52+
const pairs = await buildPairs(body, FoxyWebhook, datastore);
53+
const updated = pairs.map(p => ({...p[1],
54+
stock: Number(p[1].inventory) - Number(p[0].quantity), // Notice that OrderDesk inventory field is "stock"
55+
}));
56+
const result = await datastore.updateInventoryItems(updated);
57+
if (result.status === 'success') {
58+
return response();
59+
} else {
60+
return response('Internal Server Error', 500);
61+
}
62+
} catch (e) {
63+
console.error('Could not update inventory', e.name, e.message);
5664
return response('Internal Server Error', 500);
5765
}
5866
}

0 commit comments

Comments
 (0)