From 45ad8e9406263df2afc52ec2702a071eec7e3348 Mon Sep 17 00:00:00 2001 From: Gukhee Jo Date: Tue, 20 Aug 2024 20:49:31 +0900 Subject: [PATCH] fix: invitation response create --- .../db/schema/invitation_response.query.ts | 37 ++++++++++++------- src/lib/db/schema/invitation_response.ts | 4 +- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/lib/db/schema/invitation_response.query.ts b/src/lib/db/schema/invitation_response.query.ts index 3f672e1b..3d106d51 100644 --- a/src/lib/db/schema/invitation_response.query.ts +++ b/src/lib/db/schema/invitation_response.query.ts @@ -9,6 +9,11 @@ import { type InvitationResponseInsert, } from "~/lib/db/schema/invitation_response"; +type CreateInvitationResponseParams = Omit< + InvitationResponseInsert, + "id" | "created_at" +>; + export async function getAllInvitationResponses() { return await db.select().from(invitationResponses); } @@ -38,18 +43,24 @@ export async function getInvitationResponseStats() { } export async function createInvitationResponses( - participant_name: string, - attendance: boolean, - reason?: string, -) { - const data: InvitationResponseInsert = { - id: nanoid(), - participant_name: participant_name, - attendance: attendance, - reason: reason, - created_at: new Date(), - }; + params: CreateInvitationResponseParams, +): Promise { + const id = nanoid(); + const currentTimestamp = new Date(); + + try { + const res = await db + .insert(invitationResponses) + .values({ + ...params, + id, + created_at: currentTimestamp, + }) + .returning(); - const res = db.insert(invitationResponses).values(data); - return res; + return res[0]; + } catch (error) { + console.error("Error creating invitatio response:", error); + throw new Error("Could not create invitation response"); + } } diff --git a/src/lib/db/schema/invitation_response.ts b/src/lib/db/schema/invitation_response.ts index 6e5c8faf..7c492956 100644 --- a/src/lib/db/schema/invitation_response.ts +++ b/src/lib/db/schema/invitation_response.ts @@ -3,7 +3,9 @@ import { invitations } from "~/lib/db/schema/invitations"; export const invitationResponses = pgTable("invitation_response", { id: text("id").primaryKey().notNull(), - invitation_id: text("invitation_id").references(() => invitations.id), + invitation_id: text("invitation_id") + .references(() => invitations.id) + .notNull(), participant_name: text("participant_name").notNull(), attendance: boolean("attendance").notNull(), reason: text("reason"),