From 35dc436533bed1878cd8c9ae2de15fc16ebc085b Mon Sep 17 00:00:00 2001 From: Amy Date: Fri, 2 Sep 2022 00:33:01 -0400 Subject: [PATCH] Fetch previous term execs on error --- lib/team.ts | 18 +++++++++--------- pages/about/team.tsx | 11 +++++++++-- utils.ts | 10 ++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/team.ts b/lib/team.ts index b7bd7e7e..ca9b43f7 100644 --- a/lib/team.ts +++ b/lib/team.ts @@ -84,7 +84,7 @@ async function getExecNamePosPairs({ return [["codey", "mascot"]]; } - console.log(`Fetching Exec Name-Position pairs for ${term} ${year}`); + console.log(`Fetching exec name-position pairs for ${term} ${year}`); const url = "ldap://ldap1.csclub.uwaterloo.ca"; const searchDN = "ou=People,dc=csclub,dc=uwaterloo,dc=ca"; @@ -104,6 +104,10 @@ async function getExecNamePosPairs({ )}${year}))`, }); + if (searchEntries.length === 0) { + throw new Error(`No execs found for ${term} ${year}`); + } + // item.position might be an array if the member has more than one position searchEntries.forEach((item) => { if (typeof item.position === "string" && item.position in execPositions) { @@ -118,14 +122,10 @@ async function getExecNamePosPairs({ }); formattedExec = orderedExecPositions.map((position) => { - return [ - `${execMembers[position].split(" ")[0].toLowerCase()}-${execMembers[ - position - ] - .split(" ")[1] - .toLowerCase()}`, - position, - ]; + const fullName = execMembers[position]; + const firstName = fullName.split(" ")[0].toLowerCase(); + const lastName = fullName.split(" ")[1].toLowerCase(); + return [`${firstName}-${lastName}`, position]; }); formattedExec = [...formattedExec, ["codey", "mascot"]]; diff --git a/pages/about/team.tsx b/pages/about/team.tsx index 08895fbb..c126fa17 100644 --- a/pages/about/team.tsx +++ b/pages/about/team.tsx @@ -14,7 +14,7 @@ import { Metadata as TeamMemberData, getMemberImagePath, } from "@/lib/team"; -import { getCurrentTermYear } from "@/utils"; +import { getCurrentTermYear, getPreviousTermYear } from "@/utils"; import designData from "../../content/team/design-team.json"; import discordData from "../../content/team/discord-team.json"; @@ -155,7 +155,14 @@ function sortTeamMembers(team: Team): Team { } export const getStaticProps: GetStaticProps = async () => { - const execs = await getExecs(getCurrentTermYear()); + let execs: SerializedExec[] = []; + + try { + execs = await getExecs(getCurrentTermYear()); + } catch (e: unknown) { + console.error(e instanceof Error ? e.message : e); + execs = await getExecs(getPreviousTermYear()); + } // Note that rawTeams do not contain image paths of members, even though // TypeScript thinks that it does. It's just to simplify some code. diff --git a/utils.ts b/utils.ts index 45a182f2..96e347a6 100644 --- a/utils.ts +++ b/utils.ts @@ -86,3 +86,13 @@ export function getCurrentTermYear() { return result.value; } + +export function getPreviousTermYear() { + const result = getTermYear({ goBackwards: true, skipCurrent: true }).next(); + + if (result.done === true) { + throw new Error("Cannot get previous term. Iterator is done."); + } + + return result.value; +}