forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderHistory.js
37 lines (33 loc) · 1.35 KB
/
orderHistory.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
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const insecurity = require('../lib/insecurity')
const db = require('../data/mongodb')
module.exports.orderHistory = function orderHistory () {
return async (req, res, next) => {
const loggedInUser = insecurity.authenticatedUsers.get(req.headers.authorization.replace('Bearer ', ''))
if (loggedInUser && loggedInUser.data && loggedInUser.data.email && loggedInUser.data.id) {
const email = loggedInUser.data.email
const updatedEmail = email.replace(/[aeiou]/gi, '*')
const orders = await db.orders.find({ email: updatedEmail })
res.status(200).json({ status: 'success', data: orders })
} else {
next(new Error('Blocked illegal activity by ' + req.connection.remoteAddress))
}
}
}
module.exports.allOrders = function allOrders () {
return async (req, res, next) => {
const orders = await db.orders.find()
res.status(200).json({ status: 'success', data: orders.reverse() })
}
}
module.exports.toggleDeliveryStatus = function toggleDeliveryStatus () {
return async (req, res, next) => {
const deliveryStatus = !req.body.deliveryStatus
const eta = deliveryStatus ? '0' : '1'
await db.orders.update({ _id: req.params.id }, { $set: { delivered: deliveryStatus, eta: eta } })
res.status(200).json({ status: 'success' })
}
}