From ccb6fd5ad9ab07a751cf6766dfd2c4b5c1e0307d Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Tue, 17 Sep 2024 15:03:47 -0300 Subject: [PATCH] fix: prevent saving "undefined" tracking code on tag creation --- functions/routes/ecom/webhook.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/functions/routes/ecom/webhook.js b/functions/routes/ecom/webhook.js index 6059823..28bb64c 100644 --- a/functions/routes/ecom/webhook.js +++ b/functions/routes/ecom/webhook.js @@ -47,18 +47,21 @@ exports.post = ({ appSdk }, req, res) => { logger.info(`Start creating tag for #${storeId} ${orderId}`) const data = await createTag(order, storeId, appData, appSdk) logger.info(`Tag created for #${storeId} ${orderId}`, { data }) - trackingCodes.push({ - code: String(data['@id'] || data.id), - link: 'https://www.freteclick.com.br/rastreamento', - tag: 'freteclick' - }) - await appSdk.apiRequest( - storeId, - `/orders/${orderId}/shipping_lines/${shippingLine._id}.json`, - 'PATCH', - { tracking_codes: trackingCodes }, - auth - ) + const trackingId = data['@id'] || data.id + if (trackingId) { + trackingCodes.push({ + code: String(trackingId), + link: 'https://www.freteclick.com.br/rastreamento', + tag: 'freteclick' + }) + await appSdk.apiRequest( + storeId, + `/orders/${orderId}/shipping_lines/${shippingLine._id}.json`, + 'PATCH', + { tracking_codes: trackingCodes }, + auth + ) + } } } }