feat: add waitlist module, quick payment/transaction edit, and catering fee deduction from professor share
This commit is contained in:
@@ -75,3 +75,14 @@ exports.cancelTransaction = catchAsync(async (req, res, next) => {
|
||||
const payment = await paymentService.cancelTransaction(req.params.transactionId, actorId);
|
||||
return successResponse(res, 200, 'Transaction cancelled successfully', payment);
|
||||
});
|
||||
|
||||
exports.revertTransaction = catchAsync(async (req, res, next) => {
|
||||
const actorId = req.user?._id;
|
||||
const payment = await paymentService.revertTransaction(req.params.transactionId, actorId);
|
||||
return successResponse(res, 200, 'Transaction reverted successfully', payment);
|
||||
});
|
||||
|
||||
exports.deleteTransaction = catchAsync(async (req, res, next) => {
|
||||
const payment = await paymentService.deleteTransaction(req.params.transactionId);
|
||||
return successResponse(res, 200, 'Transaction deleted successfully', payment);
|
||||
});
|
||||
|
||||
@@ -42,9 +42,15 @@ const paymentSchema = new mongoose.Schema({
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['pending', 'partial', 'paid', 'overdue'],
|
||||
enum: ['pending', 'partial', 'paid', 'overdue', 'cancelled', 'reverted'],
|
||||
default: 'pending'
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: ['regular', 'waiting_list'],
|
||||
default: 'regular',
|
||||
index: true
|
||||
},
|
||||
notes: { type: String, trim: true, maxlength: 5000 },
|
||||
isDeleted: {
|
||||
type: Boolean,
|
||||
@@ -72,6 +78,9 @@ paymentSchema.virtual('transactions', {
|
||||
paymentSchema.pre('save', function (next) {
|
||||
this.discount = normalizeDiscount(this.discount, this.amount);
|
||||
const payable = getPayableAmount(this);
|
||||
if (this.status === 'cancelled' || this.status === 'reverted') {
|
||||
return next();
|
||||
}
|
||||
if (this.paidAmount >= payable) {
|
||||
this.status = 'paid';
|
||||
} else if (this.paidAmount > 0) {
|
||||
|
||||
@@ -30,6 +30,8 @@ router.get('/admin/get-one/:id', perm.requires(PERMISSIONS.PAYMENTS_READ), payme
|
||||
router.put('/admin/update/:id', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), validateUpdatePayment, paymentController.update);
|
||||
router.put('/admin/transactions/:transactionId', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), validateUpdateTransaction, paymentController.updateTransaction);
|
||||
router.post('/admin/transactions/:transactionId/cancel', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), paymentController.cancelTransaction);
|
||||
router.post('/admin/transactions/:transactionId/revert', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), paymentController.revertTransaction);
|
||||
router.delete('/admin/transactions/:transactionId', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), paymentController.deleteTransaction);
|
||||
router.post('/admin/transactions/:paymentId', perm.requires(PERMISSIONS.PAYMENTS_UPDATE), validateAddTransaction, paymentController.createTransaction);
|
||||
router.delete('/admin/delete/:id', perm.requires(PERMISSIONS.PAYMENTS_DELETE), paymentController.delete);
|
||||
|
||||
|
||||
@@ -500,6 +500,43 @@ const cancelTransaction = async (transactionId, actorId = null) => {
|
||||
return getPaymentById(payment._id);
|
||||
};
|
||||
|
||||
const revertTransaction = async (transactionId, actorId = null) => {
|
||||
const trx = await Transaction.findById(transactionId);
|
||||
if (!trx) throw new AppError('TRANSACTION_NOT_FOUND');
|
||||
if (trx.status === 'reverted') {
|
||||
throw new AppError('VALIDATION_FAILED', {}, 'این تراکنش قبلاً مسترد شده است.');
|
||||
}
|
||||
|
||||
const payment = await Payment.findById(trx.payment);
|
||||
if (!payment) throw new AppError('PAYMENT_NOT_FOUND');
|
||||
|
||||
const previousStatus = payment.status;
|
||||
trx.status = 'reverted';
|
||||
if (actorId) trx.recordedBy = actorId;
|
||||
await trx.save();
|
||||
|
||||
await refreshPaymentTotals(payment);
|
||||
await emitPaymentStatusChangedIfNeeded(payment, previousStatus, actorId);
|
||||
|
||||
return getPaymentById(payment._id);
|
||||
};
|
||||
|
||||
const deleteTransaction = async (transactionId) => {
|
||||
const trx = await Transaction.findById(transactionId);
|
||||
if (!trx) throw new AppError('TRANSACTION_NOT_FOUND');
|
||||
|
||||
const payment = await Payment.findById(trx.payment);
|
||||
await Transaction.findByIdAndDelete(transactionId);
|
||||
|
||||
if (payment) {
|
||||
const previousStatus = payment.status;
|
||||
await refreshPaymentTotals(payment);
|
||||
await emitPaymentStatusChangedIfNeeded(payment, previousStatus);
|
||||
return getPaymentById(payment._id);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const createBulkClassPayments = async (body, actorId = null) => {
|
||||
const { classId } = body;
|
||||
if (!classId) {
|
||||
@@ -664,6 +701,8 @@ module.exports = {
|
||||
addTransaction,
|
||||
updateTransaction,
|
||||
cancelTransaction,
|
||||
revertTransaction,
|
||||
deleteTransaction,
|
||||
getMyPayments,
|
||||
createTransactionsForPayment,
|
||||
refreshPaymentTotals,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
const AppError = require('../../utils/AppError');
|
||||
|
||||
const PAYMENT_METHODS = new Set(['online', 'card', 'cash']);
|
||||
const TRANSACTION_STATUSES = new Set(['pending', 'paid']);
|
||||
const TRANSACTION_STATUSES = new Set(['pending', 'paid', 'cancelled', 'reverted']);
|
||||
|
||||
const passThrough = (req, res, next) => next();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ const transactionSchema = new mongoose.Schema({
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['pending', 'paid', 'cancelled'],
|
||||
enum: ['pending', 'paid', 'cancelled', 'reverted'],
|
||||
default: 'pending',
|
||||
index: true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user