Skip to content

Commit

Permalink
fix: refactored check orders tracking routine
Browse files Browse the repository at this point in the history
removing ecom webhook in favor of cron send tags with reduced interval
  • Loading branch information
leomp12 committed Jul 6, 2024
1 parent 4cccd3b commit 1580b36
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 514 deletions.
4 changes: 2 additions & 2 deletions functions/ecom.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ const procedures = []

/**
* Uncomment and edit code above to configure `triggers` and receive respective `webhooks`:
*/
const { baseUri } = require('./__env')
procedures.push({
Expand Down Expand Up @@ -519,7 +519,7 @@ procedures.push({
}
]
})
/*
* You may also edit `routes/ecom/webhook.js` to treat notifications properly.
*/

Expand Down
12 changes: 6 additions & 6 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ exports.updateTokens = functions.pubsub.schedule(cron).onRun(() => {
})
console.log(`-- Sheduled update E-Com Plus tokens '${cron}'`)

const cronSendOrders = '*/30 * * * *'
const cronSendOrders = '*/12 * * * *'
const sendWaitingOrders = require('./lib/integration/send-waiting-orders')
exports.sendWaitingOrders = functions.runWith({ timeoutSeconds: 300 })
.pubsub.schedule(cronSendOrders).onRun(sendWaitingOrders)
console.log(`-- Sheduled send tags to Mandae API ${cronSendOrders}`)

const cronTracking = '*/50 * * * *'
const updateTracking = require('./lib/integration/update-tracking')
exports.updateTracking = functions.runWith({ timeoutSeconds: 300 })
.pubsub.schedule(cronTracking).onRun(updateTracking)
console.log(`-- Sheduled send tags to Mandae API ${cronTracking}`)
const cronCheckTracking = '*/47 * * * *'
const checkOrdersTracking = require('./lib/integration/check-orders-tracking')
exports.checkOrdersTracking = functions.runWith({ timeoutSeconds: 300 })
.pubsub.schedule(cronCheckTracking).onRun(checkOrdersTracking)
console.log(`-- Sheduled check tracking from Mandae API ${cronCheckTracking}`)
83 changes: 83 additions & 0 deletions functions/lib/integration/check-orders-tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const { firestore } = require('firebase-admin')
const { setup } = require('@ecomplus/application-sdk')
const logger = require('firebase-functions/logger')
const getAppData = require('../store-api/get-app-data')
const importOrderStatus = require('./import-order-status')

const listStoreIds = () => {
const storeIds = []
const date = new Date()
date.setHours(date.getHours() - 24)
return firestore()
.collection('ecomplus_app_auth')
.where('updated_at', '>', firestore.Timestamp.fromDate(date))
.get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
const storeId = documentSnapshot.get('store_id')
if (storeIds.indexOf(storeId) === -1) {
storeIds.push(storeId)
}
})
return storeIds
})
}

const fetchUndeliveredOrders = async ({ appSdk, storeId }) => {
const auth = await appSdk.getAuth(storeId)
return new Promise((resolve, reject) => {
getAppData({ appSdk, storeId, auth })
.then(async (appData) => {
resolve()
let mandaeTrackingPrefix = appData.__order_settings?.tracking_prefix
if (mandaeTrackingPrefix === undefined) {
mandaeTrackingPrefix = storeId === 1024 ? 'TIA' : ''
}
const mandaeToken = appData.mandae_token
if (mandaeToken) {
const d = new Date()
d.setDate(d.getDate() - 30)
const endpoint = '/orders.json' +
'?fields=_id,number,fulfillment_status,shipping_lines' +
'&shipping_lines.tracking_codes.tag=mandae' +
'&financial_status.current=paid' +
'&fulfillment_status.current!=delivered' +
`&updated_at>=${d.toISOString()}` +
'&sort=number' +
'&limit=200'
try {
const { response } = await appSdk.apiRequest(storeId, endpoint, 'GET')
const orders = response.data.result
for (let i = 0; i < orders.length; i++) {
const order = orders[i]
await importOrderStatus(
{ appSdk, storeId, auth },
{ order, mandaeToken, mandaeTrackingPrefix }
)
}
} catch (_err) {
if (_err.response) {
const err = new Error(`Failed exporting order for #${storeId}`)
logger.error(err, {
request: _err.config,
response: _err.response.data
})
} else {
logger.error(_err)
}
}
}
})
.catch(reject)
})
}

module.exports = context => setup(null, true, firestore())
.then(appSdk => {
return listStoreIds().then(storeIds => {
const runAllStores = fn => storeIds
.sort(() => Math.random() - Math.random())
.map(storeId => fn({ appSdk, storeId }))
return Promise.all(runAllStores(fetchUndeliveredOrders))
})
})
.catch(logger.error)
93 changes: 0 additions & 93 deletions functions/lib/integration/get-old-tag.js

This file was deleted.

28 changes: 0 additions & 28 deletions functions/lib/integration/helpers.js

This file was deleted.

90 changes: 90 additions & 0 deletions functions/lib/integration/import-order-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const logger = require('firebase-functions/logger')
const axios = require('axios')

const parseMandaeStatus = ({ id, name }) => {
switch (id) {
case '1':
return 'delivered'
case '101':
case '110':
case '31':
case '33':
case '118':
case '160':
case '122':
case '123':
case '124':
case '119':
case '120':
case '0':
case '121':
return 'shipped'
}
if (name === 'Encomenda coletada') {
return 'shipped'
}
return null
}

module.exports = async (
{ appSdk, storeId, auth },
{ order, mandaeToken, mandaeTrackingPrefix }
) => {
const { number } = order
const shippingLine = order.shipping_lines?.find(({ app }) => app?.carrier === 'MANDAE')
if (!shippingLine?.to) return
const invoice = shippingLine.invoices?.[0]
if (!invoice?.number || !invoice.serial_number || !invoice.access_key) {
logger.warn(`Skipping #${storeId} ${number} without invoice data`)
return
}
const trackingId = (mandaeTrackingPrefix || '') +
invoice.number.replace(/^0+/, '') +
invoice.serial_number.replace(/^0+/, '')
logger.info(`Tracking #${storeId} ${number} with ID ${trackingId}`)
const { data } = await axios.get(`https://api.mandae.com.br/v3/trackings/${trackingId}`, {
headers: { Authorization: mandaeToken },
timeout: 7000
})
const trackingResult = data?.events?.[0]
if (!trackingResult) return
const status = parseMandaeStatus(trackingResult)
if (!status) {
logger.warn(`No parsed fulfillment status for #${storeId} ${number}`, {
trackingId,
trackingResult
})
return
}
const lineTrackingCodes = shippingLine.tracking_codes || []
if (!lineTrackingCodes.find(({ code }) => code === trackingId)) {
lineTrackingCodes.push({
tag: 'mandae',
code: trackingId,
link: `https://rastreae.com.br/resultado/${trackingId}`
})
await appSdk.apiRequest(
storeId,
`/orders/${order._id}/shipping_lines/${shippingLine._id}.json`,
'PATCH',
{ tracking_codes: lineTrackingCodes },
auth
)
}
if (status !== order.fulfillment_status.current) {
await appSdk.apiRequest(
storeId,
`/orders/${order._id}/fulfillments.json`,
'POST',
{
shipping_line_id: shippingLine._id,
date_time: new Date().toISOString(),
status,
notification_code: `mandae:${trackingResult.id}:${trackingResult.name}`,
flags: ['mandae']
},
auth
)
logger.info(`#${storeId} ${number} updated to ${status}`)
}
}
Loading

0 comments on commit 1580b36

Please sign in to comment.