Search last two terms for execs
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Amy 2022-09-02 01:33:32 -04:00
parent de03175e6f
commit 33b5baaff2
3 changed files with 28 additions and 37 deletions

View File

@ -5,7 +5,7 @@ import matter from "gray-matter";
import { Client } from "ldapts";
import { serialize } from "next-mdx-remote/serialize";
import { capitalize, TermYear } from "@/utils";
import { capitalize, getCurrentTermYear, getTermYear, TermYear } from "@/utils";
const EXECS_PATH = path.join("content", "team", "execs");
const FILETYPE = ".md";
@ -32,8 +32,8 @@ export interface Metadata {
image: string;
}
export async function getExecs(termYear: TermYear) {
const execNamePosPairs = await getExecNamePosPairs(termYear);
export async function getExecs() {
const execNamePosPairs = await getExecNamePosPairs();
return await Promise.all(
execNamePosPairs.map((namePosPair) =>
@ -76,16 +76,13 @@ async function getExec(name: string, pos: string) {
}
}
async function getExecNamePosPairs({
term,
year,
}: TermYear): Promise<[person: string, position: string][]> {
async function getExecNamePosPairs(): Promise<
[person: string, position: string][]
> {
if (process.env.USE_LDAP?.toLowerCase() !== "true") {
return [["codey", "mascot"]];
}
console.log(`Fetching exec name-position pairs for ${term} ${year}`);
const url = "ldap://ldap1.csclub.uwaterloo.ca";
const searchDN = "ou=People,dc=csclub,dc=uwaterloo,dc=ca";
const client = new Client({ url });
@ -96,16 +93,31 @@ async function getExecNamePosPairs({
try {
await client.bind("", "");
const terms: TermYear[] = [];
// check members from last two terms (including current term)
for (const termYear of getTermYear(getCurrentTermYear(), {
goBackwards: true,
})) {
if (terms.length >= 2) {
break;
}
terms.push(termYear);
}
const termsFilters = terms
.map(
({ term, year }: TermYear) =>
`(term=${(term as string).slice(0, 1)}${year})`
)
.join("");
const { searchEntries } = await client.search(searchDN, {
scope: "sub",
filter: `(&(objectClass=member)(term=${(term as string).slice(
0,
1
)}${year}))`,
filter: `(&(objectClass=member)(|${termsFilters}))`,
});
console.log(searchEntries);
// 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) {

View File

@ -14,7 +14,6 @@ import {
Metadata as TeamMemberData,
getMemberImagePath,
} from "@/lib/team";
import { getCurrentTermYear, getPreviousTermYear } from "@/utils";
import designData from "../../content/team/design-team.json";
import discordData from "../../content/team/discord-team.json";
@ -155,14 +154,7 @@ function sortTeamMembers(team: Team): Team {
}
export const getStaticProps: GetStaticProps<Props> = async () => {
let execs: SerializedExec[] = [];
try {
execs = await getExecs(getCurrentTermYear());
} catch (e: unknown) {
console.error(e instanceof Error ? e.message : e);
execs = await getExecs(getPreviousTermYear());
}
const execs = await getExecs();
// Note that rawTeams do not contain image paths of members, even though
// TypeScript thinks that it does. It's just to simplify some code.

View File

@ -86,16 +86,3 @@ export function getCurrentTermYear() {
return result.value;
}
export function getPreviousTermYear() {
const result = getTermYear(getCurrentTermYear(), {
goBackwards: true,
skipCurrent: true,
}).next();
if (result.done === true) {
throw new Error("Cannot get previous term. Iterator is done.");
}
return result.value;
}