144 lines
4.9 KiB
JavaScript
144 lines
4.9 KiB
JavaScript
const asyncHandler = require('../../utils/asyncHandler');
|
|
const ApiResponse = require('../../utils/ApiResponse');
|
|
const service = require('./purchase-orders.service');
|
|
const attachmentService = require('./po.attachments.service');
|
|
const path = require('path');
|
|
|
|
const create = asyncHandler(async (req, res) => {
|
|
const data = await service.createPurchaseOrder(req.body, req.user?.id, req.id);
|
|
res.status(201).json(new ApiResponse(201, data, 'Purchase order created successfully'));
|
|
});
|
|
|
|
const list = asyncHandler(async (req, res) => {
|
|
const result = await service.listPurchaseOrders(req.query);
|
|
res.json(new ApiResponse(200, result.data, 'Purchase orders fetched', result.meta));
|
|
});
|
|
|
|
const listPendingApproval = asyncHandler(async (req, res) => {
|
|
const result = await service.listPendingApprovalPurchaseOrders(req.query);
|
|
res.json(
|
|
new ApiResponse(200, result.data, 'Purchase orders pending approval fetched', result.meta)
|
|
);
|
|
});
|
|
|
|
const exportCsv = asyncHandler(async (req, res) => {
|
|
const csv = await service.exportPurchaseOrders(req.query);
|
|
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
|
|
res.setHeader('Content-Disposition', 'attachment; filename="purchase-orders-export.csv"');
|
|
res.send(csv);
|
|
});
|
|
|
|
const getOne = asyncHandler(async (req, res) => {
|
|
const data = await service.getPurchaseOrderById(req.params.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order fetched'));
|
|
});
|
|
|
|
const update = asyncHandler(async (req, res) => {
|
|
const data = await service.updatePurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order updated successfully'));
|
|
});
|
|
|
|
const remove = asyncHandler(async (req, res) => {
|
|
await service.deletePurchaseOrder(req.params.id, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, null, 'Purchase order deleted successfully'));
|
|
});
|
|
|
|
const submit = asyncHandler(async (req, res) => {
|
|
const data = await service.submitPurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order submitted for approval'));
|
|
});
|
|
|
|
const approve = asyncHandler(async (req, res) => {
|
|
const data = await service.approvePurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order approved'));
|
|
});
|
|
|
|
const reject = asyncHandler(async (req, res) => {
|
|
const data = await service.rejectPurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order rejected'));
|
|
});
|
|
|
|
const amend = asyncHandler(async (req, res) => {
|
|
const data = await service.amendPurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.status(201).json(new ApiResponse(201, data, 'Purchase order amendment created'));
|
|
});
|
|
|
|
const cancel = asyncHandler(async (req, res) => {
|
|
const data = await service.cancelPurchaseOrder(req.params.id, req.body, req.user?.id, req.id);
|
|
res.json(new ApiResponse(200, data, 'Purchase order cancelled'));
|
|
});
|
|
|
|
const pdf = asyncHandler(async (req, res) => {
|
|
const { filename, buffer } = await service.getPurchaseOrderPdf(req.params.id);
|
|
res.setHeader('Content-Type', 'application/pdf');
|
|
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
|
|
res.send(buffer);
|
|
});
|
|
|
|
const listAttachments = asyncHandler(async (req, res) => {
|
|
const data = await attachmentService.listPoAttachments(req.params.poId);
|
|
res.json(new ApiResponse(200, data, 'Purchase order attachments fetched'));
|
|
});
|
|
|
|
const uploadAttachment = asyncHandler(async (req, res) => {
|
|
const data = await attachmentService.uploadPoAttachment(
|
|
req.params.poId,
|
|
req.file,
|
|
req.user?.id,
|
|
req.id
|
|
);
|
|
res.status(201).json(new ApiResponse(201, data, 'Purchase order attachment uploaded successfully'));
|
|
});
|
|
|
|
const getAttachment = asyncHandler(async (req, res) => {
|
|
const data = await attachmentService.getPoAttachmentById(
|
|
req.params.poId,
|
|
req.params.attachmentId
|
|
);
|
|
res.json(new ApiResponse(200, data, 'Purchase order attachment fetched'));
|
|
});
|
|
|
|
const downloadAttachment = asyncHandler(async (req, res) => {
|
|
const { attachment, absolutePath } = await attachmentService.downloadPoAttachment(
|
|
req.params.poId,
|
|
req.params.attachmentId
|
|
);
|
|
res.setHeader('Content-Type', attachment.file_type || 'application/octet-stream');
|
|
res.setHeader(
|
|
'Content-Disposition',
|
|
`attachment; filename="${path.basename(attachment.file_name)}"`
|
|
);
|
|
res.sendFile(absolutePath);
|
|
});
|
|
|
|
const removeAttachment = asyncHandler(async (req, res) => {
|
|
await attachmentService.deletePoAttachment(
|
|
req.params.poId,
|
|
req.params.attachmentId,
|
|
req.user?.id,
|
|
req.id
|
|
);
|
|
res.json(new ApiResponse(200, null, 'Purchase order attachment deleted successfully'));
|
|
});
|
|
|
|
module.exports = {
|
|
create,
|
|
list,
|
|
listPendingApproval,
|
|
exportCsv,
|
|
getOne,
|
|
update,
|
|
remove,
|
|
submit,
|
|
approve,
|
|
reject,
|
|
amend,
|
|
cancel,
|
|
pdf,
|
|
listAttachments,
|
|
uploadAttachment,
|
|
getAttachment,
|
|
downloadAttachment,
|
|
removeAttachment,
|
|
};
|