fix excel exports on publishers

This commit is contained in:
Dobromir Popov
2024-12-28 18:27:23 +02:00
parent c290b1a37b
commit 9e5cce3644
4 changed files with 19736 additions and 18 deletions

View File

@ -616,7 +616,7 @@ exports.ReadDocxFileForMonth = async function (filePath, buffer, month, year, pr
};
exports.ExportPublishersToExcel = async function (req, res) {
exports.generatePublishersExcel = async function () {
const prisma = common.getPrismaClient();
const publishers = await prisma.publisher.findMany({
// where: { isActive: true, },
@ -624,9 +624,9 @@ exports.ExportPublishersToExcel = async function (req, res) {
// availabilities: { where: { isActive: true, }, },
// assignments: { include: { shift: true, }, },
congregation: true,
},
});
const ExcelJS = require("exceljs");
const xjswb = new ExcelJS.Workbook();
const sheet = xjswb.addWorksheet("Publishers");
@ -639,10 +639,11 @@ exports.ExportPublishersToExcel = async function (req, res) {
{ header: "Congregation", key: "congregationName", width: 32 },
{ header: "Last Login", key: "lastLogin", width: 32 },
{ header: "Type", key: "PublisherTypeText", width: 32 },
{ header: "Active", key: "isActive", width: 10 },
// { header: "Active", key: "isActive", width: 10 },
{ header: "Created At", key: "createdAt", width: 32 },
{ header: "Updated At", key: "updatedAt", width: 32 },
];
publishers.forEach((publisher) => {
sheet.addRow({
name: publisher.firstName + " " + publisher.lastName,
@ -650,18 +651,17 @@ exports.ExportPublishersToExcel = async function (req, res) {
email: publisher.email,
phone: publisher.phone,
role: publisher.role,
congregationName: publisher.congregation.name,
congregationName: publisher.congregation?.name || 'не е зададен', // Add null check here
lastLogin: publisher.lastLogin,
PublisherTypeText: publisher.PublisherTypeText,
isActive: publisher.isActive,
// isActive: publisher.isActive,
createdAt: publisher.createdAt,
updatedAt: publisher.updatedAt,
});
});
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("Content-Disposition", "attachment; filename=" + encodeURI("Publishers.xlsx"));
xjswb.xlsx.write(res);
// Return a buffer with the Excel data
return await xjswb.xlsx.writeBuffer();
}
const weekNames = [