Add debug route to delete the database
All checks were successful
/ build (push) Successful in 1m15s
/ deploy (push) Successful in 16s

To simplify development add a debug route to delete the database content
so that it'll be re-generated to the demo schedule.
This commit is contained in:
Hornwitser 2025-05-31 23:10:25 +02:00
parent 25793fecb8
commit 16191a8dd2
2 changed files with 38 additions and 1 deletions

View file

@ -0,0 +1,20 @@
import { deleteDatbase, readAccounts } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === session.accountId
);
if (!sessionAccount) {
throw Error("Account does not exist");
}
if (sessionAccount.type !== "admin") {
throw createError({
statusCode: 403,
statusMessage: "Forbidden",
});
}
await deleteDatbase();
})

View file

@ -1,4 +1,4 @@
import { readFile, writeFile } from "node:fs/promises";
import { readFile, unlink, writeFile } from "node:fs/promises";
import { Schedule } from "~/shared/types/schedule";
import { Account, Subscription, Session } from "~/shared/types/account";
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
@ -13,6 +13,23 @@ const nextAccountIdPath = "data/next-account-id.json";
const sessionsPath = "data/sessions.json";
const nextSessionIdPath = "data/next-session-id.json";
async function remove(path: string) {
try {
await unlink(path);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
}
export async function deleteDatbase() {
await remove(schedulePath);
await remove(subscriptionsPath);
await remove(accountsPath);
await remove(sessionsPath);
}
async function readJson<T>(filePath: string, fallback: T) {
let data: T extends () => infer R ? R : T;
try {