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;
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<Exec[]> {
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();
}