|
| 1 | +const fetch = require("node-fetch"); |
| 2 | +const { config } = require("../../../config.js"); |
| 3 | + |
| 4 | +/** |
| 5 | + * Receives the request, validates the price and inventory in Wix and sends the response. |
| 6 | + * |
| 7 | + * @param {Object} requestEvent the request event built by Netlify Functions |
| 8 | + * @returns {Promise<{statusCode: number, body: string}>} the response object |
| 9 | + */ |
| 10 | +async function handler(requestEvent) { |
| 11 | + const cart = JSON.parse(requestEvent.body); |
| 12 | + |
| 13 | + console.log(`Starting up for transaction ${cart.id}`); |
| 14 | + |
| 15 | + const items = cart["_embedded"]["fx:items"]; |
| 16 | + const mismatchedPrice = []; |
| 17 | + const insufficientInventory = []; |
| 18 | + |
| 19 | + try { |
| 20 | + for (const item of items) { |
| 21 | + const slug = item["_embedded"]["fx:item_options"].find( |
| 22 | + (option) => option.name.toLowerCase() === "slug" |
| 23 | + )?.value; |
| 24 | + if (!slug) { |
| 25 | + const details = `Cannot find slug in item options for item ${item.name}`; |
| 26 | + console.error(details); |
| 27 | + return { |
| 28 | + body: JSON.stringify({ |
| 29 | + details, |
| 30 | + ok: false, |
| 31 | + }), |
| 32 | + statusCode: 200, |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + const wixCreds = config.datastore.provider.wix; |
| 37 | + const res = await fetch( |
| 38 | + "https://www.wixapis.com/stores-reader/v1/products/query", |
| 39 | + { |
| 40 | + body: JSON.stringify({ |
| 41 | + includeVariants: true, |
| 42 | + query: { |
| 43 | + filter: JSON.stringify({ slug }), |
| 44 | + }, |
| 45 | + }), |
| 46 | + headers: { |
| 47 | + Authorization: wixCreds.apiKey, |
| 48 | + "Content-Type": "application/json", |
| 49 | + "wix-account-id": wixCreds.accountId, |
| 50 | + "wix-site-id": wixCreds.siteId, |
| 51 | + }, |
| 52 | + method: "POST", |
| 53 | + } |
| 54 | + ); |
| 55 | + const data = await res.json(); |
| 56 | + |
| 57 | + if (data.totalResults !== 1) { |
| 58 | + const details = `Cannot find product in Wix by slug ${slug}`; |
| 59 | + console.error(details); |
| 60 | + return { |
| 61 | + body: JSON.stringify({ |
| 62 | + details, |
| 63 | + ok: false, |
| 64 | + }), |
| 65 | + statusCode: 200, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + const product = data.products[0]; |
| 70 | + const variant = product.variants.find( |
| 71 | + (variant) => variant.variant.sku === item.code |
| 72 | + ); |
| 73 | + |
| 74 | + if (!variant) { |
| 75 | + const details = `Cannot find variant by sku ${item.code}`; |
| 76 | + console.error(details); |
| 77 | + return { |
| 78 | + body: JSON.stringify({ |
| 79 | + details, |
| 80 | + ok: false, |
| 81 | + }), |
| 82 | + statusCode: 200, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + const skipPriceCodes = |
| 87 | + (config.datastore.skipValidation.price || "") |
| 88 | + .split(",") |
| 89 | + .map((code) => code.trim()) |
| 90 | + .filter((code) => !!code) || []; |
| 91 | + const skipInventoryCodes = |
| 92 | + (config.datastore.skipValidation.inventory || "") |
| 93 | + .split(",") |
| 94 | + .map((code) => code.trim()) |
| 95 | + .filter((code) => !!code) || []; |
| 96 | + |
| 97 | + if (!skipPriceCodes.includes(item.code)) { |
| 98 | + const price = variant.variant.priceData.discountedPrice; |
| 99 | + if (price !== item.price) { |
| 100 | + mismatchedPrice.push(item.code); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!skipInventoryCodes.includes(item.code)) { |
| 105 | + const stock = variant.stock; |
| 106 | + if (stock.trackQuantity) { |
| 107 | + if (stock.quantity < item.quantity) { |
| 108 | + insufficientInventory.push(item.code); |
| 109 | + } |
| 110 | + } else { |
| 111 | + if (!stock.inStock) { |
| 112 | + insufficientInventory.push(item.code); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (mismatchedPrice.length > 0 || insufficientInventory.length > 0) { |
| 119 | + console.error({ insufficientInventory, mismatchedPrice }); |
| 120 | + return { |
| 121 | + body: JSON.stringify({ |
| 122 | + details: |
| 123 | + (insufficientInventory.length > 0 |
| 124 | + ? `Insufficient inventory for these items: ${insufficientInventory}. ` |
| 125 | + : "") + |
| 126 | + (mismatchedPrice.length > 0 |
| 127 | + ? `Price does not match for these items: ${mismatchedPrice}.` |
| 128 | + : ""), |
| 129 | + ok: false, |
| 130 | + }), |
| 131 | + statusCode: 200, |
| 132 | + }; |
| 133 | + } else { |
| 134 | + console.log("All checks have passed"); |
| 135 | + return { |
| 136 | + body: JSON.stringify({ |
| 137 | + details: "", |
| 138 | + ok: true, |
| 139 | + }), |
| 140 | + statusCode: 200, |
| 141 | + }; |
| 142 | + } |
| 143 | + } catch (error) { |
| 144 | + console.error(error); |
| 145 | + return { |
| 146 | + body: JSON.stringify({ |
| 147 | + details: "An internal error has occurred", |
| 148 | + ok: false, |
| 149 | + }), |
| 150 | + statusCode: 500, |
| 151 | + }; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +module.exports = { |
| 156 | + handler, |
| 157 | +}; |
0 commit comments