-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
515 lines (491 loc) · 15.5 KB
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
const FoxyWebhook = require("../../foxy/FoxyWebhook.js");
const Webflow = require("webflow-api");
const { config } = require("../../../config.js");
let webflowApi;
function setApi(api) {
webflowApi = api;
}
/**
* Returns custom Options set as environment variables.
*
* @returns {{webflow: {limit: number}, skip: {price: *[], inventory: *[]}, fields: {code: (*|string), price: (*|string), inventory: (*|string)}}} custom options
*/
function customOptions() {
return {
fields: {
code: config.datastore.field.code || 'code',
inventory: config.datastore.field.inventory || 'inventory',
price: config.datastore.field.price || 'price',
},
skip: {
inventory: (config.datastore.skipValidation.inventory || '').split(',').map(e => e.trim()).filter(e => !!e) || [],
price: (config.datastore.skipValidation.price || '').split(',').map(e => e.trim()).filter(e => !!e) || [],
updateinfo: config.datastore.skipValidation.updateinfo || 'Update Your Customer Information',
},
webflow: {
limit: 100,
},
}
}
/**
* Returns error messages available.
*
* @returns {Record<string, string>} error messages
*/
function getMessages() {
return {
insufficientInventory: config.datastore.error.insufficientInventory ||
'Insufficient inventory for these items:',
priceMismatch: config.datastore.error.priceMismatch ||
'Prices do not match.',
}
}
/**
* @param {Object} requestEvent the request event built by Netlify Functions
* @returns {Promise<{statusCode: number, body: string}>} the response object
*/
async function handler(requestEvent) {
// Validation
if (!validation.configuration.validate()) {
return validation.configuration.response();
}
if (!validation.input.validate(requestEvent)) {
return validation.input.response();
}
const items = extractItems(requestEvent.body);
if (!validation.items.validate(items)) {
return validation.items.response(items);
}
const values = [];
const cache = createCache();
// Fetch information needed to validate the cart
try {
await items.reduce(
(p, i) => p.then(
(accum) => fetchItem(cache, i).then((fetched) => {
values.push(fetched);
return accum;
}),
), Promise.resolve(values),
);
let failed = findMismatch(values);
if (!failed) {
const outOfStock = outOfStockItems(values);
if (outOfStock) {
failed = getMessages().insufficientInventory + " " + outOfStock;
}
}
if (failed) {
return {
body: JSON.stringify({ details: failed, ok: false, }),
statusCode: 200,
};
} else {
console.log('OK: payment approved - no mismatch found')
return {
body: JSON.stringify({ details: '', ok: true, }),
statusCode: 200,
};
}
} catch (e) {
console.error(e);
return {
body: JSON.stringify({ details: "An internal error has occurred", ok: false, }),
statusCode: 500,
};
}
}
/**
* Get an option of an item.
*
* The option may be set in the object itself or in the fx:item_options property of the _embedded attribute
*
* @param {object} item the item that should have the option
* @param {string} option to be retrieved
* @returns {{}|{name: string, value: string|number}} name and value of the option
* returns an empty object if the option is not available
*/
function getOption(item, option) {
let found = iGet(item, option);
if (found) return { name: option, value: iGet(item, option) };
if (item._embedded) {
if (item._embedded['fx:item_options']) {
found = item._embedded['fx:item_options'].find((e) => e.name.toLowerCase().trim() === option.toLowerCase().trim());
if (found) return found;
}
}
return {};
}
/**
* Returns the custom key for a given option, if it is set, or the default key.
*
* @param {string} default_key to be checked
* @returns {string} actual key to be used
*/
function getCustomKey(default_key) {
const options = customOptions();
if (Object.keys(options.fields).indexOf(default_key) >= 0) {
return options.fields[default_key];
} else {
return default_key;
}
}
/**
* Retrieve an option from an item using it's custom key, if set, or the default key
*
* @param {Object} item the item to retrieve the custom option from
* @param {string} option the option to retrieve
* @returns {{}|{name: string, value: string|number}} the retrieved option
*/
function getCustomizableOption(item, option) {
const custom_option = getCustomKey(option);
let result = getOption(item, custom_option);
if (!result) result = {};
return result;
}
/**
* Creates a cache object to store collection items and avoid repeated requests to webflow within the same execution.
*
* This cache is not intended to persist data between requests, but to simplify getting the Webflow Items in the same request.
*
* @returns {{addItems: Function, cache: object, findItem: Function}} a Cache object
*/
function createCache() {
return {
addItems(collection, items) {
if (!this.cache[collection]) {
this.cache[collection] = [];
}
this.cache[collection] = this.cache[collection].concat(items);
},
cache: {},
findItem(collection, item) {
if (!this.cache[collection]) {
return null;
}
return this.cache[collection].find(
(e) => {
const itemCode = item.code;
const wfCode = getCustomizableOption(e, 'code').value;
return itemCode && wfCode && wfCode.toString().trim() === itemCode.toString().trim();
},
);
},
};
}
/**
* Extract items from payload received from FoxyCart
*
* @param {string} body of the response received from Webflow
* @returns {Array} an array of items
*/
function extractItems(body) {
const objBody = JSON.parse(body);
if (objBody && objBody._embedded && objBody._embedded['fx:items']) {
return objBody._embedded['fx:items'].filter(item => item.name !== customOptions().skip.updateinfo);
}
return [];
}
/**
* Checks if item is valid
*
* @param {Object} item to be validated
* @returns {boolean} valid
*/
function validItem(item) {
const errors = [];
if (!(item.price || parseInt(item.price, 10) === 0)) {
errors.push(`${item.name} has no price.`)
}
if (!(item.quantity || parseInt(item.quantity, 10) === 0)) {
errors.push(`${item.name} has no quantity.`)
}
if (!(item.code || parseInt(item.code, 10) === 0)) {
errors.push(`${item.name} has no code.`)
}
if (!getCollectionId(item)) {
errors.push(`${item.name} has no collection_id.`)
}
if (errors.length) {
console.log("Invalid item ", item.name, errors.join(' '));
return false;
}
return true;
}
/**
* Validation checks
*/
const validation = {
configuration: {
response: () => ({
body: JSON.stringify({ details: 'Webflow token not configured.', ok: false }),
statusCode: 503,
}),
validate: () => !!config.datastore.provider.webflow.token,
},
input: {
errorMessage: "",
response: function() {
return {
body: JSON.stringify({ details: this.errorMessage, ok: false }),
statusCode: 400,
}
},
validate: function (requestEvent) {
this.errorMessage = FoxyWebhook.validFoxyRequest(requestEvent);
return !this.errorMessage;
}
},
items: {
response: (items) => ({
body: JSON.stringify({
details: `Invalid items: ${items.filter(e => !validItem(e)).map((e) => e.name).join(',')}`,
ok: false,
}),
statusCode: 200,
}),
validate: (items) => items.every(e => validItem(e)),
}
}
/**
* Checks if the price of the item is the same as found in WebFlow Collection
*
* @param comparable item received from webflow with item received from foxy embedded
* @returns {boolean} price is correct
*/
function isPriceCorrect(comparable) {
const wfItem = comparable.wfItem;
const fxItem = comparable.fxItem;
if (
!fxItem // an item with no matched item is not to be checked
|| customOptions().skip.price.indexOf(iGet(wfItem, getCustomKey('code'))) >=0 // items with price set to be skipped are not to be checked
) {
return true;
} else {
return parseFloat(fxItem.price) === parseFloat(iGet(wfItem, getCustomKey('price')));
}
}
/**
* Checks if there is sufficient inventory for this purchase.
*
* @param comparable pair of matched items to be checked
* @returns {boolean} the inventory is sufficient
*/
function sufficientInventory(comparable) {
const wfItem = comparable.wfItem;
const fxItem = comparable.fxItem;
const field = getCustomKey('inventory');
if (Number(fxItem.quantity) === 0) {
return true;
}
if (field.toLowerCase() === "null" || field.toLowerCase() === "false") {
// The webhook is configured not to check the inventory: ignore
return true;
}
if (customOptions().skip.inventory.indexOf(getCustomizableOption(wfItem, 'code').value) >= 0) {
// The code is set to be ignored: ignore
return true;
}
let inventoryField = Object.keys(wfItem).find(k => k.toLowerCase().trim() === field.toLowerCase().trim())
if (!inventoryField) {
const numbered = new RegExp(field.toLowerCase().trim()+'-\\d+');
inventoryField = Object.keys(wfItem).find(k => k.toLowerCase().trim().match(numbered));
}
if (inventoryField === undefined) {
// The Webflow collection does not have the proper inventory field: ignore
console.log(`Warning: the inventory field (${field}) does not exist in this webflow collection. Skipping inventory check.`);
console.log(`Available fields: `, Object.keys(wfItem));
return true;
}
const fxQuantity = Number(fxItem.quantity);
const wfInventory = Number(wfItem[inventoryField]);
if (isNaN(fxQuantity) || isNaN(wfInventory)) {
console.log(`Warning: a value for quantity or inventory is not a number: quantity ${fxQuantity} ; inventory: ${wfInventory}`)
return true;
}
return wfInventory >= fxQuantity;
}
/**
* Retrieve the Webflow Token
*
* @returns {string} the FOXY_WEBFLOW_TOKEN
*/
function getToken() {
return config.datastore.provider.webflow.token;
}
/**
* Retrieve an instance of the Webflow API Client
*
* @returns {Webflow} the webflow api object
*/
function getWebflow() {
if (!webflowApi) {
webflowApi = new Webflow({ token: getToken() });
}
return webflowApi;
}
/**
* Retrieve the collection id to be used for this item.
*
* When not set for the specific item, the collection id set for the whole environment is used.
*
* @param {Object} item the item received from Foxy
* @returns {string|number} the collection item to be used to fetch data on this item.
*/
function getCollectionId(item) {
return getOption(item, 'collection_id').value || config.datastore.provider.webflow.collection;
}
/**
* Stores a reference to the matched item in the item itself.
* returns a pair of matched items that can be easily validated.
*
* @param webflowItem the item received from Webflow
* @param foxyItem the item received from Foxy
* @returns {object} a pair of comparable items
*/
function enrichFetchedItem(webflowItem, foxyItem) {
return {fxItem: foxyItem, wfItem: webflowItem};
}
/**
* Returns a recursive promise that fetches items from the collection until it
* finds the item. Resolves the found item.
*
* Note: this method will take time linear on the size of the collection.
* For large collections it will probably timeout.
*
* Webflow does not provide a documented feature for retrieving filtered
* results based on arbitrary field.
*
* @param {object} cache object
* @param {object} foxyItem received from foxycart
* @param {number} offset number of items to skip
* @returns {Promise<{object}>} a promise for the item from Webflow
*/
function fetchItem(cache, foxyItem, offset = 0) {
if (offset > 500) {
console.log(" ... giving up.");
return Promise.reject(new Error('Item not found'));
}
if (offset) {
console.log(" ... couldn't find the item in first", offset, "items.");
}
const collectionId = getCollectionId(foxyItem);
const webflow = getWebflow();
const found = cache.findItem(collectionId, foxyItem);
if (found) {
return Promise.resolve(enrichFetchedItem(found, foxyItem));
}
return new Promise((resolve, reject) => {
webflow.items(
{ collectionId }, { limit: customOptions().webflow.limit, offset, sort: [getCustomKey('code'), 'ASC'] },
).then((collection) => {
cache.addItems(collectionId, collection.items);
let code_exists = null;
const match = collection.items.find(
(e) => {
const wfItemCode = iGet(e, getCustomKey('code'));
if (wfItemCode === undefined) {
if (code_exists === null) {
code_exists = false;
}
return false;
}
code_exists = true;
return foxyItem.code && wfItemCode.toString() === foxyItem.code.toString()
}
);
if (code_exists === false) {
reject(new Error(`Could not find the code field (${getCustomKey('code')}) in Webflow.
this field must exist and not be empty for all items in the collection.`));
} else {
if (match) {
resolve(enrichFetchedItem(match, foxyItem));
} else if (collection.total > collection.offset + collection.count) {
fetchItem(cache, foxyItem, ((offset / customOptions().webflow.limit) + 1) * customOptions().webflow.limit)
.then((i) => resolve(i))
.catch((e) => {reject(e);});
} else {
reject(new Error('Item not found'));
}
}
}).catch((e) => {
reject(e);
});
});
}
/**
* Checks if a particular enriched item should be evaluated or not
*
* @param comparable enriched item to evaluate
* @returns {boolean} the item should be evaluated
*/
function shouldEvaluate(comparable) {
// Ignore past subscriptions
const fxItem = comparable.fxItem;
if (
fxItem.subscription_frequency
&& fxItem.subscription_start_date
) {
const subscriptionStart = new Date(fxItem.subscription_start_date);
const stripTime = (v) => v.replace(/T.*$/, '');
// Convert to UTC, strip time and compare
if (stripTime(new Date().toISOString()) > stripTime(subscriptionStart.toISOString())) {
return false;
}
}
// Evaluates by default
return true;
}
/**
* Searches for an invalid value applying a list of criteria
*
* @param values to find mismatches
* @returns {boolean|string} mismatches were found
*/
function findMismatch(values) {
const evaluations = [
[isPriceCorrect, getMessages().priceMismatch],
];
for (let v = 0; v < values.length; v += 1) {
if (shouldEvaluate(values[v])) {
for (let i = 0; i < evaluations.length; i += 1) {
if (!evaluations[i][0](values[v])) {
return evaluations[i][1];
}
}
}
}
return false;
}
/**
* Returns a list of names of products that are out of stock
*
* @param {Array<Object>} values comparable objects
* @returns {string} comma separated out of stock values
*/
function outOfStockItems(values) {
return values
.filter(v => !sufficientInventory(v))
.map(v => v.wfItem.name)
.join(', ')
;
}
/**
* Returns a value from an object given a case-insensitive key
*
* @param {object} object the object to get the value from
* @param {string} key field to get the value from
* @returns {any} the value stored in the key
*/
function iGet(object, key) {
const numbered = new RegExp(key.toLowerCase().trim()+'(-\\d+)?');
const existingKey = Object.keys(object).filter(k => k.toLowerCase().trim().match(numbered)).sort();
return object[existingKey[0]];
}
module.exports = {
handler,
getWebflow,
extractItems,
getCustomizableOption,
setApi
}