forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasketItems.ts
100 lines (89 loc) · 3.94 KB
/
basketItems.ts
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
/*
* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import { type Request, type Response, type NextFunction } from 'express'
import { BasketItemModel } from '../models/basketitem'
import { QuantityModel } from '../models/quantity'
import challengeUtils = require('../lib/challengeUtils')
import * as utils from '../lib/utils'
import { challenges } from '../data/datacache'
const security = require('../lib/insecurity')
interface RequestWithRawBody extends Request {
rawBody: string
}
module.exports.addBasketItem = function addBasketItem () {
return (req: RequestWithRawBody, res: Response, next: NextFunction) => {
const result = utils.parseJsonCustom(req.rawBody)
const productIds = []
const basketIds = []
const quantities = []
for (let i = 0; i < result.length; i++) {
if (result[i].key === 'ProductId') {
productIds.push(result[i].value)
} else if (result[i].key === 'BasketId') {
basketIds.push(result[i].value)
} else if (result[i].key === 'quantity') {
quantities.push(result[i].value)
}
}
const user = security.authenticatedUsers.from(req)
if (user && basketIds[0] && basketIds[0] !== 'undefined' && Number(user.bid) != Number(basketIds[0])) { // eslint-disable-line eqeqeq
res.status(401).send('{\'error\' : \'Invalid BasketId\'}')
} else {
const basketItem = {
ProductId: productIds[productIds.length - 1],
BasketId: basketIds[basketIds.length - 1],
quantity: quantities[quantities.length - 1]
}
challengeUtils.solveIf(challenges.basketManipulateChallenge, () => { return user && basketItem.BasketId && basketItem.BasketId !== 'undefined' && user.bid != basketItem.BasketId }) // eslint-disable-line eqeqeq
const basketItemInstance = BasketItemModel.build(basketItem)
basketItemInstance.save().then((addedBasketItem: BasketItemModel) => {
res.json({ status: 'success', data: addedBasketItem })
}).catch((error: Error) => {
next(error)
})
}
}
}
module.exports.quantityCheckBeforeBasketItemAddition = function quantityCheckBeforeBasketItemAddition () {
return (req: Request, res: Response, next: NextFunction) => {
void quantityCheck(req, res, next, req.body.ProductId, req.body.quantity).catch((error: Error) => {
next(error)
})
}
}
module.exports.quantityCheckBeforeBasketItemUpdate = function quantityCheckBeforeBasketItemUpdate () {
return (req: Request, res: Response, next: NextFunction) => {
BasketItemModel.findOne({ where: { id: req.params.id } }).then((item: BasketItemModel | null) => {
const user = security.authenticatedUsers.from(req)
challengeUtils.solveIf(challenges.basketManipulateChallenge, () => { return user && req.body.BasketId && user.bid != req.body.BasketId }) // eslint-disable-line eqeqeq
if (req.body.quantity) {
if (item == null) {
throw new Error('No such item found!')
}
void quantityCheck(req, res, next, item.ProductId, req.body.quantity)
} else {
next()
}
}).catch((error: Error) => {
next(error)
})
}
}
async function quantityCheck (req: Request, res: Response, next: NextFunction, id: number, quantity: number) {
const product = await QuantityModel.findOne({ where: { ProductId: id } })
if (product == null) {
throw new Error('No such product found!')
}
// is product limited per user and order, except if user is deluxe?
if (!product.limitPerUser || (product.limitPerUser && product.limitPerUser >= quantity) || security.isDeluxe(req)) {
if (product.quantity >= quantity) { // enough in stock?
next()
} else {
res.status(400).json({ error: res.__('We are out of stock! Sorry for the inconvenience.') })
}
} else {
res.status(400).json({ error: res.__('You can order only up to {{quantity}} items of this product.', { quantity: product.limitPerUser.toString() }) })
}
}