fix excel exports on publishers
This commit is contained in:
19697
package-lock.json
generated
Normal file
19697
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -12,7 +12,7 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { all } from "axios";
|
||||
import { logger } from "src/helpers/common";
|
||||
import { ExportPublishersToExcel } from "src/helpers/excel";
|
||||
import { generatePublishersExcel } from "src/helpers/excel";
|
||||
|
||||
/**
|
||||
*
|
||||
@ -435,9 +435,16 @@ export default async function handler(req, res) {
|
||||
res.status(200).json(await dataHelper.getAllPublishersWithStatisticsMonth(day, noEndDate));
|
||||
case "exportPublishersExcel":
|
||||
try {
|
||||
await ExportPublishersToExcel(req, res);
|
||||
const today = new Date();
|
||||
const dateStr = today.toISOString().split('T')[0]; // Gets YYYY-MM-DD format
|
||||
|
||||
const excelBuffer = await generatePublishersExcel();
|
||||
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
res.setHeader("Content-Disposition", "attachment; filename=" + encodeURI(`Publishers_${dateStr}.xlsx`));
|
||||
res.send(excelBuffer);
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(error));
|
||||
res.status(500).json({ error: "Failed to generate Excel file" });
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -197,20 +197,34 @@ function PublishersPage({ publishers = [] }: IProps) {
|
||||
|
||||
const exportPublishers = async () => {
|
||||
try {
|
||||
const response = await axiosInstance.get('/api/?action=exportPublishersExcel');
|
||||
const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
||||
const response = await axiosInstance.get('/api/?action=exportPublishersExcel', { responseType: 'arraybuffer' });
|
||||
|
||||
// Get filename from Content-Disposition header
|
||||
const contentDisposition = response.headers['content-disposition'];
|
||||
let filename = 'publishers.xlsx'; // default fallback
|
||||
if (contentDisposition) {
|
||||
const filenameMatch = contentDisposition.match(/filename=(.*?)(;|$)/);
|
||||
if (filenameMatch) {
|
||||
filename = decodeURI(filenameMatch[1]);
|
||||
}
|
||||
}
|
||||
|
||||
const blob = new Blob([response.data], {
|
||||
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||||
});
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'publishers.xlsx';
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url); // Clean up
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(error)); // Log the error
|
||||
console.error(JSON.stringify(error));
|
||||
toast.error("Грешка при експорт на данни");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
|
||||
@ -245,7 +259,7 @@ function PublishersPage({ publishers = [] }: IProps) {
|
||||
</div>
|
||||
{/* export by calling excel helper .ExportPublishersToExcel() */}
|
||||
<div className="flex justify-center m-4">
|
||||
<button className="btn" onClick={exportPublishers}>Export to Excel</button>
|
||||
<button className="button m-2 btn btn-primary" onClick={exportPublishers}>Export to Excel</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -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 = [
|
||||
|
Reference in New Issue
Block a user