fix setCountdown API

This commit is contained in:
Gabriel Brown 2024-10-29 11:24:10 -05:00
parent 3d09199702
commit f8152c6db8

View File

@ -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;