From f8152c6db846d8ec6e6d4fea730fbad62250e686 Mon Sep 17 00:00:00 2001 From: gibbyb Date: Tue, 29 Oct 2024 11:24:10 -0500 Subject: [PATCH] fix setCountdown API --- src/server/functions.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/server/functions.ts b/src/server/functions.ts index 50f0657..b2b22a0 100755 --- a/src/server/functions.ts +++ b/src/server/functions.ts @@ -389,10 +389,17 @@ export const setCountdown = async (userId: number, countdown: Countdown) => { const relationship = await getRelationship(userId); if (!relationship) throw new Error("Relationship not found"); const existingCountdown: Countdown | null = await getCountdown(userId); - let result; + + // Ensure the date is a valid Date object + const countdownDate = new Date(countdown.date); + if (isNaN(countdownDate.getTime())) { + throw new Error("Invalid date provided"); + } + + let result; if (existingCountdown !== null) { result = await db.update(schema.countdowns) - .set({ title: countdown.title, date: countdown.date }) + .set({ title: countdown.title, date: countdownDate }) .where(eq(schema.countdowns.id, existingCountdown.id)) .returning(); } else { @@ -400,7 +407,7 @@ export const setCountdown = async (userId: number, countdown: Countdown) => { .values({ relationshipId: relationship.id, title: countdown.title, - date: countdown.date, + date: countdownDate, }).returning(); } return result[0] as Countdown;