-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.03 KB
/
index.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
const { json, send } = require('micro')
const { router, post } = require('microrouter')
const cors = require('micro-cors')()
const motlinGateway = require('@moltin/sdk').gateway
const moltin = motlinGateway({
client_id: process.env.MOLTIN_CLIENT_ID,
client_secret: process.env.MOLTIN_CLIENT_SECRET
})
module.exports = cors(
router(
post('/', async (req, res) => {
const {
data: {
tracking_status: {
status: delivery_status,
status_details: delivery_details,
status_date: delivery_date
},
extra: { order_id }
}
} = await json(req)
try {
const { json: { data: order } } = await moltin.Orders.Get(order_id)
if (order.delivery_status === 'DELIVERED') send(res, 400)
await moltin.Orders.Update(order_id, {
id: order_id,
delivery_status,
delivery_details,
delivery_date
})
send(res, 200)
} catch (errors) {
send(res, 500, errors)
}
})
)
)