Fixed exec name showing middle name (#537)
continuous-integration/drone/push Build is passing Details

Co-authored-by: shahanneda <shahan.neda@gmail.com>
Reviewed-on: #537
Reviewed-by: Mark Chiu <e26chiu@csclub.uwaterloo.ca>
This commit is contained in:
Shahan Nedadahandeh 2023-01-24 20:13:01 -05:00
parent af9246f53f
commit 03f652ab99
1 changed files with 45 additions and 24 deletions

View File

@ -31,22 +31,32 @@ export interface Metadata {
role?: string; role?: string;
image: string; image: string;
} }
interface Person {
export async function getExecs() { firstName: string;
const execNamePosPairs = await getExecNamePosPairs(); lastName: string;
return await Promise.all(
execNamePosPairs.map((namePosPair) =>
getExec(namePosPair[0], namePosPair[1])
)
);
} }
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; let content, metadata;
const expectedFileName =
`${exec.person.firstName}` +
(exec.person.lastName ? `-${exec.person.lastName}` : "");
try { 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)); ({ content, data: metadata } = matter(raw));
const image = await getMemberImagePath(metadata.name as string); const image = await getMemberImagePath(metadata.name as string);
@ -57,10 +67,10 @@ async function getExec(name: string, pos: string) {
}; };
} catch (err) { } catch (err) {
// Capitalize the first letter of the first name and last name // Capitalize the first letter of the first name and last name
const firstName = capitalize(name.split("-")[0]); const firstName = capitalize(exec.person.firstName);
const lastName = capitalize(name.split("-")[1]); const lastName = capitalize(exec.person.lastName);
const posName = execPositions[pos]; const posName = execPositions[exec.position];
content = "Coming Soon!"; content = "Coming Soon!";
metadata = { metadata = {
name: `${firstName} ${lastName}`, name: `${firstName} ${lastName}`,
@ -76,11 +86,14 @@ async function getExec(name: string, pos: string) {
} }
} }
async function getExecNamePosPairs(): Promise< async function getExecNamePosPairs(): Promise<Exec[]> {
[person: string, position: string][] const CodeyExec = {
> { person: { firstName: "codey", lastName: "" },
position: "mascot",
};
if (process.env.USE_LDAP?.toLowerCase() !== "true") { if (process.env.USE_LDAP?.toLowerCase() !== "true") {
return [["codey", "mascot"]]; return [CodeyExec];
} }
const url = "ldap://ldap1.csclub.uwaterloo.ca"; const url = "ldap://ldap1.csclub.uwaterloo.ca";
@ -89,7 +102,8 @@ async function getExecNamePosPairs(): Promise<
// position: name // position: name
const execMembers: { [position: string]: string } = {}; const execMembers: { [position: string]: string } = {};
let formattedExec: [person: string, position: string][] = [];
let formattedExec: Exec[] = [];
try { try {
await client.bind("", ""); await client.bind("", "");
@ -137,13 +151,20 @@ async function getExecNamePosPairs(): Promise<
if (fullName == undefined) { if (fullName == undefined) {
return null; return null;
} }
const firstName = fullName.split(" ")[0].toLowerCase(); const nameParts = fullName.split(" ");
const lastName = fullName.split(" ")[1].toLowerCase(); const firstName = nameParts[0].toLowerCase();
return [`${firstName}-${lastName}`, position]; 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 { } finally {
await client.unbind(); await client.unbind();
} }