forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaddress.ts
More file actions
56 lines (51 loc) · 2.08 KB
/
Copy pathaddress.ts
File metadata and controls
56 lines (51 loc) · 2.08 KB
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
/*
* Copyright (c) 2014-2026 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import { type Request, type Response } from 'express'
import { AddressModel } from '../models/address'
import * as utils from '../lib/utils'
export function getAddress () {
return async (req: Request, res: Response) => {
const addresses = await AddressModel.findAll({ where: { UserId: req.body.UserId } })
res.status(200).json({ status: 'success', data: addresses })
}
}
export function getAddressById () {
return async (req: Request, res: Response) => {
const address = await AddressModel.findOne({ where: { id: req.params.id, UserId: req.body.UserId } })
if (address != null) {
res.status(200).json({ status: 'success', data: address })
} else {
res.status(400).json({ status: 'error', data: 'Malicious activity detected.' })
}
}
}
export function updateAddressById () {
return async (req: Request, res: Response) => {
const address = await AddressModel.findOne({ where: { id: req.params.id, UserId: req.body.UserId } })
if (address == null) {
res.status(400).json({ status: 'error', data: 'Malicious activity detected.' })
return
}
const fields = ['fullName', 'mobileNum', 'zipCode', 'streetAddress', 'city', 'state', 'country'] as const
const updateData = Object.fromEntries(fields.filter(field => field in req.body).map(field => [field, req.body[field]]))
try {
await address.update(updateData)
} catch (error: unknown) {
res.status(400).json({ status: 'error', error: utils.getErrorMessage(error) })
return
}
res.status(200).json({ status: 'success', data: address })
}
}
export function delAddressById () {
return async (req: Request, res: Response) => {
const address = await AddressModel.destroy({ where: { id: req.params.id, UserId: req.body.UserId } })
if (address) {
res.status(200).json({ status: 'success', data: 'Address deleted successfully.' })
} else {
res.status(400).json({ status: 'error', data: 'Malicious activity detected.' })
}
}
}