generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.js
276 lines (261 loc) · 8.83 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// read configured E-Com Plus app data
const getAppData = require('./../../lib/store-api/get-app-data')
const updateAppData = require('./../../lib/store-api/update-app-data')
const Horus = require('../../lib/horus/client')
const requestHorus = require('../../lib/horus/request')
const importProductsToEcom = require('../../lib/integration/imports/products-to-ecom')
// const { getItemHorusAndSendProductToImport } = require('../../lib/integration/imports/utils')
const { saveAndSendExportOrderToHorus } = require('../../lib/integration/exports/utils')
const SKIP_TRIGGER_NAME = 'SkipTrigger'
const ECHO_SUCCESS = 'SUCCESS'
const ECHO_SKIP = 'SKIP'
const ECHO_API_ERROR = 'STORE_API_ERR'
const queueRetry = (appClient, { action, queue, nextId }, appData) => {
const retryKey = `${appClient.storeId}_${action}_${queue}_${nextId}`
console.warn(retryKey)
let queueList = appData[action] && appData[action][queue]
if (!Array.isArray(queueList)) {
queueList = [nextId]
} else if (!queueList.includes(nextId)) {
queueList.unshift(nextId)
}
return new Promise((resolve, reject) => {
setTimeout(() => {
updateAppData(appClient, {
[action]: {
...appData[action],
[queue]: queueList
}
})
.then(resolve)
.catch(reject)
}, 7000)
})
}
const updateApp = async ({ appSdk, storeId, auth }, _id, opts) => {
const {
queueEntry,
appData
} = opts
const { action, queue, nextId } = queueEntry
let queueList = appData[action][queue]
if (Array.isArray(queueList)) {
const idIndex = queueList.indexOf(nextId)
if (idIndex > -1) {
queueList.splice(idIndex, 1)
}
} else {
queueList = []
}
const data = {
[action]: {
...appData[action],
[queue]: queueList
}
}
console.log(`Update App #${storeId} ${JSON.stringify(data)}`)
return updateAppData({ appSdk, storeId, auth }, data)
.then(() => {
return { _id }
})
.catch(async (err) => {
if (err.response && (!err.response.status || err.response.status >= 500)) {
await queueRetry({ appSdk, storeId, auth }, queueEntry, appData, err.response)
return { _id }
} else {
throw err
}
})
}
const getItemByIdHorusAndCreateProduct = async ({ appSdk, storeId, auth }, appData, queueEntry) => {
console.log('>> Import Products: ', JSON.stringify(queueEntry))
// return getItemHorusAndSendProductToImport(storeId, queueEntry.nextId, appData, { queueEntry })
// const getItemHorusAndSendProductToImport = async (storeId, codItem, appData, options) => {
const codItem = queueEntry.nextId
const {
username,
password,
baseURL
} = appData
const appClient = { appSdk, storeId, auth }
const opts = {
appData,
isUpdateDate: false,
queueEntry
}
const endpoint = `/Busca_Acervo?COD_ITEM=${codItem}&offset=0&limit=1`
const horus = new Horus(username, password, baseURL)
const item = await requestHorus(horus, endpoint, 'get')
.catch((err) => {
if (err.response) {
console.warn(JSON.stringify(err.response?.data))
} else {
console.error(err)
}
return null
})
// console.log('>> item', JSON.stringify(item))
if (item && item.length && item[0]) {
const objectHorus = item && item.length && item[0]
return importProductsToEcom(appClient, objectHorus, opts)
.then(response => {
const _id = response?._id || 'not_update'
return updateApp(appClient, _id, opts)
})
.catch(console.error)
// const options = {
// storeId,
// resource: 'products',
// objectHorus: item && item.length && item[0],
// opts: {
// appData,
// isUpdateDate: false,
// queueEntry
// },
// eventName: `${topicResourceToEcom}_events`
// }
// return importToEcom(options, { eventId: Date.now() })
}
return updateApp(appClient, 'item_not_found_horus', opts)
}
const exportOrder = async ({ appSdk, storeId, auth }, appData, queueEntry) => {
console.log('>> Exports Orders: ', JSON.stringify(queueEntry))
return saveAndSendExportOrderToHorus(storeId, queueEntry.nextId, appData, { queueEntry })
.then(() => {
const opts = {
queueEntry,
appData
}
return updateApp({ appSdk, storeId, auth }, queueEntry.nextId, opts)
})
}
const integrationHandlers = {
exportation: {
orders: exportOrder
},
importation: {
products: getItemByIdHorusAndCreateProduct
}
}
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 resourceId = trigger.resource_id || trigger.inserted_id
// get app configured options
appSdk.getAuth(storeId)
.then((auth) => {
return getAppData({ appSdk, storeId, auth })
.then(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
}
console.log(`> Webhook #${storeId} ${resourceId} [${trigger.resource}]`)
let integrationConfig
// const actionsQueue = []
// let canCreateNew = false
if (trigger.resource === 'applications') {
integrationConfig = appData
} else if (trigger.authentication_id !== auth.myId) {
switch (trigger.resource) {
case 'orders':
if (trigger.body) {
// canCreateNew = appData.new_orders ? undefined : false
integrationConfig = {
_exportation: {
orders: [resourceId]
}
}
}
break
default:
break
}
}
if (integrationConfig) {
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]
// console.log('>> ', action, ' ', actionQueues)
if (typeof actionQueues === 'object' && actionQueues) {
let j = 0
const queues = Object.keys(actionQueues)
while (j < queues.length) {
const queue = queues[j]
// Object.keys(actionQueues).forEach((queue) => {
const ids = actionQueues[queue]
const handlerName = action.replace(/^_+/, '')
if (Array.isArray(ids) && ids.length) {
// const isHiddenQueue = action.charAt(0) === '_'
const mustUpdateAppQueue = trigger.resource === 'applications'
const handler = integrationHandlers[handlerName][queue.toLowerCase()]
const nextId = ids[0]
// console.log('>> ', isHiddenQueue, ' ', mustUpdateAppQueue, ' ', handlerName, ' ', nextId, queue.toLowerCase())
const queueEntry = { action, queue, nextId, mustUpdateAppQueue }
return handler({ appSdk, storeId, auth }, appData, queueEntry)
.then(() => ({ appData, action, queue }))
}
j += 1
}
//
}
//
}
//
}
// nothing to do
return {}
})
})
.then(({ appData, action, queue }) => {
// removeFromQueue(resourceId)
if (appData) {
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 => {
if (err.name === SKIP_TRIGGER_NAME) {
// trigger ignored by app configuration
res.send(ECHO_SKIP)
} else if (err.appWithoutAuth === true) {
const msg = `Webhook for ${storeId} unhandled with no authentication found`
const error = new Error(msg)
error.trigger = JSON.stringify(trigger)
console.error(error)
res.status(412).send(msg)
} else {
// console.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
})
}
})
}