From a7751e131142cfe12cadbbeb006ac186d85739db Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Mon, 16 Sep 2024 19:57:52 -0300 Subject: [PATCH] fix: better handling new freteclick customer and error debugs --- functions/lib/freteclick/client.js | 3 +- functions/lib/freteclick/create-tag.js | 47 ++++++++++------- functions/lib/freteclick/customer.js | 73 +++++++++++++++----------- 3 files changed, 72 insertions(+), 51 deletions(-) diff --git a/functions/lib/freteclick/client.js b/functions/lib/freteclick/client.js index a0b41a4..d6effc3 100644 --- a/functions/lib/freteclick/client.js +++ b/functions/lib/freteclick/client.js @@ -13,8 +13,9 @@ module.exports = ({ token, data, timeout = 8000 -}) => { +}, axiosConfig = {}) => { const config = { + ...axiosConfig, url, method, headers: { diff --git a/functions/lib/freteclick/create-tag.js b/functions/lib/freteclick/create-tag.js index 1f03038..9a33245 100644 --- a/functions/lib/freteclick/create-tag.js +++ b/functions/lib/freteclick/create-tag.js @@ -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] @@ -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 = { @@ -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, @@ -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 }) diff --git a/functions/lib/freteclick/customer.js b/functions/lib/freteclick/customer.js index 5a7c130..2917315 100644 --- a/functions/lib/freteclick/customer.js +++ b/functions/lib/freteclick/customer.js @@ -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 + } }