Skip to content

Commit

Permalink
fix: setting warehouse specific address to retrieve on tag creation
Browse files Browse the repository at this point in the history
new address field on each warehouse config schema
  • Loading branch information
leomp12 committed Sep 18, 2024
1 parent ccb6fd5 commit 452edda
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 41 deletions.
107 changes: 82 additions & 25 deletions functions/ecom.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const app = {
schema: {
type: 'object',
title: 'Endereço do remetente',
description: 'Configure endereço de remetente para cálculo.',
description: 'Configure endereço de remetente para cálculo',
properties: {
street: {
type: 'string',
Expand Down Expand Up @@ -245,7 +245,6 @@ const app = {
maxLength: 100,
title: 'País de Origem'
},

}
},
hide: true
Expand Down Expand Up @@ -473,7 +472,7 @@ const app = {
items: {
title: 'Centro de distribuição',
type: 'object',
required: ['code', 'zip'],
required: ['code', 'zip', 'number'],
additionalProperties: false,
properties: {
code: {
Expand All @@ -488,13 +487,93 @@ const app = {
title: 'Api Key',
description: 'Api Key da Frete Click específica para o CD, se houver'
},
zip_range: {
title: 'Faixa de CEP atendida',
type: 'object',
required: [
'min',
'max'
],
properties: {
min: {
type: 'integer',
minimum: 10000,
maximum: 999999999,
title: 'CEP inicial'
},
max: {
type: 'integer',
minimum: 10000,
maximum: 999999999,
title: 'CEP final'
}
}
},
zip: {
type: 'string',
maxLength: 9,
pattern: '^[0-9]{5}-?[0-9]{3}$',
title: 'CEP de origem',
description: 'Código postal do remetente para cálculo do frete'
},
street: {
type: 'string',
maxLength: 100,
title: 'Rua'
},
borough: {
type: 'string',
maxLength: 100,
title: 'Bairro'
},
number: {
type: 'number',
maxLength: 100,
title: 'Número'
},
complement: {
type: 'number',
maxLength: 100,
title: 'Complemento'
},
city: {
type: 'string',
maxLength: 100,
title: 'Cidade de Origem'
},
province_code: {
type: 'string',
title: 'Sigla do Estado de Origem',
enum: [
'AC',
'AL',
'AP',
'AM',
'BA',
'CE',
'DF',
'ES',
'GO',
'MA',
'MT',
'MS',
'MG',
'PA',
'PB',
'PR',
'PE',
'PI',
'RR',
'RO',
'RJ',
'RS',
'RN',
'SC',
'SP',
'SE',
'TO'
]
},
posting_deadline: {
title: 'Prazo de envio do CD',
type: 'object',
Expand All @@ -519,28 +598,6 @@ const app = {
title: 'Após aprovação do pagamento'
}
}
},
zip_range: {
title: 'Faixa de CEP atendida',
type: 'object',
required: [
'min',
'max'
],
properties: {
min: {
type: 'integer',
minimum: 10000,
maximum: 999999999,
title: 'CEP inicial'
},
max: {
type: 'integer',
minimum: 10000,
maximum: 999999999,
title: 'CEP final'
}
}
}
}
}
Expand Down
33 changes: 18 additions & 15 deletions functions/lib/freteclick/create-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ module.exports = async (order, storeId, appData, appSdk) => {
let token = appData.api_key
const shippingLine = order.shipping_lines[0]
const warehouseCode = shippingLine.warehouse_code
const from = {
...appData.from,
zip: appData.zip,
...(order.shipping_lines?.[0]?.from)
}
if (warehouseCode) {
const warehouse = appData.warehouses?.find(({ code }) => code === warehouseCode)
if (warehouse.api_key) {
token = warehouse.api_key
if (warehouse) {
if (warehouse.api_key) {
token = warehouse.api_key
}
Object.assign(from, warehouse)
}
}
const fcCompany = await getCompany(token)
logger.info(`Freteclick ids for #${storeId}`, { fcCompany })
const customer = order.buyers?.[0]
const address = order.shipping_lines?.[0]?.to
const retrieve = {
...appData.from,
zip: appData.zip,
...(order.shipping_lines?.[0]?.from)
}
const freteClickCustom = (order, field) => {
const shippingCustom = order.shipping_lines?.[0]?.custom_fields
const customField = shippingCustom?.find(custom => custom.field === field)
Expand Down Expand Up @@ -65,14 +68,14 @@ module.exports = async (order, storeId, appData, appSdk) => {
"id": fcCompany.companyId,
"address": {
"id": null,
"country": retrieve.country || "Brasil",
"state": retrieve.province_code,
"city": retrieve.city,
"district": retrieve.borough,
"complement": retrieve.complement || "",
"street": retrieve.street,
"number": String(retrieve.number || 0),
"postal_code": String(retrieve.zip.replace(/\D/g, ''))
"country": from.country || "Brasil",
"state": from.province_code,
"city": from.city,
"district": from.borough,
"complement": from.complement || "",
"street": from.street,
"number": String(from.number || 0),
"postal_code": String(from.zip.replace(/\D/g, ''))
},
"contact": fcCompany.peopleId
},
Expand Down
1 change: 0 additions & 1 deletion functions/routes/ecom/modules/calculate-shipping.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ exports.post = async ({ appSdk }, req, res) => {
const shippingLine = {
from: {
...params.from,
...appData.from,
zip: originZip
},
to: params.to,
Expand Down

0 comments on commit 452edda

Please sign in to comment.