generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.js
243 lines (227 loc) · 8.63 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// read configured E-Com Plus app data
const { logger } = require('./../../context')
const getAppData = require('../../lib/store-api/get-app-data')
const { Timestamp, getFirestore } = require('firebase-admin/firestore')
const { nameCollectionEvents } = require('./../../__env')
const checkApiBling = require('../../lib/bling-auth/check-enable-api')
const integrationHandlers = {
exportation: {
product_ids: true,
order_ids: true
},
importation: {
skus: true,
order_numbers: true
}
}
const SKIP_TRIGGER_NAME = 'SkipTrigger'
const ECHO_SUCCESS = 'SUCCESS'
const ECHO_SKIP = 'SKIP'
const ECHO_API_ERROR = 'STORE_API_ERR'
const handlingIds = []
const removeFromQueue = (resourceId) => {
logger.info(handlingIds)
const handlingIndex = handlingIds.indexOf(resourceId)
handlingIds.splice(handlingIndex, 1)
}
exports.post = async ({ appSdk, admin }, req, res) => {
// receiving notification from Store API
const { storeId } = req
/* TODO:
if (req.get('host') && !baseUri.includes(req.get('host'))) {
logger.info('>>> Proxy to function v2')
const axios = require('axios')
try {
const { status, data } = await axios.post(req.url, req.body, {
baseURL: baseUri,
headers: {
'x-store-id': storeId,
'x-operator-token': operatorToken
}
})
logger.info(`>>> Webhook proxy response: ${status} ${data}`)
return res.status(status).send(data)
} catch (error) {
const err = new Error('Error proxying to function v2')
err.config = error.config
err.response = {
status: error.response.status,
data: error.response.data
}
logger.error(err)
}
}
// */
/**
* Treat E-Com Plus trigger body here
* Ref.: https://developers.e-com.plus/docs/api/#/store/triggers/
*/
const trigger = req.body
const resourceId = trigger.resource_id || trigger.inserted_id
// get app configured options
if (!handlingIds.includes(resourceId)) {
handlingIds.push(resourceId)
appSdk.getAuth(storeId)
.then((auth) => {
// eslint-disable-next-line promise/no-nesting
return getAppData({ appSdk, storeId, auth })
.then(async appData => {
if (
Array.isArray(appData.ignore_triggers) &&
appData.ignore_triggers.indexOf(trigger.resource) > -1
) {
// ignore current trigger
const err = new Error()
err.name = SKIP_TRIGGER_NAME
throw err
}
/* DO YOUR CUSTOM STUFF HERE */
const blingClientId = appData.client_id
logger.info(`> Webhook #${storeId} ${resourceId} [${trigger.resource}]`)
if (typeof blingClientId === 'string' && blingClientId) {
let integrationConfig
let canCreateNew = false
if (trigger.resource === 'applications') {
console.log(`appData: ${JSON.stringify(appData)}`)
console.log(`body ${JSON.stringify(trigger)}`)
integrationConfig = appData
canCreateNew = true
} else if (trigger.authentication_id !== auth.myId) {
switch (trigger.resource) {
case 'orders':
canCreateNew = appData.new_orders ? undefined : false
integrationConfig = {
_exportation: {
order_ids: [resourceId]
}
}
break
case 'products':
if (trigger.action === 'create') {
if (!appData.new_products) {
break
}
canCreateNew = true
} else if (
(!trigger.body?.price || !appData.export_price) &&
(!trigger.body?.quantity || !appData.export_quantity)
) {
break
}
integrationConfig = {
_exportation: {
product_ids: [resourceId]
}
}
break
}
}
logger.info(`Integration config ${JSON.stringify(integrationConfig)}`)
if (integrationConfig) {
const isApiBlingOk = await checkApiBling(storeId)
if (!isApiBlingOk) {
logger.warn('> Error in request to api Bling')
return {}
}
// const Bling = require('../bling-auth/client')
const actions = Object.keys(integrationHandlers)
actions.forEach(action => {
for (let i = 1; i <= 3; i++) {
actions.push(`${('_'.repeat(i))}${action}`)
}
})
for (let i = 0; i < actions.length; i++) {
const action = actions[i]
const actionQueues = integrationConfig[action]
if (typeof actionQueues === 'object' && actionQueues) {
for (const queue in actionQueues) {
const ids = actionQueues[queue]
if (Array.isArray(ids) && ids.length) {
const isHiddenQueue = action.charAt(0) === '_'
const mustUpdateAppQueue = trigger.resource === 'applications'
const handlerName = action.replace(/^_+/, '')
const handler = integrationHandlers[handlerName][queue.toLowerCase()]
const nextId = ids[0]
if (
typeof nextId === 'string' &&
nextId.length &&
handler
) {
const debugFlag = `#${storeId} ${action}/${queue}/${nextId}`
const now = Timestamp.now()
const docRef = getFirestore()
.doc(`queue/${storeId}/${nameCollectionEvents}/ecomplus_${nextId}`)
logger.info(`> Send ${debugFlag}`)
const queueEntry = { action, queue, nextId, mustUpdateAppQueue }
const body = {
eventBy: 'ecomplus', // todo: remove?
storeId,
action: handlerName,
queue,
resourceId: nextId,
createdAt: now,
mustUpdateAppQueue, // todo remove?
isHiddenQueue // used in importation
}
if (canCreateNew !== undefined) {
Object.assign(body, { canCreateNew })
}
return docRef.set(body, { merge: true })
.then(() => ({ appData, queueEntry }))
}
}
}
}
}
}
}
// nothing to do
return {}
})
})
.then(({ appData, queueEntry }) => {
removeFromQueue(resourceId)
if (appData) {
const { action, queue } = queueEntry
if (appData[action] && Array.isArray(appData[action][queue])) {
res.status(202)
} else {
res.status(201)
}
return res.send(`> Processed \`${action}.${queue}\``)
} else {
return res.send(ECHO_SUCCESS)
}
})
.catch(err => {
removeFromQueue(resourceId)
if (err.name === SKIP_TRIGGER_NAME) {
// trigger ignored by app configuration
res.send(ECHO_SKIP)
} else {
if (err.response) {
const error = new Error('Webhook process request error')
error.config = JSON.stringify(err.config)
error.response = JSON.stringify({
status: err.response.status,
data: err.response.data
})
logger.error(error)
} else {
logger.error(err)
}
// request to Store API with error response
// return error status code
res.status(500)
const { message } = err
res.send({
error: ECHO_API_ERROR,
message
})
}
})
} else {
logger.info(`# Skipped in execution #${resourceId} [${trigger.resource} - ${trigger.action}]`)
res.status(203).send('Concurrent request with same ResourceId')
}
}