diff --git a/lib/team.ts b/lib/team.ts index 420563a7..f1ad11e6 100644 --- a/lib/team.ts +++ b/lib/team.ts @@ -5,7 +5,7 @@ import matter from "gray-matter"; import { Client } from "ldapts"; import { serialize } from "next-mdx-remote/serialize"; -import { capitalize, TermYear } from "@/utils"; +import { capitalize, getCurrentTermYear, getTermYear, TermYear } from "@/utils"; const EXECS_PATH = path.join("content", "team", "execs"); const FILETYPE = ".md"; @@ -32,8 +32,8 @@ export interface Metadata { image: string; } -export async function getExecs(termYear: TermYear) { - const execNamePosPairs = await getExecNamePosPairs(termYear); +export async function getExecs() { + const execNamePosPairs = await getExecNamePosPairs(); return await Promise.all( execNamePosPairs.map((namePosPair) => @@ -76,16 +76,13 @@ async function getExec(name: string, pos: string) { } } -async function getExecNamePosPairs({ - term, - year, -}: TermYear): Promise<[person: string, position: string][]> { +async function getExecNamePosPairs(): Promise< + [person: string, position: string][] +> { if (process.env.USE_LDAP?.toLowerCase() !== "true") { return [["codey", "mascot"]]; } - 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"; const client = new Client({ url }); @@ -96,16 +93,31 @@ async function getExecNamePosPairs({ try { await client.bind("", ""); + + const terms: TermYear[] = []; + + // check members from last two terms (including current term) + for (const termYear of getTermYear(getCurrentTermYear(), { + goBackwards: true, + })) { + if (terms.length >= 2) { + break; + } + terms.push(termYear); + } + + const termsFilters = terms + .map( + ({ term, year }: TermYear) => + `(term=${(term as string).slice(0, 1)}${year})` + ) + .join(""); + const { searchEntries } = await client.search(searchDN, { scope: "sub", - filter: `(&(objectClass=member)(term=${(term as string).slice( - 0, - 1 - )}${year}))`, + filter: `(&(objectClass=member)(|${termsFilters}))`, }); - console.log(searchEntries); - // 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) { diff --git a/pages/about/team.tsx b/pages/about/team.tsx index c126fa17..00f7972b 100644 --- a/pages/about/team.tsx +++ b/pages/about/team.tsx @@ -14,7 +14,6 @@ import { Metadata as TeamMemberData, getMemberImagePath, } from "@/lib/team"; -import { getCurrentTermYear, getPreviousTermYear } from "@/utils"; import designData from "../../content/team/design-team.json"; import discordData from "../../content/team/discord-team.json"; @@ -155,14 +154,7 @@ function sortTeamMembers(team: Team): Team { } export const getStaticProps: GetStaticProps = async () => { - let execs: SerializedExec[] = []; - - try { - execs = await getExecs(getCurrentTermYear()); - } catch (e: unknown) { - console.error(e instanceof Error ? e.message : e); - execs = await getExecs(getPreviousTermYear()); - } + const execs = await getExecs(); // 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 27cc4d7c..45a182f2 100644 --- a/utils.ts +++ b/utils.ts @@ -86,16 +86,3 @@ export function getCurrentTermYear() { return result.value; } - -export function getPreviousTermYear() { - const result = getTermYear(getCurrentTermYear(), { - goBackwards: true, - skipCurrent: true, - }).next(); - - if (result.done === true) { - throw new Error("Cannot get previous term. Iterator is done."); - } - - return result.value; -}