From 831bdb8faaa7c5396984633b47661cabf1053233 Mon Sep 17 00:00:00 2001 From: Andreas Date: Tue, 14 Jan 2025 03:21:08 +0100 Subject: [PATCH] fix inconsistent no-response --- src/app/api/setupDB/route.ts | 49 ++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/app/api/setupDB/route.ts b/src/app/api/setupDB/route.ts index 40a6fea..4beec25 100644 --- a/src/app/api/setupDB/route.ts +++ b/src/app/api/setupDB/route.ts @@ -24,7 +24,7 @@ import { SqliteQueryInterface, } from "@sequelize/sqlite3"; import { hashpassword } from "@/util/auth"; -import { copyFile, mkdirSync, readFileSync } from "fs"; +import { copyFile, copyFileSync, mkdirSync, readFileSync } from "fs"; import path from "path"; import { UUID } from "crypto"; import { runApiAction } from "@/util/api"; @@ -76,8 +76,8 @@ async function seedPosts(qif: SqliteQueryInterface) { ], }, ]; - projects.map((project) => { - Project.create({ + projects.map(async (project) => { + await Project.create({ name: project.name, readableIdentifier: project.readableIdentifier, }); @@ -90,19 +90,19 @@ async function seedPosts(qif: SqliteQueryInterface) { user_id: post.user_id, }); post.buckets.map(async (bucket) => { - pst.createBucket({ id: bucket.id as UUID }); - bucket.attachments.map((attachment) => { - Attachment.create({ + await pst.createBucket({ id: bucket.id as UUID }); + bucket.attachments.map(async (attachment) => { + await Attachment.create({ bucket_id: bucket.id as UUID, filename: attachment.filename, - }).then((a) => { + }).then(async (a) => { console.log(bucket.id); mkdirSync(`./bucket/${bucket.id}/`, { recursive: true }); - copyFile( + copyFileSync( path.resolve(".", "db", "seed", "posts", a.filename), - path.resolve(".", "bucket", bucket.id, a.filename), - () => {} + path.resolve(".", "bucket", bucket.id, a.filename) ); + return; }); }); }); @@ -114,19 +114,19 @@ async function seedPosts(qif: SqliteQueryInterface) { readableIdentifier: "blog", }, }).then( - (project) => + async (project) => project || - Project.create({ + (await Project.create({ name: "General Blog", readableIdentifier: "blog", - }) + })) ); } async function seedDatabase(qif: SqliteQueryInterface) { await seedUsers(qif); await seedPosts(qif); - + return; // await Post.create({ // title: 'Test Post', // content: ` @@ -147,8 +147,8 @@ async function seedDatabase(qif: SqliteQueryInterface) { // }) } -export async function GET(request: Request) { - runApiAction(async (request) => { +export async function tryGET(request: Request) { + return await runApiAction(async (request) => { await dbSync; const queryInterface = sequelize.queryInterface; @@ -161,19 +161,18 @@ export async function GET(request: Request) { case 1: break; default: - seedDatabase(queryInterface); + await seedDatabase(queryInterface); const postsRows: SqliteColumnsDescription = await queryInterface .describeTable(Post.table.tableName) .then((t) => t); if (!postsRows["project_id"]) - queryInterface.addColumn("Posts", "project_id", { + await queryInterface.addColumn("Posts", "project_id", { type: DataTypes.INTEGER, acceptsNull: () => false, defaultValue: 1, }); break; } - console.log("pp"); return new Response( JSON.stringify({ test: await queryInterface.describeTable("Posts").then((t) => t), @@ -187,3 +186,15 @@ export async function GET(request: Request) { ); }, request); } + +export async function GET(request: Request) { + try { + return await tryGET(request); + } catch (e) { + if (e instanceof APIError) { + return new Response(e.info.responseText, { status: e.info.status }); + } else { + throw e; + } + } +}