Warning Header (Closes #205) #394
|
@ -0,0 +1,104 @@
|
|||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
import { parse } from "date-fns";
|
||||
import matter from "gray-matter";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { DATE_FORMAT, getLocalDateFromEST } from "../../lib/events";
|
||||
|
||||
export const WARNINGS_PATH = path.join("content", "warnings");
|
||||
|
||||
export interface Metadata {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
}
|
||||
|
||||
export interface Warning {
|
||||
content: string;
|
||||
metadata: Metadata;
|
||||
}
|
||||
export const EMPTY_WARNING: Warning = {
|
||||
content: "",
|
||||
metadata: { startDate: new Date(0), endDate: new Date(0) },
|
||||
};
|
||||
|
||||
async function getAllWarningSlugs(): Promise<string[]> {
|
||||
return (
|
||||
await fs.readdir(WARNINGS_PATH, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter((dirent) => dirent.isFile() && dirent.name.endsWith(".txt"))
|
||||
.map((dirent) => dirent.name.slice(0, -".txt".length));
|
||||
}
|
||||
|
||||
async function getWarningBySlug(slug: string): Promise<Warning> {
|
||||
const raw = await fs.readFile(
|
||||
path.join(WARNINGS_PATH, `${slug}.txt`),
|
||||
"utf-8"
|
||||
);
|
||||
const { content, data: metadata } = matter(raw);
|
||||
|
||||
console.log(metadata);
|
||||
return {
|
||||
content: content,
|
||||
metadata: {
|
||||
startDate: getLocalDateFromEST(
|
||||
parse(metadata.startDate, DATE_FORMAT, new Date())
|
||||
),
|
||||
endDate: getLocalDateFromEST(
|
||||
parse(metadata.endDate, DATE_FORMAT, new Date())
|
||||
),
|
||||
} as Metadata,
|
||||
};
|
||||
}
|
||||
async function getAllWarnings(): Promise<Warning[]> {
|
||||
try {
|
||||
const allWarningSlugs = await getAllWarningSlugs();
|
||||
const allWarnings = await Promise.all(
|
||||
allWarningSlugs.map((slug) => getWarningBySlug(slug))
|
||||
);
|
||||
return allWarnings;
|
||||
} catch (error) {
|
||||
console.error('Failed to parse "warning"! \n', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCurrentWarning(): Promise<Warning> {
|
||||
const today = new Date();
|
||||
const allWarnings = await getAllWarnings();
|
||||
|
||||
let currentWarning: Warning | null = null;
|
||||
|
||||
allWarnings.forEach((warning) => {
|
||||
if (
|
||||
warning.metadata.startDate.getTime() < today.getTime() &&
|
||||
warning.metadata.endDate.getTime() > today.getTime()
|
||||
) {
|
||||
if (currentWarning) {
|
||||
console.error(
|
||||
"ERROR: TWO WARNINGS SCHEDULED AT SAME TIME! ",
|
||||
currentWarning,
|
||||
warning
|
||||
);
|
||||
}
|
||||
currentWarning = warning;
|
||||
}
|
||||
});
|
||||
|
||||
if (currentWarning == null) {
|
||||
return EMPTY_WARNING;
|
||||
}
|
||||
|
||||
return currentWarning;
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const currentWarning = await getCurrentWarning();
|
||||
res.status(200).json(currentWarning);
|
||||
}
|
Loading…
Reference in New Issue