owltide/server/api/modify-event.post.ts
Hornwitser 250ca9a1ac Port application from Next.js to Nuxt
Nuxt is based on Vue.js and I find their building blocks to be much
neater compared to the React based Next.js.
2025-03-05 15:36:50 +01:00

37 lines
1.2 KiB
TypeScript

import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "~/server/streams";
import { sendPush } from "~/server/web-push";
export default defineEventHandler(async (event) => {
const formData = await readFormData(event);
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const id = formData.get("id") as string;
const name = formData.get("name") as string;
const description = formData.get("description") as string;
const start = formData.get("start") as string;
const end = formData.get("end") as string;
const location = formData.get("location") as string;
const index = schedule.events.findIndex(event => event.id === id);
if (index === -1) {
throw Error("No such event");
}
const timeChanged = schedule.events[index].slots[0].start !== start + "Z";
schedule.events[index] = {
name,
id,
description,
slots: [
{
id: `${id}-1`,
start: start + "Z",
end: end + "Z",
locations: [location],
}
]
};
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
if (timeChanged)
await sendPush(`New time for ${name}`, `${name} will now start at ${start}`);
});