-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
157 lines (144 loc) · 4.28 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
const fetch = require("node-fetch");
const { config } = require("../../../config.js");
/**
* Receives the request, validates the price and inventory in Wix and sends the response.
*
* @param {Object} requestEvent the request event built by Netlify Functions
* @returns {Promise<{statusCode: number, body: string}>} the response object
*/
async function handler(requestEvent) {
const cart = JSON.parse(requestEvent.body);
console.log(`Starting up for transaction ${cart.id}`);
const items = cart["_embedded"]["fx:items"];
const mismatchedPrice = [];
const insufficientInventory = [];
try {
for (const item of items) {
const slug = item["_embedded"]["fx:item_options"].find(
(option) => option.name.toLowerCase() === "slug"
)?.value;
if (!slug) {
const details = `Cannot find slug in item options for item ${item.name}`;
console.error(details);
return {
body: JSON.stringify({
details,
ok: false,
}),
statusCode: 200,
};
}
const wixCreds = config.datastore.provider.wix;
const res = await fetch(
"https://www.wixapis.com/stores-reader/v1/products/query",
{
body: JSON.stringify({
includeVariants: true,
query: {
filter: JSON.stringify({ slug }),
},
}),
headers: {
Authorization: wixCreds.apiKey,
"Content-Type": "application/json",
"wix-account-id": wixCreds.accountId,
"wix-site-id": wixCreds.siteId,
},
method: "POST",
}
);
const data = await res.json();
if (data.totalResults !== 1) {
const details = `Cannot find product in Wix by slug ${slug}`;
console.error(details);
return {
body: JSON.stringify({
details,
ok: false,
}),
statusCode: 200,
};
}
const product = data.products[0];
const variant = product.variants.find(
(variant) => variant.variant.sku === item.code
);
if (!variant) {
const details = `Cannot find variant by sku ${item.code}`;
console.error(details);
return {
body: JSON.stringify({
details,
ok: false,
}),
statusCode: 200,
};
}
const skipPriceCodes =
(config.datastore.skipValidation.price || "")
.split(",")
.map((code) => code.trim())
.filter((code) => !!code) || [];
const skipInventoryCodes =
(config.datastore.skipValidation.inventory || "")
.split(",")
.map((code) => code.trim())
.filter((code) => !!code) || [];
if (!skipPriceCodes.includes(item.code)) {
const price = variant.variant.priceData.discountedPrice;
if (price !== item.price) {
mismatchedPrice.push(item.code);
}
}
if (!skipInventoryCodes.includes(item.code)) {
const stock = variant.stock;
if (stock.trackQuantity) {
if (stock.quantity < item.quantity) {
insufficientInventory.push(item.code);
}
} else {
if (!stock.inStock) {
insufficientInventory.push(item.code);
}
}
}
}
if (mismatchedPrice.length > 0 || insufficientInventory.length > 0) {
console.error({ insufficientInventory, mismatchedPrice });
return {
body: JSON.stringify({
details:
(insufficientInventory.length > 0
? `Insufficient inventory for these items: ${insufficientInventory}. `
: "") +
(mismatchedPrice.length > 0
? `Price does not match for these items: ${mismatchedPrice}.`
: ""),
ok: false,
}),
statusCode: 200,
};
} else {
console.log("All checks have passed");
return {
body: JSON.stringify({
details: "",
ok: true,
}),
statusCode: 200,
};
}
} catch (error) {
console.error(error);
return {
body: JSON.stringify({
details: "An internal error has occurred",
ok: false,
}),
statusCode: 500,
};
}
}
module.exports = {
handler,
};