Add debug route to delete the database
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:
parent
25793fecb8
commit
16191a8dd2
2 changed files with 38 additions and 1 deletions
20
server/api/admin/delete-database.post.ts
Normal file
20
server/api/admin/delete-database.post.ts
Normal 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();
|
||||||
|
})
|
|
@ -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 { Schedule } from "~/shared/types/schedule";
|
||||||
import { Account, Subscription, Session } from "~/shared/types/account";
|
import { Account, Subscription, Session } from "~/shared/types/account";
|
||||||
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
|
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
|
||||||
|
@ -13,6 +13,23 @@ const nextAccountIdPath = "data/next-account-id.json";
|
||||||
const sessionsPath = "data/sessions.json";
|
const sessionsPath = "data/sessions.json";
|
||||||
const nextSessionIdPath = "data/next-session-id.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) {
|
async function readJson<T>(filePath: string, fallback: T) {
|
||||||
let data: T extends () => infer R ? R : T;
|
let data: T extends () => infer R ? R : T;
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue