Skip to content

Commit 08461e9

Browse files
committed
feat: delivery "default" delivery time and total price from services
1 parent a165872 commit 08461e9

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

assets/application.json

+34-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"state": "active",
44
"title": "Frete personalizado",
55
"slug": "customshipping",
6-
"version": "1.6.0",
6+
"version": "1.7.0",
77
"type": "external",
88
"authentication": true,
99
"auth_callback_uri": "https://customshipping.ecomplus.biz/ecom/auth-callback",
@@ -71,6 +71,37 @@
7171
"maxLength": 14,
7272
"title": "CNPJ da transportadora",
7373
"description": "Insira apenas números do CNPJ"
74+
},
75+
"delivery_time": {
76+
"title": "Prazo de entrega padrão do serviço",
77+
"type": "object",
78+
"required": [
79+
"days"
80+
],
81+
"additionalProperties": false,
82+
"properties": {
83+
"days": {
84+
"type": "integer",
85+
"minimum": 0,
86+
"maximum": 999999,
87+
"default": 20,
88+
"title": "Prazo de entrega (dias)",
89+
"description": "Número de dias estimado para entrega após o despacho"
90+
},
91+
"working_days": {
92+
"type": "boolean",
93+
"default": true,
94+
"title": "Dias úteis",
95+
"description": "Se o prazo é calculado em dias úteis"
96+
}
97+
}
98+
},
99+
"total_price": {
100+
"type": "number",
101+
"minimum": 0,
102+
"maximum": 9999999999,
103+
"title": "Preço padrão do serviço",
104+
"description": "Valor do frete com possíveis taxas e adicionais fixos"
74105
}
75106
}
76107
}
@@ -127,9 +158,7 @@
127158
"title": "Regra de envio",
128159
"type": "object",
129160
"required": [
130-
"service_code",
131-
"delivery_time",
132-
"total_price"
161+
"service_code"
133162
],
134163
"properties": {
135164
"service_code": {
@@ -142,8 +171,7 @@
142171
"title": "Faixa de CEP atendida",
143172
"type": "object",
144173
"required": [
145-
"min",
146-
"max"
174+
"min"
147175
],
148176
"properties": {
149177
"min": {

routes/ecom/modules/calculate-shipping.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ module.exports = appSdk => {
2323
}
2424

2525
const destinationZip = params.to ? params.to.zip.replace(/\D/g, '') : ''
26-
const checkZipCode = rule => {
27-
// validate rule zip range
26+
const checkZipCode = (rule) => {
2827
if (destinationZip && rule.zip_range) {
2928
const { min, max } = rule.zip_range
30-
return Boolean((!min || destinationZip >= min) && (!max || destinationZip <= max))
29+
if (!min && !max) {
30+
return true
31+
}
32+
if (!max) {
33+
return destinationZip === min
34+
}
35+
if (!min) {
36+
return destinationZip === max
37+
}
38+
return destinationZip >= min && destinationZip <= max
3139
}
3240
return true
3341
}
@@ -196,8 +204,15 @@ module.exports = appSdk => {
196204
if (validShippingRules.length) {
197205
// group by service code selecting lower price
198206
const shippingRulesByCode = validShippingRules.reduce((shippingRulesByCode, rule) => {
207+
const serviceCode = rule.service_code
199208
if (typeof rule.total_price !== 'number') {
200-
rule.total_price = 0
209+
let service
210+
if (Array.isArray(config.services)) {
211+
service = config.services.find((_service) => {
212+
return _service && _service.service_code === serviceCode
213+
})
214+
}
215+
rule.total_price = (service && service.total_price) || 0
201216
}
202217
if (typeof rule.price !== 'number') {
203218
rule.price = rule.total_price
@@ -208,7 +223,6 @@ module.exports = appSdk => {
208223
if (typeof rule.amount_tax === 'number' && !isNaN(rule.amount_tax)) {
209224
rule.total_price += (rule.amount_tax * amount / 100)
210225
}
211-
const serviceCode = rule.service_code
212226
const currentShippingRule = shippingRulesByCode[serviceCode]
213227
if (!currentShippingRule || currentShippingRule.total_price > rule.total_price) {
214228
shippingRulesByCode[serviceCode] = rule
@@ -236,8 +250,15 @@ module.exports = appSdk => {
236250
service = config.services.find(service => {
237251
return service && service.service_code === serviceCode
238252
})
239-
if (service && !label) {
240-
label = service.label
253+
if (service) {
254+
if (!label) {
255+
label = service.label
256+
}
257+
if (!rule.delivery_time || typeof rule.delivery_time.days !== 'number') {
258+
rule.delivery_time = service.delivery_time
259+
}
260+
delete service.delivery_time
261+
delete service.total_price
241262
}
242263
}
243264
if (!label) {

0 commit comments

Comments
 (0)