Allow admins to edit transaction amount and status, recalculate payment totals, and mark invoices fully paid when paid transactions cover the payable amount.
61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
// /components/payments/paymentController.js
|
|
|
|
const catchAsync = require('../../utils/catchAsync');
|
|
const paymentService = require('./paymentService');
|
|
const { successResponse, listResponse } = require('../../utils/apiResponse');
|
|
|
|
exports.create = catchAsync(async (req, res, next) => {
|
|
const actorId = req.user?._id;
|
|
const payment = await paymentService.createPayment(req.body, actorId);
|
|
return successResponse(res, 201, 'Payment created successfully', payment);
|
|
});
|
|
|
|
exports.getOne = catchAsync(async (req, res, next) => {
|
|
const payment = await paymentService.getPaymentById(req.params.id);
|
|
return successResponse(res, 200, 'Payment retrieved successfully', payment);
|
|
});
|
|
|
|
exports.getAll = catchAsync(async (req, res, next) => {
|
|
const { data, meta } = await paymentService.getAllPayments(req.query);
|
|
return listResponse(res, 200, data, meta);
|
|
});
|
|
|
|
exports.update = catchAsync(async (req, res, next) => {
|
|
const actorId = req.user?._id;
|
|
const payment = await paymentService.updatePayment(req.params.id, req.body, actorId);
|
|
return successResponse(res, 200, 'Payment updated successfully', payment);
|
|
});
|
|
|
|
exports.delete = catchAsync(async (req, res, next) => {
|
|
await paymentService.deletePayment(req.params.id);
|
|
return successResponse(res, 200, 'Payment deleted successfully');
|
|
});
|
|
|
|
exports.search = catchAsync(async (req, res, next) => {
|
|
const { data, meta } = await paymentService.searchPayments(req.query);
|
|
return listResponse(res, 200, data, meta);
|
|
});
|
|
|
|
exports.payUser = catchAsync(async (req, res, next) => {
|
|
const actorId = req.user?._id;
|
|
const payment = await paymentService.addTransaction(req.params.id, req.body, actorId);
|
|
return successResponse(res, 200, 'Payment transaction recorded', payment);
|
|
});
|
|
|
|
exports.getMyPayments = catchAsync(async (req, res, next) => {
|
|
const { data, meta } = await paymentService.getMyPayments(req.user._id, req.query);
|
|
return listResponse(res, 200, data, meta);
|
|
});
|
|
|
|
exports.createTransaction = catchAsync(async (req, res, next) => {
|
|
const actorId = req.user?._id;
|
|
const payment = await paymentService.addTransaction(req.params.paymentId, req.body, actorId);
|
|
return successResponse(res, 201, 'Payment transaction recorded', payment);
|
|
});
|
|
|
|
exports.updateTransaction = catchAsync(async (req, res, next) => {
|
|
const actorId = req.user?._id;
|
|
const payment = await paymentService.updateTransaction(req.params.transactionId, req.body, actorId);
|
|
return successResponse(res, 200, 'Transaction updated successfully', payment);
|
|
});
|