forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.ts
34 lines (31 loc) · 1.12 KB
/
wallet.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
/*
* Copyright (c) 2014-2021 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
import models = require('../models/index')
module.exports.getWalletBalance = function getWalletBalance () {
return async (req, res, next) => {
const wallet = await models.Wallet.findOne({ where: { UserId: req.body.UserId } })
if (wallet) {
res.status(200).json({ status: 'success', data: wallet.balance })
} else {
res.status(404).json({ status: 'error' })
}
}
}
module.exports.addWalletBalance = function addWalletBalance () {
return async (req, res, next) => {
const cardId = req.body.paymentId
const card = cardId ? await models.Card.findOne({ where: { id: cardId, UserId: req.body.UserId } }) : null
if (card) {
const wallet = await models.Wallet.increment({ balance: req.body.balance }, { where: { UserId: req.body.UserId } })
if (wallet) {
res.status(200).json({ status: 'success', data: wallet.balance })
} else {
res.status(404).json({ status: 'error' })
}
} else {
res.status(402).json({ status: 'error', message: 'Payment not accepted.' })
}
}
}