generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhook.js
132 lines (128 loc) · 4.25 KB
/
webhook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const logger = require('firebase-functions/logger')
const getAppData = require('./../../lib/store-api/get-app-data')
const createTag = require('../../lib/kangu/create-tag')
const parseStatus = (status) => {
if (status) {
switch (status.toLowerCase()) {
case 'pago':
return 'paid'
case 'em produção':
return 'in_production'
case 'em separação':
return 'in_separation'
case 'pronto para envio':
return 'ready_for_shipping'
case 'nf emitida':
return 'invoice_issued'
case 'enviado':
return 'shipped'
default:
return 'ready_for_shipping'
}
}
return 'ready_for_shipping'
}
exports.post = ({ appSdk }, req, res) => {
// receiving notification from Store API
const { storeId } = req
/**
* Treat E-Com Plus trigger body here
* Ref.: https://developers.e-com.plus/docs/api/#/store/triggers/
*/
const trigger = req.body
const order = trigger.body
const orderId = trigger.resource_id
if (
trigger.resource !== 'orders' || !order ||
(!order.fulfillment_status && !order.financial_status)
) {
res.sendStatus(204)
return
}
appSdk.getAuth(storeId).then(auth => {
return getAppData({ appSdk, storeId, auth }).then(appData => {
const { kangu_token: kanguToken } = appData
if (!kanguToken) return
if (!appData.enable_auto_tag) return
const statusToSend = parseStatus(appData.send_tag_status)
if (
(statusToSend === order.fulfillment_status?.current) ||
(statusToSend === order.financial_status?.current)
) {
logger.info(`Handling webhook to tag ${orderId}`)
return appSdk.apiRequest(storeId, `/orders/${orderId}.json`, 'GET', null, auth)
.then(({ response }) => {
const order = response.data
const shippingLine = order.shipping_lines?.find(({ flags }) => {
return flags?.find((flag) => flag.startsWith('kangu-'))
})
if (!shippingLine) {
return
}
logger.info(`Shipping tag for #${storeId} ${orderId}`)
return createTag({
order,
shippingLine,
kanguToken,
storeId,
appData,
appSdk
})
.then(data => {
const trackingCode = data?.codigo?.replaceAll(' ', '') ||
data?.[0]?.codigo?.replaceAll(' ', '')
if (!trackingCode) {
logger.warn(`Unexpected create tag response for ${orderId}`, { data })
return
}
const trackingCodes = shippingLine.tracking_codes || []
trackingCodes.push({
code: trackingCode,
link: 'https://www.kangu.com.br/rastreio/',
tag: 'kangu'
})
const carrierTrackingCode = data.etiquetas?.[0]?.numeroTransp;
if (carrierTrackingCode) {
trackingCodes.push({
code: carrierTrackingCode,
link: 'https://www.kangu.com.br/rastreio/',
tag: 'kangu_transp'
})
}
logger.info(`Updating ${orderId}`, { data, trackingCodes })
return appSdk.apiRequest(
storeId,
`/orders/${orderId}/shipping_lines/${shippingLine._id}.json`,
'PATCH',
{ tracking_codes: trackingCodes },
auth
)
})
.then(() => {
if (!res.headersSent) res.sendStatus(201)
})
.catch(err => {
logger.error(err)
res.sendStatus(500)
})
})
}
})
})
.then(() => {
if (!res.headersSent) res.sendStatus(200)
})
.catch(err => {
if (err.appWithoutAuth === true) {
const msg = `Webhook for ${storeId} unhandled with no authentication found`
const error = new Error(msg)
error.trigger = JSON.stringify(trigger)
logger.error(error)
res.status(412).send(msg)
} else {
res.status(500)
const { message } = err
res.send({ message })
}
})
}