cancell request route added
This commit is contained in:
@@ -10,7 +10,7 @@ module.exports.createRequest = [
|
||||
body('card').notEmpty().isObject().withMessage('گارت ورودی خود را انتخاب کنید'),
|
||||
body('amount').notEmpty().isNumeric().withMessage('مقدار را وارد کنید')
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const withdraw = await Withdraw.findOne({ userId: req.user_id, status: 0 })
|
||||
@@ -50,6 +50,32 @@ module.exports.createRequest = [
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.cancellRequest = async (req, res) => {
|
||||
try {
|
||||
// Find the latest request with status 0 for the current user
|
||||
const withdraw = await Withdraw.findOne({ userId: req.user_id, status: 0 })
|
||||
.sort({ _id: -1 }) // Get the latest one
|
||||
|
||||
if (!withdraw) {
|
||||
return res.status(404).json({ msg: 'درخواست در حال بررسی یافت نشد' })
|
||||
}
|
||||
|
||||
// Restore the wallet balance
|
||||
const user = await User.findById(req.user_id)
|
||||
if (user) {
|
||||
user.walletBalance += withdraw.amount
|
||||
await user.save()
|
||||
}
|
||||
|
||||
// Delete the request
|
||||
await Withdraw.findByIdAndDelete(withdraw._id)
|
||||
|
||||
res.status(200).json({ msg: 'درخواست با موفقیت لغو شد' })
|
||||
} catch (error) {
|
||||
res.status(500).json({ msg: error.message || error })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.getAllForUser = async (req, res) => {
|
||||
const rows = !req.query?.rows || req.query?.rows <= 5 ? 10 : parseInt(req.query.rows)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user