Skip to content

Commit

Permalink
chore: add warehouse doc
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis authored Jul 18, 2024
1 parent f99bebf commit db31cc9
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions functions/routes/ecom/modules/calculate-shipping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -274,7 +307,7 @@ exports.post = ({ appSdk }, req, res) => {
},
posting_deadline: {
days: 3,
...appData.posting_deadline
...postingDeadline
},
from: {
zip: originZip
Expand All @@ -298,6 +331,9 @@ exports.post = ({ appSdk }, req, res) => {
}
}
}
if (docNumber) {
shippingLine.carrier_doc_number = docNumber
}
response.shipping_services.push(shippingLine)
}
}
Expand Down

0 comments on commit db31cc9

Please sign in to comment.