cancell request route added

This commit is contained in:
morteza-mortezai
2025-12-01 20:38:32 +03:30
parent 176998476d
commit 8b65385bfe
2 changed files with 28 additions and 1 deletions
+27 -1
View File
@@ -10,7 +10,7 @@ module.exports.createRequest = [
body('card').notEmpty().isObject().withMessage('گارت ورودی خود را انتخاب کنید'), body('card').notEmpty().isObject().withMessage('گارت ورودی خود را انتخاب کنید'),
body('amount').notEmpty().isNumeric().withMessage('مقدار را وارد کنید') body('amount').notEmpty().isNumeric().withMessage('مقدار را وارد کنید')
], ],
checkValidations(validationResult), checkValidations(validationResult),
async (req, res) => { async (req, res) => {
try { try {
const withdraw = await Withdraw.findOne({ userId: req.user_id, status: 0 }) 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) => { module.exports.getAllForUser = async (req, res) => {
const rows = !req.query?.rows || req.query?.rows <= 5 ? 10 : parseInt(req.query.rows) const rows = !req.query?.rows || req.query?.rows <= 5 ? 10 : parseInt(req.query.rows)
+1
View File
@@ -43,6 +43,7 @@ router.delete('/bank-card/:id', bankAccount.deleteCard)
/// ///////////////// Withdraw request /// ///////////////// Withdraw request
router.post('/withdraw', withdraw.createRequest) router.post('/withdraw', withdraw.createRequest)
router.get('/withdraw', withdraw.getAllForUser) router.get('/withdraw', withdraw.getAllForUser)
router.delete('/withdraw', withdraw.cancellRequest)
/// //////////////// Installed device /// //////////////// Installed device
router.post('/register-device', installedDevice.deviceRegistration) router.post('/register-device', installedDevice.deviceRegistration)