www-new/lib/team.ts

155 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

import { readFile, readdir, access } from "fs/promises";
import path from "path";
import matter from "gray-matter";
2022-02-02 02:32:53 -05:00
import { Client } from "ldapts";
import { serialize } from "next-mdx-remote/serialize";
2022-02-02 02:32:53 -05:00
import { getCurrentTerm } from "@/lib/events";
const EXECS_PATH = path.join("content", "team", "execs");
const fileType = ".md";
2022-02-02 02:32:53 -05:00
const { year, term } = getCurrentTerm();
2022-03-04 03:15:15 -05:00
const execPositions: { [name: string]: string } = {
president: "President",
"vice-president": "Vice President",
secretary: "Assistant Vice President",
treasurer: "Treasurer",
sysadmin: "System Administrator",
2022-02-13 10:48:30 -05:00
};
2022-02-13 22:45:30 -05:00
const positionNames: string[] = [
2022-03-04 03:15:15 -05:00
"president",
"vice-president",
"secretary",
"treasurer",
"sysadmin",
2022-02-13 22:45:30 -05:00
];
export interface Metadata {
name: string;
role: string;
image: string;
}
export async function getExecNamePosPairs() {
2022-02-09 08:22:38 -05:00
if (process.env.USE_LDAP?.toLowerCase() !== "true") {
return [["codey", "mascot"]];
2022-02-09 08:22:38 -05:00
}
2022-02-02 02:32:53 -05:00
const url = "ldap://ldap1.csclub.uwaterloo.ca";
const searchDN = "ou=People,dc=csclub,dc=uwaterloo,dc=ca";
const client = new Client({ url });
2022-03-04 03:15:15 -05:00
// position: name
2022-03-04 04:08:52 -05:00
const execMembers: { [name: string]: string } = {};
let formattedExec: [string, string][] = [];
2022-02-09 08:22:38 -05:00
2022-02-02 02:32:53 -05:00
try {
await client.bind("", "");
const { searchEntries } = await client.search(searchDN, {
scope: "sub",
filter: `(&(objectClass=member)(term=${(term as string).slice(
0,
1
2022-02-16 07:29:32 -05:00
)}${year}))`,
2022-02-02 02:32:53 -05:00
});
2022-03-04 03:15:15 -05:00
// 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) {
execMembers[item.position] = item.cn as string;
} else if (item.position instanceof Array) {
2022-03-04 03:15:15 -05:00
item.position.forEach((p) => {
if ((p as string) in execPositions) {
execMembers[p as string] = item.cn as string;
}
});
}
2022-02-16 08:45:37 -05:00
});
2022-02-13 10:48:30 -05:00
formattedExec = positionNames.map((position) => {
return [
`${execMembers[position].split(" ")[0].toLowerCase()}-${execMembers[
position
]
.split(" ")[1]
.toLowerCase()}`,
position,
];
});
2022-02-13 20:33:00 -05:00
formattedExec = [...formattedExec, ["codey", "mascot"]];
2022-02-02 02:32:53 -05:00
} finally {
await client.unbind();
}
2022-02-02 04:06:13 -05:00
return formattedExec;
}
2022-03-09 21:31:57 -05:00
export async function getExec(name: string, pos: string, convert = true) {
2022-02-13 22:45:30 -05:00
let content, metadata;
try {
2022-03-09 21:31:57 -05:00
const raw = await readFile(path.join(EXECS_PATH, `${name}${fileType}`));
2022-02-13 22:45:30 -05:00
({ content, data: metadata } = matter(raw));
2022-03-09 21:42:49 -05:00
const image = await getMemberImagePath(metadata.name);
2022-02-13 22:45:30 -05:00
return {
content: convert ? await serialize(content) : content,
metadata: { ...metadata, image } as Metadata,
};
} catch (err) {
2022-03-09 21:42:49 -05:00
// Capitalize the first letter of the first name and last name
2022-02-13 22:45:30 -05:00
const firstName =
2022-03-09 21:31:57 -05:00
name.split("-")[0][0].toUpperCase() + name.split("-")[0].slice(1);
2022-02-13 22:45:30 -05:00
const lastName =
2022-03-09 21:31:57 -05:00
name.split("-")[1][0].toUpperCase() + name.split("-")[1].slice(1);
2022-02-13 22:45:30 -05:00
2022-03-09 21:31:57 -05:00
const posName = execPositions[pos];
2022-03-09 21:42:49 -05:00
content = "Coming Soon";
metadata = {
name: `${firstName} ${lastName}`,
role: `${posName}`,
};
2022-02-13 22:45:30 -05:00
const image = await getMemberImagePath(metadata.name);
return {
content: convert ? await serialize(content) : content,
metadata: { ...metadata, image } as Metadata,
};
}
}
async function getImage(imgPath: string) {
try {
await access(path.join("public", imgPath));
return imgPath;
} catch {
return undefined;
}
}
export async function getMemberImagePath(name: string) {
const imgPath = path.join("images", "team", name.replace(" ", ""));
const placeholder = path.join(
"images",
"team",
"team-member-placeholder.svg"
);
const img =
(await getImage(imgPath + ".jpg")) ??
(await getImage(imgPath + ".png")) ??
(await getImage(imgPath + ".gif")) ??
(await getImage(imgPath + ".jpeg")) ??
(await getImage(imgPath + ".JPG")) ??
(await getImage(imgPath + ".PNG")) ??
(await getImage(imgPath + ".GIF")) ??
(await getImage(imgPath + ".JPEG")) ??
placeholder;
return img;
}