Skip to content

Commit

Permalink
fix: formatDate to accept many date types
Browse files Browse the repository at this point in the history
  • Loading branch information
mathhulk committed Oct 12, 2024
1 parent c088ce9 commit 0a3bec1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
18 changes: 13 additions & 5 deletions apps/backend/src/modules/class/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down Expand Up @@ -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: [
Expand All @@ -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
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions apps/backend/src/modules/course/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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 ??
Expand Down
11 changes: 5 additions & 6 deletions apps/backend/src/modules/term/formatter.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Expand All @@ -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;
};

0 comments on commit 0a3bec1

Please sign in to comment.