57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { db } from "@/lib/db/client"
|
|
import type { ToastRequest } from "@/lib/db/types"
|
|
import type { InteractionSubmissionInput, ToastStatus } from "@/lib/validation/interactions"
|
|
|
|
export interface InvitationInteractions {
|
|
toastRequests: ToastRequest[]
|
|
}
|
|
|
|
export async function getInvitationInteractions(
|
|
invitationId: string,
|
|
): Promise<InvitationInteractions> {
|
|
const toastRequests = await db
|
|
.selectFrom("toast_requests")
|
|
.selectAll()
|
|
.where("invitation_id", "=", invitationId)
|
|
.orderBy("created_at", "asc")
|
|
.execute()
|
|
|
|
return { toastRequests }
|
|
}
|
|
|
|
export async function replaceInvitationInteractions(input: InteractionSubmissionInput) {
|
|
return db.transaction().execute(async (trx) => {
|
|
await trx
|
|
.deleteFrom("toast_requests")
|
|
.where("invitation_id", "=", input.invitationId)
|
|
.execute()
|
|
|
|
if (input.toastRequest) {
|
|
await trx
|
|
.insertInto("toast_requests")
|
|
.values({
|
|
invitation_id: input.invitationId,
|
|
member_id: input.toastRequest.memberId,
|
|
speaker_name: input.toastRequest.speakerName,
|
|
estimated_minutes: input.toastRequest.estimatedMinutes,
|
|
status: "pending",
|
|
})
|
|
.execute()
|
|
}
|
|
|
|
await trx
|
|
.updateTable("invitations")
|
|
.set({ updated_at: new Date() })
|
|
.where("id", "=", input.invitationId)
|
|
.execute()
|
|
})
|
|
}
|
|
|
|
export async function updateToastRequestStatus(id: string, status: ToastStatus) {
|
|
await db
|
|
.updateTable("toast_requests")
|
|
.set({ status, updated_at: new Date() })
|
|
.where("id", "=", id)
|
|
.execute()
|
|
}
|