Skip to content

Commit 06e2697

Browse files
committed
feat(calculate-shipping): handling warehouses for multi cd
1 parent b058b0a commit 06e2697

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

functions/ecom.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ const app = {
259259
required: ['zip'],
260260
additionalProperties: false,
261261
properties: {
262+
code: {
263+
type: 'string',
264+
maxLength: 30,
265+
pattern: '^[A-Za-z0-9-_]{2,30}$',
266+
title: 'Código do CD'
267+
},
262268
zip: {
263269
type: 'string',
264270
maxLength: 9,

functions/routes/ecom/modules/calculate-shipping.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ exports.post = ({ appSdk }, req, res) => {
3030
}
3131

3232
const destinationZip = params.to ? params.to.zip.replace(/\D/g, '') : ''
33-
const originZip = params.from
34-
? params.from.zip.replace(/\D/g, '')
35-
: appData.zip ? appData.zip.replace(/\D/g, '') : ''
36-
3733
const checkZipCode = rule => {
3834
// validate rule zip range
3935
if (destinationZip && rule.zip_range) {
@@ -43,11 +39,36 @@ exports.post = ({ appSdk }, req, res) => {
4339
return true
4440
}
4541

42+
let originZip, warehouseCode
43+
if (params.from) {
44+
originZip = params.from.zip
45+
} else if (Array.isArray(appData.warehouses) && appData.warehouses.length) {
46+
for (let i = 0; i < appData.warehouses.length; i++) {
47+
const warehouse = appData.warehouses[i]
48+
if (warehouse && warehouse.zip && checkZipCode(warehouse)) {
49+
const { code } = warehouse
50+
if (
51+
code && params.items &&
52+
params.items.find(({ inventory }) => inventory && Object.keys(inventory).length && !inventory[code])
53+
) {
54+
// item not available on current warehouse
55+
continue
56+
}
57+
originZip = warehouse.zip
58+
warehouseCode = code
59+
}
60+
}
61+
}
62+
if (!originZip) {
63+
originZip = appData.zip
64+
}
65+
originZip = typeof originZip === 'string' ? originZip.replace(/\D/g, '') : ''
66+
4667
// search for configured free shipping rule
4768
if (Array.isArray(appData.free_shipping_rules)) {
4869
for (let i = 0; i < appData.free_shipping_rules.length; i++) {
4970
const rule = appData.free_shipping_rules[i]
50-
if (checkZipCode(rule)) {
71+
if (rule && checkZipCode(rule)) {
5172
if (!rule.min_amount) {
5273
response.free_shipping_from_value = 0
5374
break
@@ -190,6 +211,7 @@ exports.post = ({ appSdk }, req, res) => {
190211
days: 3,
191212
...appData.posting_deadline
192213
},
214+
warehouse_code: warehouseCode,
193215
flags: ['datafrete-ws', `datafrete-${serviceCode}`.substr(0, 20)]
194216
}
195217
})

0 commit comments

Comments
 (0)