From f245738dd5d8b3888ca61c4e022475ed7f911212 Mon Sep 17 00:00:00 2001 From: Minh Nguyen <64875104+MinhxNguyen7@users.noreply.github.com> Date: Mon, 20 May 2024 07:11:59 -0700 Subject: [PATCH] =?UTF-8?q?fix(creation):=20=F0=9F=90=9B=20start=20and=20e?= =?UTF-8?q?nd=20times=20formats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/components/creation/Creation.svelte | 16 ++-------------- src/routes/api/create/+server.ts | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/lib/components/creation/Creation.svelte b/src/lib/components/creation/Creation.svelte index 482c5dc4..4a93305f 100644 --- a/src/lib/components/creation/Creation.svelte +++ b/src/lib/components/creation/Creation.svelte @@ -9,23 +9,11 @@ import type { CreateMeetingPostParams } from "$lib/types/meetings"; import { cn } from "$lib/utils/utils"; - /** - * Converts a time string in the format "HH:MM" to an ISO string. - * @param time - */ - function timeStringToIso(time: string): string { - const [hours, minutes] = time.split(":").map(Number); - const date = new Date(); - date.setHours(hours); - date.setMinutes(minutes); - return date.toISOString(); - } - const handleCreation = async () => { const body: CreateMeetingPostParams = { title: $meetingName, - fromTime: timeStringToIso($startTime), - toTime: timeStringToIso($endTime), + fromTime: $startTime, + toTime: $endTime, meetingDates: $selectedDays.map((zotDate) => zotDate.day.toISOString()), description: "", }; diff --git a/src/routes/api/create/+server.ts b/src/routes/api/create/+server.ts index 440fcfc8..b1da7583 100644 --- a/src/routes/api/create/+server.ts +++ b/src/routes/api/create/+server.ts @@ -2,6 +2,7 @@ import { error, json } from "@sveltejs/kit"; import { insertMeeting } from "$lib/db/databaseUtils.server"; import type { MeetingInsertSchema } from "$lib/db/schema"; +import type { HourMinuteString } from "$lib/types/chrono.js"; import type { CreateMeetingPostParams } from "$lib/types/meetings"; export async function POST({ request }) { @@ -27,11 +28,14 @@ export async function POST({ request }) { .map((dateString) => new Date(dateString)) .sort((a, b) => a.getUTCMilliseconds() - b.getUTCMilliseconds()); + const firstDate = sortedDates[0]; + const lastDate = sortedDates[sortedDates.length - 1]; + const meeting: MeetingInsertSchema = { title, description: description || "", - from_time: new Date(fromTime), - to_time: new Date(toTime), + from_time: dateWithHourMinute(firstDate, fromTime), + to_time: dateWithHourMinute(lastDate, toTime), }; try { @@ -42,3 +46,10 @@ export async function POST({ request }) { error(500, "Error creating meeting"); } } + +function dateWithHourMinute(date: Date, time: HourMinuteString) { + const [hour, minute] = time.split(":").map((t) => parseInt(t, 10)); + const newDate = new Date(date); + date.setHours(hour, minute, 0, 0); + return newDate; +}