-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhook.js
54 lines (47 loc) · 1.31 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
'use strict'
// read configured E-Com Plus app data
const getConfig = require(process.cwd() + '/lib/store-api/get-config')
const SKIP_TRIGGER_NAME = 'SkipTrigger'
const ECHO_SUCCESS = 'SUCCESS'
const ECHO_SKIP = 'SKIP'
const ECHO_API_ERROR = 'STORE_API_ERR'
module.exports = appSdk => {
return (req, res) => {
const { storeId } = req
/*
Treat E-Com Plus trigger body here
// https://developers.e-com.plus/docs/api/#/store/triggers/
const trigger = req.body
*/
// get app configured options
getConfig({ appSdk, storeId })
.then(configObj => {
/* Do the stuff */
// example only
if (configObj.ignore_trigger_x) {
// ignore current trigger
let err = new Error()
err.name = SKIP_TRIGGER_NAME
throw err
}
// all done
res.send(ECHO_SUCCESS)
})
.catch(err => {
if (err.name === SKIP_TRIGGER_NAME) {
// trigger ignored by app configuration
res.send(ECHO_SKIP)
} else {
// logger.error(err)
// request to Store API with error response
// return error status code
res.status(500)
let { message } = err
res.send({
error: ECHO_API_ERROR,
message
})
}
})
}
}