From 0a3bec1915f2b1cb80e49b8ff304064b38598bb1 Mon Sep 17 00:00:00 2001 From: Matthew Rowland Date: Sat, 12 Oct 2024 16:20:23 -0700 Subject: [PATCH] fix: formatDate to accept many date types --- apps/backend/src/modules/class/formatter.ts | 18 +++++++++++++----- apps/backend/src/modules/course/formatter.ts | 5 +++-- apps/backend/src/modules/term/formatter.ts | 11 +++++------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/backend/src/modules/class/formatter.ts b/apps/backend/src/modules/class/formatter.ts index f944d8043..9dbd31a8c 100644 --- a/apps/backend/src/modules/class/formatter.ts +++ b/apps/backend/src/modules/class/formatter.ts @@ -17,6 +17,14 @@ export type IntermediateClass = Omit< sections: null; }; +export const formatDate = (date?: string | number | Date | null) => { + if (!date) return date; + + if (date instanceof Date) return date.toISOString(); + + return new Date(date).toISOString(); +}; + export const formatClass = (_class: ClassType) => { const [year, semester] = _class.session?.term?.name?.split(" ") as string[]; @@ -76,8 +84,8 @@ export const formatSection = (section: SectionType) => { component: section.component?.code as string, - endDate: section?.endDate?.toISOString() as string, - startDate: section?.startDate?.toISOString() as string, + endDate: formatDate(section?.endDate), + startDate: formatDate(section?.startDate), meetings: section.meetings?.map((m) => ({ days: [ @@ -90,10 +98,10 @@ export const formatSection = (section: SectionType) => { m.meetsSaturday, ], - endDate: m.endDate?.toISOString() as string, + endDate: formatDate(m.endDate), endTime: m.endTime, location: m.location?.description, - startDate: m.startDate?.toISOString() as string, + startDate: formatDate(m.startDate), startTime: m.startTime, instructors: m?.assignedInstructors @@ -117,7 +125,7 @@ export const formatSection = (section: SectionType) => { })), exams: section.exams?.map((e) => ({ - date: e.date?.toISOString() as string, + date: formatDate(e.date), endTime: e.endTime, location: e.location?.description as string, startTime: e.startTime, diff --git a/apps/backend/src/modules/course/formatter.ts b/apps/backend/src/modules/course/formatter.ts index 5afc64af8..ca9737877 100644 --- a/apps/backend/src/modules/course/formatter.ts +++ b/apps/backend/src/modules/course/formatter.ts @@ -6,6 +6,7 @@ import { CourseGradingBasis, InstructionMethod, } from "../../generated-types/graphql"; +import { formatDate } from "../class/formatter"; import { CourseModule } from "./generated-types/module-types"; export type IntermediateCourse = Omit< @@ -38,12 +39,12 @@ export function formatCourse(course: CourseType) { description: course.description as string, primaryInstructionMethod: course.primaryInstructionMethod ?.code as InstructionMethod, - fromDate: course.fromDate as string, + fromDate: formatDate(course.fromDate), finalExam: course.finalExam?.description as CourseFinalExam, gradingBasis: course.gradingBasis?.description as CourseGradingBasis, academicCareer: course.academicCareer?.code as AcademicCareer, title: course.title as string, - toDate: course.toDate?.toISOString?.() as string, + toDate: formatDate(course.toDate), typicallyOffered: // @ts-expect-error - The model was typed incorrectly course.formatsOffered?.typicallyOffered?.terms?.termNames ?? diff --git a/apps/backend/src/modules/term/formatter.ts b/apps/backend/src/modules/term/formatter.ts index aa04ea5d1..7f61ba6bb 100644 --- a/apps/backend/src/modules/term/formatter.ts +++ b/apps/backend/src/modules/term/formatter.ts @@ -1,16 +1,15 @@ import { SessionType, TermType } from "@repo/common"; +import { formatDate } from "../class/formatter"; import { TermModule } from "./generated-types/module-types"; -export const formatDate = (date?: Date | null) => date?.toISOString(); - export const formatSession = (session: SessionType) => { const { name, beginDate, endDate, temporalPosition } = session; return { name, - startDate: beginDate?.toISOString(), - endDate: endDate?.toISOString(), + startDate: formatDate(beginDate), + endDate: formatDate(endDate), temporalPosition: temporalPosition as TermModule.TemporalPosition, } as TermModule.Session; }; @@ -25,7 +24,7 @@ export const formatTerm = (term: TermType) => { year: parseInt(year), temporalPosition: temporalPosition as TermModule.TemporalPosition, sessions: sessions.map(formatSession), - startDate: beginDate?.toISOString(), - endDate: endDate?.toISOString(), + startDate: formatDate(beginDate), + endDate: formatDate(endDate), } as TermModule.Term; };