From 9db33e6a51ac8a10e31e402e2e708961ff96d9c7 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Tue, 20 Aug 2024 09:17:48 +0100 Subject: [PATCH] Fixed date usage --- src/pages/api/assignments/[id]/[export]/excel.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/pages/api/assignments/[id]/[export]/excel.ts b/src/pages/api/assignments/[id]/[export]/excel.ts index 20661676..633c51e5 100644 --- a/src/pages/api/assignments/[id]/[export]/excel.ts +++ b/src/pages/api/assignments/[id]/[export]/excel.ts @@ -112,6 +112,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const assigneesData = data.assignees .map((assignee: string) => { const userStats = allStats.filter((s: any) => s.user === assignee); + const dates = userStats.map((s: any) => moment(s.date)); return { userId: assignee, user: users.find((u) => u.id === assignee), @@ -126,6 +127,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) { }, { correct: 0, missing: 0, total: 0 } ), + firstDate: moment.min(...dates), + lastDate: moment.max(...dates), stats: userStats, }; }) @@ -135,13 +138,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const highestScore = Math.max(...results); const lowestScore = Math.min(...results); const averageScore = results.reduce((a, b) => a + b, 0) / results.length; - - const dates = assigneesData - .flatMap((r: any) => r.stats) - .map((r: any) => r.date) - .map((d: number) => moment(d)); - const firstDate = moment.min(dates); - const lastDate = moment.max(dates); + const firstDate = moment.min(assigneesData.map((r: any) => r.firstDate)); + const lastDate = moment.max(assigneesData.map((r: any) => r.lastDate)); const firstSectionData = [ {