better export

This commit is contained in:
Dobromir Popov
2025-01-08 10:39:56 +02:00
parent 747bdad3c6
commit 2d039466fa

View File

@ -218,44 +218,100 @@ function PublishersPage({ publishers = [] }: IProps) {
// Add this function to your component
const exportFilteredPublishers = () => {
try {
// Format the data for Excel
const excelData = shownPubs.map(publisher => ({
// Calculate total statistics
const stats = {
totalPublishers: shownPubs.length,
activePublishers: shownPubs.filter(p => p.isActive).length,
trainedPublishers: shownPubs.filter(p => p.isTrained).length,
totalAssignments: shownPubs.reduce((sum, p) => sum + p.assignments.length, 0),
totalAvailabilities: shownPubs.reduce((sum, p) => sum + p.availabilities.length, 0),
subscribedToReminders: shownPubs.filter(p => p.isSubscribedToReminders).length,
subscribedToCoverMe: shownPubs.filter(p => p.isSubscribedToCoverMe).length,
familyHeads: shownPubs.filter(p => p.familyHeadId).length,
avgAssignments: (shownPubs.reduce((sum, p) => sum + p.assignments.length, 0) / shownPubs.length).toFixed(2),
avgAvailabilities: (shownPubs.reduce((sum, p) => sum + p.availabilities.length, 0) / shownPubs.length).toFixed(2)
};
// Format the publishers data for Excel
const publishersData = shownPubs.map(publisher => ({
'First Name': publisher.firstName,
'Last Name': publisher.lastName,
'Email': publisher.email,
'Phone': publisher.phone,
'Phone': publisher.phone || '',
'Active': publisher.isActive ? 'Yes' : 'No',
'Trained': publisher.isTrained ? 'Yes' : 'No',
'Imported': publisher.isImported ? 'Yes' : 'No',
'Role': publisher.role ? 'Yes' : 'No',
'Last Login': publisher.lastLogin ? new Date(publisher.lastLogin).toLocaleDateString() : 'Never',
'Subscribed to Reminders': publisher.isSubscribedToReminders ? 'Yes' : 'No',
'Subscribed to CoverMe': publisher.isSubscribedToCoverMe ? 'Yes' : 'No',
'Family Head ID': publisher.familyHeadId || '',
'Assignments Count': publisher.assignments.length,
'Availabilities Count': publisher.availabilities.length,
'Last Assignment': publisher.assignments.length > 0
? new Date(Math.max(...publisher.assignments.map(a => new Date(a.shift.startTime)))).toLocaleDateString()
: 'Never',
'Number of Assignments': publisher.assignments.length,
'Number of Availabilities': publisher.availabilities.length
}));
// Create worksheet
const ws = utils.json_to_sheet(excelData);
// Create workbook
const wb = utils.book_new();
// Set column widths
// Create publishers worksheet
const ws_data = utils.json_to_sheet(publishersData);
// Set column widths for publishers sheet
const colWidths = [
{ wch: 15 }, // First Name
{ wch: 15 }, // Last Name
{ wch: 30 }, // Email
{ wch: 15 }, // Phone
{ wch: 10 }, // Active
{ wch: 10 }, // Trained
{ wch: 10 }, // Imported
{ wch: 10 }, // Role
{ wch: 15 }, // Last Login
{ wch: 20 }, // Subscribed to Reminders
{ wch: 20 }, // Subscribed to CoverMe
{ wch: 15 }, // Family Head ID
{ wch: 15 }, // Assignments Count
{ wch: 15 }, // Availabilities Count
{ wch: 15 }, // Last Assignment
{ wch: 20 }, // Number of Assignments
{ wch: 20 }, // Number of Availabilities
];
ws['!cols'] = colWidths;
ws_data['!cols'] = colWidths;
// Create workbook and add worksheet
const wb = utils.book_new();
utils.book_append_sheet(wb, ws, 'Publishers');
// Create summary worksheet
const summary_data = [
['Summary Statistics', ''],
['Total Publishers', stats.totalPublishers],
['Active Publishers', stats.activePublishers],
['Trained Publishers', stats.trainedPublishers],
['Total Assignments', stats.totalAssignments],
['Average Assignments per Publisher', stats.avgAssignments],
['Total Availabilities', stats.totalAvailabilities],
['Average Availabilities per Publisher', stats.avgAvailabilities],
['Subscribed to Reminders', stats.subscribedToReminders],
['Subscribed to CoverMe', stats.subscribedToCoverMe],
['Family Heads', stats.familyHeads],
['Export Date', new Date().toLocaleDateString()],
[''],
['Applied Filters:', ''],
['Name Filter', filter || 'None'],
['Zero Shifts Only', showZeroShiftsOnly ? 'Yes' : 'No'],
['No Training', flterNoTraining ? 'Yes' : 'No'],
];
const ws_summary = utils.aoa_to_sheet(summary_data);
// Set column widths for summary sheet
ws_summary['!cols'] = [
{ wch: 30 }, // Label
{ wch: 15 }, // Value
];
// Add sheets to workbook
utils.book_append_sheet(wb, ws_summary, 'Summary');
utils.book_append_sheet(wb, ws_data, 'Publishers');
// Generate buffer
const excelBuffer = write(wb, { bookType: 'xlsx', type: 'array' });
@ -265,9 +321,10 @@ function PublishersPage({ publishers = [] }: IProps) {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
// Generate filename with current date
// Generate filename with current date and filter indication
const date = new Date().toISOString().split('T')[0];
const filename = `publishers-${date}.xlsx`;
const isFiltered = filter || showZeroShiftsOnly || flterNoTraining;
const filename = `publishers-${isFiltered ? 'filtered-' : ''}${date}.xlsx`;
saveAs(blob, filename);
@ -416,6 +473,7 @@ export const getServerSideProps = async (context) => {
isSubscribedToReminders: true,
isSubscribedToCoverMe: true,
familyHeadId: true,
role: true,
assignments: {
select: {
shift: {