diff --git a/server/controllers/GPS.Withdraw.js b/server/controllers/GPS.Withdraw.js index eb9bd10..5b20cd4 100644 --- a/server/controllers/GPS.Withdraw.js +++ b/server/controllers/GPS.Withdraw.js @@ -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) diff --git a/server/routes/gps.js b/server/routes/gps.js index 1afcfca..ec1e79c 100644 --- a/server/routes/gps.js +++ b/server/routes/gps.js @@ -43,6 +43,7 @@ router.delete('/bank-card/:id', bankAccount.deleteCard) /// ///////////////// Withdraw request router.post('/withdraw', withdraw.createRequest) router.get('/withdraw', withdraw.getAllForUser) +router.delete('/withdraw', withdraw.cancellRequest) /// //////////////// Installed device router.post('/register-device', installedDevice.deviceRegistration)