removed api warning system

This commit is contained in:
Shahan Nedadahandeh 2022-02-15 03:25:44 -05:00
parent df51348643
commit 3d6073ab99
3 changed files with 0 additions and 112 deletions

View File

@ -1,5 +0,0 @@
---
startDate: 'January 29 2022 21:00'
endDate: 'January 30 2022 18:00'
---
Warning: There will be a scheduled system outage on Jan 29, 2022

View File

@ -1,5 +0,0 @@
---
startDate: 'January 30 2022 18:01'
endDate: 'March 30 2022 18:00'
---
Warning: There will be a scheduled system outage on March 25, 2022

View File

@ -1,102 +0,0 @@
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);
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);
}