-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorders.js
67 lines (56 loc) · 1.92 KB
/
orders.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
const express = require('express');
const db = require('../db/index');
const requireLogin = require('./middleware');
const router = express.Router();
const checkIdValidity = async (req, res, next) => {
try {
const orderId = req.params.id;
const orderUserId = await db.getOrderUserId(orderId);
if (!orderUserId) {
return res.status(404).send(`An order with the ID '${orderId}' does not exist.`);
} else if (orderUserId !== req.user.id) {
return res.status(401).send(
'Invalid credentials. You cannot view another user\'s order.'
);
}
next();
} catch(err) {
res.status(500).send('Query failed. Please ensure you provided a valid order ID.');
}
};
router.get('', requireLogin, async (req, res) => {
try {
const userId = req.user.id;
const ordersSummary = await db.getOrdersSummary(userId);
res.status(200).json(ordersSummary);
} catch(err) {
res.status(500).send('Orders retrieval failed.');
}
});
router.get('/:id', requireLogin, checkIdValidity, async (req, res) => {
try {
const orderId = req.params.id;
const orderData = await db.getOrderById(orderId);
res.status(200).json(orderData);
} catch(err) {
res.status(500).send('Query failed. Please ensure you provided a valid order ID.');
}
});
router.delete('/:id', requireLogin, checkIdValidity, async (req, res) => {
try {
const orderId = req.params.id;
const orderStatus = await db.getOrderStatus(orderId);
if (orderStatus === 'cancelled') {
return res.status(204).send();
} else if (orderStatus !== 'pending') {
return res.status(400).send(
`Only 'pending' orders can be cancelled; this order's status is '${orderStatus}'.`
);
}
await db.updateOrderStatus(orderId, 'cancelled');
res.status(204).send();
} catch(err) {
res.status(500).send('Query failed. Please ensure you provided a valid order ID.');
}
});
module.exports = router;