is there any possible way I can output the excel file to be like this. (Image below)
In my front side, I have a choice to pick year or month, but currently I’m just focusing in the month.
So what I did is export an array of object wherein it has a key of year, month , day and counts. The counts means is the data is during the day.
export-to-csv/route.ts
const getHistoryDataSchema = z.object({
timeframe: z.enum(['month', 'year']),
month: z.coerce.number().min(0).max(11).default(0),
year: z.coerce.number().min(2000).max(3000)
})
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const timeframe = searchParams.get('timeframe')
const year = searchParams.get('year')
const month = searchParams.get('month')
const queryParams = getHistoryDataSchema.safeParse({
timeframe,
year,
month
})
if (!queryParams.success) {
return Response.json(queryParams.error.message, {
status: 400
})
}
try {
const data = await getZoomReportData(
queryParams.data.timeframe, {
month: queryParams.data.month,
year: queryParams.data.year
}
)
return Response.json(data)
} catch (error) {
return Response.json(error)
}
}
async function getZoomReportData(
timeframe: TimeFrame, period: Period
) {
switch (timeframe) {
case "year":
return getYearZoomData(period.year)
case "month":
return await getMonthZoomData(period.year, period.month)
}
}
interface HistoryData {
year: number;
month: number;
day?: number;
counts?: DepartmentCounts;
}
interface DepartmentCounts {
UNIT_1: number;
UNIT_2: number;
UNIT_3: number;
UNIT_4: number;
UNIT_5: number;
UNIT_6: number;
UNIT_7: number;
UNIT_8: number;
UNIT_9: number;
UNIT_10: number;
UNIT_11: number;
UNIT_12: number;
UNIT_13: number;
UNIT_14: number;
UNIT_15: number;
}
async function getYearZoomData(year: number) {
// Implementation for year data fetching
}
async function getMonthZoomData(year: number, month: number) {
const departments = [
'UNIT_1', 'UNIT_2', 'UNIT_3', 'UNIT_4', 'UNIT_5', 'UNIT_6', 'UNIT_7',
'UNIT_8', 'UNIT_9', 'UNIT_10', 'UNIT_11', 'UNIT_12', 'UNIT_13',
'UNIT_14', 'UNIT_15'
];
const results = await Promise.all(
departments.map(department =>
db.scheduleDate.groupBy({
by: ['day'],
where: {
year,
month,
soft_delete_scheduleDate: false,
appointment: {
soft_delete: false,
department
}
},
orderBy: [{ day: 'asc' }],
_count: { _all: true },
})
)
);
const daysInMonth = new Date(year, month + 1, 0).getDate();
const history: HistoryData[] = Array.from({ length: daysInMonth }, (_, i) => {
const day = i + 1;
const counts: DepartmentCounts = {
UNIT_1: 0,
UNIT_2: 0,
UNIT_3: 0,
UNIT_4: 0,
UNIT_5: 0,
UNIT_6: 0,
UNIT_7: 0,
UNIT_8: 0,
UNIT_9: 0,
UNIT_10: 0,
UNIT_11: 0,
UNIT_12: 0,
UNIT_13: 0,
UNIT_14: 0,
UNIT_15: 0,
};
departments.forEach((department, index) => {
const dayData = results[index].find(item => item.day === day);
counts[department as keyof DepartmentCounts] = dayData ? dayData._count._all : 0;
});
return { year, month, day, counts };
});
return history;
}
And this will be the result of my json file
[
{
"year": 2024,
"month": 7,
"day": 1,
"counts": {
"UNIT_1": 0,
"UNIT_2": 0,
"UNIT_3": 0,
"UNIT_4": 0,
"UNIT_5": 0,
"UNIT_6": 0,
"UNIT_7": 1,
"UNIT_8": 0,
"UNIT_9": 0,
"UNIT_10": 0,
"UNIT_11": 0,
"UNIT_12": 0,
"UNIT_13": 0,
"UNIT_14": 0,
"UNIT_15": 0,
"UNIT_16": 0,
"UNIT_17": 0,
"UNIT_18": 0
}
},
{
"year": 2024,
"month": 7,
"day": 2,
"counts": {
"UNIT_1": 0,
"UNIT_2": 0,
"UNIT_3": 0,
"UNIT_4": 0,
"UNIT_5": 0,
"UNIT_6": 0,
"UNIT_7": 1,
"UNIT_8": 0,
"UNIT_9": 0,
"UNIT_10": 0,
"UNIT_11": 0,
"UNIT_12": 0,
"UNIT_13": 0,
"UNIT_14": 0,
"UNIT_15": 0,
"UNIT_16": 0,
"UNIT_17": 0,
"UNIT_18": 0
}
},
...//rest of the day
]