first commit

This commit is contained in:
Isuru Bimsara
2026-08-14 22:46:02 +05:30
commit 81d0e65686
132 changed files with 24398 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// app/logic/documents/index.js
const registry = require("./registry");
const { generateExcel } = require("./engine/excel.engine");
const {
generatePdfFromPreCost,
generatePdfFromInvoice,
generatePdfFromDispatchNote,
generatePdfFromDeliveryNote,
generatePdfFromPO,
generatePdfFromCustomDocument,
generatePdfFromCreditNote,
} = require("../../utils/pdfGenerator");
const formatMeta = {
pdf: {
ext: "pdf",
mime: "application/pdf",
},
excel: {
ext: "xlsx",
mime:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
},
};
const pdfGenerators = {
precost: generatePdfFromPreCost,
invoice: generatePdfFromInvoice,
dispatchnote: generatePdfFromDispatchNote,
deliverynote: generatePdfFromDeliveryNote,
po: generatePdfFromPO,
customdocument: generatePdfFromCustomDocument,
creditnote: generatePdfFromCreditNote,
};
const generateDocument = async (
document,
format,
data
) => {
document = document.toLowerCase().replace(/[\s_-]+/g, "");
format = format.toLowerCase();
const doc = registry[document];
if (!doc) {
const availableTypes = Object.keys(registry).join(", ");
throw new Error(`Unsupported document type: "${document}". Available types: ${availableTypes}`);
}
let fileBuffer;
if (format === "pdf") {
// Format the data using the template
const formattedData = await doc.pdfTemplate(data);
// Get the correct PDF generator for this document type
const pdfGenerator = pdfGenerators[document];
if (pdfGenerator) {
fileBuffer = await pdfGenerator(formattedData);
} else {
throw new Error(`No PDF generator found for document type: ${document}`);
}
}
else if (format === "excel") {
if (doc.excelBuilder) {
fileBuffer = await generateExcel(
doc.excelBuilder,
data
);
} else {
throw new Error(`Excel generation not supported for document type: ${document}`);
}
}
else {
throw new Error("Unsupported format");
}
const meta = formatMeta[format];
return {
fileBuffer,
fileName: `${document}-${Date.now()}.${meta.ext}`,
mimeType: meta.mime,
};
};
module.exports = {
generateDocument,
};