Refactored code to use proper types
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Shahan Nedadahandeh 2023-01-21 16:59:27 -05:00
parent 3f6efa99c3
commit 10f43b7eb3
1 changed files with 45 additions and 26 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);
@ -56,13 +66,11 @@ async function getExec(name: string, pos: string) {
metadata: { ...metadata, image } as Metadata,
};
} catch (err) {
const nameParts = name.split("-");
// Capitalize the first letter of the first name and last name
const firstName = capitalize(nameParts[0]);
const lastName = capitalize(nameParts[nameParts.length - 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}`,
@ -78,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";
@ -91,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("", "");
@ -139,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();
}