Skip to content

Commit

Permalink
chore: add warehouse in calc
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis authored Jul 5, 2024
1 parent 9e94e42 commit 6177e75
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 @@ -29,10 +29,43 @@ exports.post = ({ appSdk }, req, res) => {
response.free_shipping_from_value = appData.free_shipping_from_value
}

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 originZip, warehouseCode, docNumber
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, '') : ''

const checkZipCode = rule => {
// validate rule zip range
Expand Down Expand Up @@ -226,7 +259,7 @@ exports.post = ({ appSdk }, req, res) => {
},
posting_deadline: {
days: 3,
...appData.posting_deadline
...postingDeadline
},
package: {
weight: {
Expand Down Expand Up @@ -299,6 +332,9 @@ exports.post = ({ appSdk }, req, res) => {
response.shipping_services.push({
label,
carrier: 'Correios (Manda Bem)',
carrier_doc_number: isWareHouse && docNumber
? docNumber
: '34028316000103',
service_name: servico,
shipping_line: shippingLine
})
Expand Down

0 comments on commit 6177e75

Please sign in to comment.