Skip to content

Commit

Permalink
feat(appmax): create customer and order router
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis committed Mar 27, 2024
1 parent 47c70b0 commit 1482e59
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
53 changes: 53 additions & 0 deletions functions/lib/appmax/customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const axios = require('axios')

const createOrUpdateCustomer = async (buyer, to, items, browser_ip, utm, token) => {
const { email, fullname } = buyer
const splitedName = fullname.split(' ')
const products = []
items.forEach(({sku, quantity}) => {
if (quantity > 0) {
products.push({
product_sku: sku,
product_qty: quantity
})
}
});
const body = {
"access-token": token,
"firstname": splitedName[0],
"lastname": splitedName[splitedName.length - 1],
email,
"telephone": buyer.phone.number,
"postcode": to.zip.replace(/\D/g, '').padStart(8, '0'),
"address_street": to.street,
"address_street_number": to.number ? String(to.number) : 'SN',
"address_street_complement": to.complement,
"address_street_district": to.borough,
"address_city": to.city,
"address_state": to.province || to.province_code,
"ip": browser_ip,
"custom_txt": items[0].name,
products,
"tracking": {
"utm_source": utm.source,
"utm_campaign": utm.campaign,
"utm_medium": utm.medium,
"utm_content": utm.content,
"utm_term": utm.content
}
}

const { data } = await axios({
url: 'https://homolog.sandboxappmax.com.br/api/v3/customer',
method: 'post',
data: body
})
console.log('created customer', JSON.stringify(data))

if (data) {
return data.customer_id
}
return null
}

module.exports = createOrUpdateCustomer
38 changes: 38 additions & 0 deletions functions/lib/appmax/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const axios = require('axios')

const createOrder = async (items, amount, customer_id, token) => {
const { total, discount, freight } = amount
const products = []
items.forEach(({sku, quantity, name, price, final_price}) => {
if (quantity > 0) {
products.push({
sku,
name,
qty: quantity,
price: final_price || price
})
}
});
const body = {
"access-token": token,
total,
products,
"shipping": freight,
customer_id,
discount
}

const { data } = await axios({
url: 'https://homolog.sandboxappmax.com.br/api/v3/order',
method: 'post',
data: body
})
console.log('created order', JSON.stringify(data))

if (data) {
return data.order_id
}
return null
}

module.exports = createOrder

0 comments on commit 1482e59

Please sign in to comment.