Skip to content

Commit

Permalink
style: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Sep 14, 2024
1 parent 18607be commit d1fa63a
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 257 deletions.
76 changes: 36 additions & 40 deletions functions/lib/freteclick/create-tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const axios = require('axios')
const ecomUtils = require('@ecomplus/utils')
/* eslint-disable quote-props, quotes */
const { logger } = require('firebase-functions')
const freteClickApi = require('./client')
const getOrCreateCustomer = require('./customer')
const getCompanyId = require('./me')
const client = require('./client')
Expand All @@ -14,16 +12,16 @@ module.exports = async (order, token, storeId, appData, appSdk) => {
companyId
} = await getCompanyId(token)
console.log('people id and company id', peopleId, companyId)
const customer = order.buyers && order.buyers.length && order.buyers[0]
const address = order.shipping_lines && order.shipping_lines.length && order.shipping_lines[0] && order.shipping_lines.length && order.shipping_lines[0].to
const customer = order.buyers?.[0]
const address = order.shipping_lines?.[0]?.to
const retrieve = {
...(order.shipping_lines && order.shipping_lines.length && order.shipping_lines[0] && order.shipping_lines.length && order.shipping_lines[0].from),
...(order.shipping_lines?.[0]?.from),
...appData.from,
zip: appData.zip
}
const freteClickCustom = (order, field) => {
const shippingCustom = order.shipping_lines[0] && order.shipping_lines[0].custom_fields
const customField = shippingCustom.find(custom => custom.field === field);
const customField = shippingCustom.find(custom => custom.field === field)
if (customField !== undefined && customField !== 'false') {
return customField.value
} else {
Expand All @@ -32,46 +30,44 @@ module.exports = async (order, token, storeId, appData, appSdk) => {
}
const quoteId = freteClickCustom(order, 'freteclick_id')
const orderId = freteClickCustom(order, 'freteclick_order_id')
const { id } = await getOrCreateCustomer(token, customer, address)
const { id } = await getOrCreateCustomer(token, customer, address)
console.log('id customer', id)
const data = {
"quote": quoteId,
"price": order.amount && order.amount.freight,
"payer": companyId,
"retrieve": {
"id": 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, ''))
"price": order.amount && order.amount.freight,
"payer": companyId,
"retrieve": {
"id": 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, ''))
},
"contact": peopleId
"contact": peopleId
},
"delivery": {
id,
"address": {
"id": null,
"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, ''))
},
"delivery": {
id,
"address": {
"id": null,
"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, ''))
},
"contact": id
}
"contact": id
}
}



const debugAxiosError = error => {
const err = new Error(error.message)
if (error.response) {
Expand Down
Loading

0 comments on commit d1fa63a

Please sign in to comment.