From db31cc9156b64aa13530dc82921bd05c19e7ecf0 Mon Sep 17 00:00:00 2001 From: Matheus Reis <35343551+matheusgnreis@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:10:22 -0300 Subject: [PATCH] chore: add warehouse doc --- .../routes/ecom/modules/calculate-shipping.js | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/functions/routes/ecom/modules/calculate-shipping.js b/functions/routes/ecom/modules/calculate-shipping.js index 244acac..46e1af1 100644 --- a/functions/routes/ecom/modules/calculate-shipping.js +++ b/functions/routes/ecom/modules/calculate-shipping.js @@ -128,12 +128,45 @@ exports.post = ({ appSdk }, req, res) => { if (appData.free_shipping_from_value >= 0) { response.free_shipping_from_value = appData.free_shipping_from_value } - + let originZip, warehouseCode, docNumber const destinationZip = params.to ? params.to.zip.replace(/\D/g, '') : '' - const originZip = params.from - ? params.from.zip.replace(/\D/g, '') - : appData.zip ? appData.zip.replace(/\D/g, '') : '' + let postingDeadline = appData.posting_deadline + let isWareHouse = false + if (params.from) { + originZip = params.from.zip + } else if (Array.isArray(appData.warehouses) && appData.warehouses.length) { + for (let i = 0; i < appData.warehouses.length; i++) { + const warehouse = appData.warehouses[i] + if (warehouse && warehouse.zip && checkZipCode(warehouse)) { + const { code } = warehouse + if (!code) { + continue + } + if ( + params.items && + params.items.find(({ quantity, inventory }) => inventory && Object.keys(inventory).length && !(inventory[code] >= quantity)) + ) { + // item not available on current warehouse + continue + } + originZip = warehouse.zip + isWareHouse = true + if (warehouse.posting_deadline) { + postingDeadline = warehouse.posting_deadline + } + if (warehouse.doc) { + docNumber = warehouse.doc + } + warehouseCode = code + } + } + } + + if (!originZip) { + originZip = appData.zip + } + originZip = typeof originZip === 'string' ? originZip.replace(/\D/g, '') : '' // search for configured free shipping rule if (Array.isArray(appData.shipping_rules)) { @@ -274,7 +307,7 @@ exports.post = ({ appSdk }, req, res) => { }, posting_deadline: { days: 3, - ...appData.posting_deadline + ...postingDeadline }, from: { zip: originZip @@ -298,6 +331,9 @@ exports.post = ({ appSdk }, req, res) => { } } } + if (docNumber) { + shippingLine.carrier_doc_number = docNumber + } response.shipping_services.push(shippingLine) } }