Skip to content

Commit

Permalink
fix(creation): 🐛 start and end times formats
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhxNguyen7 committed May 20, 2024
1 parent 16299d5 commit f245738
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
16 changes: 2 additions & 14 deletions src/lib/components/creation/Creation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
};
Expand Down
15 changes: 13 additions & 2 deletions src/routes/api/create/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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 {
Expand All @@ -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;
}

0 comments on commit f245738

Please sign in to comment.