Skip to content

Commit

Permalink
fix: better handling new freteclick customer and error debugs
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Sep 16, 2024
1 parent 907a6ac commit a7751e1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
3 changes: 2 additions & 1 deletion functions/lib/freteclick/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ module.exports = ({
token,
data,
timeout = 8000
}) => {
}, axiosConfig = {}) => {
const config = {
...axiosConfig,
url,
method,
headers: {
Expand Down
47 changes: 27 additions & 20 deletions functions/lib/freteclick/create-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const getOrCreateCustomer = require('./customer')
const getCompanyId = require('./me')
const client = require('./client')

const debugAxiosError = error => {
const err = new Error(error.message)
if (error.response) {
err.status = error.response.status
err.response = error.response.data
}
err.request = error.config
logger.error(err)
}

module.exports = async (order, storeId, appData, appSdk) => {
let token = appData.api_key
const shippingLine = order.shipping_lines[0]
Expand All @@ -14,11 +24,8 @@ module.exports = async (order, storeId, appData, appSdk) => {
token = warehouse.api_key
}
}
const {
peopleId,
companyId
} = await getCompanyId(token)
console.log('people id and company id', peopleId, companyId)
const { peopleId, companyId } = await getCompanyId(token)
logger.info(`Freteclick ids for #${storeId}`, { peopleId, companyId })
const customer = order.buyers?.[0]
const address = order.shipping_lines?.[0]?.to
const retrieve = {
Expand All @@ -37,8 +44,18 @@ module.exports = async (order, storeId, appData, appSdk) => {
}
const quoteId = freteClickCustom(order, 'freteclick_id')
const orderId = freteClickCustom(order, 'freteclick_order_id')
const { id } = await getOrCreateCustomer(token, customer, address)
console.log('id customer', id)
let id
try {
id = (await getOrCreateCustomer(token, customer, address)).id
} catch (error) {
if (error.response) {
debugAxiosError(error)
} else {
logger.error(error)
}
throw error
}
logger.info(`Freteclick customer ${id} for #${storeId}`)
const data = {
"quote": quoteId,
"price": order.amount && order.amount.freight,
Expand Down Expand Up @@ -74,26 +91,16 @@ module.exports = async (order, storeId, appData, appSdk) => {
"contact": id
}
}

const debugAxiosError = error => {
const err = new Error(error.message)
if (error.response) {
err.status = error.response.status
err.response = error.response.data
}
err.request = error.config
logger.error(err)
}
console.log('frete click body', JSON.stringify(data))
logger.info(`Freteclick tag for ${storeId} ${order._id}`, { data })

return client({
url: `/purchasing/orders/${orderId}/choose-quote`,
method: 'put',
token,
data
}).then(res => res.data)
})
.then(res => res.data)
.catch(err => {
console.log('erro ao gerar tag', err)
debugAxiosError(err)
throw err
})
Expand Down
73 changes: 43 additions & 30 deletions functions/lib/freteclick/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,55 @@ const freteClickApi = require('./client')
const ecomUtils = require('@ecomplus/utils')

module.exports = async (token, customer, address) => {
const { data, status } = await freteClickApi({
const { data } = await freteClickApi({
url: `/people/customer?email=${customer.main_email}`,
method: 'get',
token
}, {
validateStatus (status) {
return status === 200
}
})
if (status === 200 && data && data.response && data.response.data && data.response.count) {
const { id } = data.response.data
if (!data?.response?.data) {
const err = new Error('Unexpected Freteclick response on customer list')
err.data = data
throw err
}
const { id: listedCustomerId } = data.response.data
if (listedCustomerId) {
return {
id
id: listedCustomerId
}
} else if (status === 200 && data && data.response && data.response.data && data.response.count === 0) {
const body = {
"name": ecomUtils.fullName(customer),
"type": customer.registry_type === 'p' ? 'F' : 'J',
"document": customer.doc_number,
"email": customer.main_email,
"address": {
"country": address.country || "Brasil",
"state": address.province_code,
"city": address.city,
"district": address.borough,
"complement": address.complement || "",
"street": address.street,
"number": String(address.number || 0),
"postal_code": String(address.zip.replace(/\D/g, ''))
}
}
const response = await freteClickApi({
url: `/people/customer`,
method: 'post',
token,
data: body
})
const { id } = response && response.data && response.data.response && response.data.response.data
return {
id
}
const body = {
name: ecomUtils.fullName(customer),
type: customer.registry_type === 'p' ? 'F' : 'J',
document: customer.doc_number,
email: customer.main_email,
address: {
country: 'Brasil',
state: address.province_code,
city: address.city,
district: address.borough,
complement: address.complement || '',
street: address.street,
number: String(address.number || 0),
postal_code: String(address.zip.replace(/\D/g, ''))
}
}
const postResponse = await freteClickApi({
url: '/people/customer',
method: 'post',
token,
data: body
})
const { id: newCustomerId } = postResponse?.data?.response?.data || {}
if (!newCustomerId) {
const err = new Error('Unexpected Freteclick response on customer creation')
err.data = postResponse?.data
throw err
}
return {
id: newCustomerId
}
}

0 comments on commit a7751e1

Please sign in to comment.