From 03f652ab99bda77dc46db448381ebc8f46648feb Mon Sep 17 00:00:00 2001 From: Shahan Nedadahandeh Date: Tue, 24 Jan 2023 20:13:01 -0500 Subject: [PATCH] Fixed exec name showing middle name (#537) Co-authored-by: shahanneda Reviewed-on: https://git.csclub.uwaterloo.ca/www/www-new/pulls/537 Reviewed-by: Mark Chiu --- lib/team.ts | 69 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/lib/team.ts b/lib/team.ts index f1ad11e6..06cef786 100644 --- a/lib/team.ts +++ b/lib/team.ts @@ -31,22 +31,32 @@ export interface Metadata { role?: string; image: string; } - -export async function getExecs() { - const execNamePosPairs = await getExecNamePosPairs(); - - return await Promise.all( - execNamePosPairs.map((namePosPair) => - getExec(namePosPair[0], namePosPair[1]) - ) - ); +interface Person { + firstName: string; + lastName: string; } -async function getExec(name: string, pos: string) { +interface Exec { + person: Person; + position: string; +} + +export async function getExecs() { + const execs = await getExecNamePosPairs(); + + return await Promise.all(execs.map(getExec)); +} + +async function getExec(exec: Exec) { let content, metadata; + const expectedFileName = + `${exec.person.firstName}` + + (exec.person.lastName ? `-${exec.person.lastName}` : ""); try { - const raw = await readFile(path.join(EXECS_PATH, `${name}${FILETYPE}`)); + const raw = await readFile( + path.join(EXECS_PATH, `${expectedFileName}${FILETYPE}`) + ); ({ content, data: metadata } = matter(raw)); const image = await getMemberImagePath(metadata.name as string); @@ -57,10 +67,10 @@ async function getExec(name: string, pos: string) { }; } catch (err) { // Capitalize the first letter of the first name and last name - const firstName = capitalize(name.split("-")[0]); - const lastName = capitalize(name.split("-")[1]); + const firstName = capitalize(exec.person.firstName); + const lastName = capitalize(exec.person.lastName); - const posName = execPositions[pos]; + const posName = execPositions[exec.position]; content = "Coming Soon!"; metadata = { name: `${firstName} ${lastName}`, @@ -76,11 +86,14 @@ async function getExec(name: string, pos: string) { } } -async function getExecNamePosPairs(): Promise< - [person: string, position: string][] -> { +async function getExecNamePosPairs(): Promise { + const CodeyExec = { + person: { firstName: "codey", lastName: "" }, + position: "mascot", + }; + if (process.env.USE_LDAP?.toLowerCase() !== "true") { - return [["codey", "mascot"]]; + return [CodeyExec]; } const url = "ldap://ldap1.csclub.uwaterloo.ca"; @@ -89,7 +102,8 @@ async function getExecNamePosPairs(): Promise< // position: name const execMembers: { [position: string]: string } = {}; - let formattedExec: [person: string, position: string][] = []; + + let formattedExec: Exec[] = []; try { await client.bind("", ""); @@ -137,13 +151,20 @@ async function getExecNamePosPairs(): Promise< if (fullName == undefined) { return null; } - const firstName = fullName.split(" ")[0].toLowerCase(); - const lastName = fullName.split(" ")[1].toLowerCase(); - return [`${firstName}-${lastName}`, position]; + const nameParts = fullName.split(" "); + const firstName = nameParts[0].toLowerCase(); + const lastName = + nameParts.length > 1 + ? nameParts[nameParts.length - 1].toLowerCase() + : ""; + return { + person: { firstName: firstName, lastName: lastName }, + position: position, + }; }) - .filter((pair) => pair != null) as [person: string, position: string][]; + .filter((pair) => pair != null) as Exec[]; - formattedExec = [...formattedExec, ["codey", "mascot"]]; + formattedExec = [...formattedExec, CodeyExec]; } finally { await client.unbind(); }