Skip to content

Commit

Permalink
feat: ✨ create meeting dates w/ meeting, fix retrieval of availabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed May 1, 2024
1 parent 5fc6a5c commit 0da71fd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/lib/components/availability/PersonalAvailability.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import {
availabilityDates,
availabilityTimeBlocks,
generateSampleDates,
guestSession,
isEditingAvailability,
isStateUnsaved,
Expand Down Expand Up @@ -45,6 +44,7 @@
// Triggers on every pagination change and selection confirmation
$: {
const datesToOffset = currentPage * itemsPerPage;
currentPageAvailability = $availabilityDates.slice(datesToOffset, datesToOffset + itemsPerPage);
if (currentPage === lastPage) {
Expand Down Expand Up @@ -157,7 +157,15 @@
$guestSession.meetingId = data.meetingId;
const generalAvailability = await getGeneralAvailability(data, $guestSession);
$availabilityDates = generalAvailability ?? generateSampleDates();
const defaultMeetingDates = data.defaultDates.map((item) => new ZotDate(item.date, false, []));
ZotDate.initializeAvailabilities(defaultMeetingDates);
console.log(generalAvailability, defaultMeetingDates);
$availabilityDates =
generalAvailability && generalAvailability.length > 0
? generalAvailability
: defaultMeetingDates;
});
</script>

Expand Down
35 changes: 32 additions & 3 deletions src/lib/db/databaseUtils.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import type { SuperValidated } from "sveltekit-superforms";
import type { ZodObject, ZodString } from "zod";

import { db } from "./drizzle";
import { members, users, guests, meetings } from "./schema";
import { members, users, guests, meetings, meetingDates } from "./schema";
import type {
UserInsertSchema,
MemberInsertSchema,
MeetingSelectSchema,
GuestInsertSchema,
MeetingInsertSchema,
MeetingDateInsertSchema,
} from "./schema";

import type { AlertMessageType } from "$lib/types/auth";
Expand Down Expand Up @@ -110,6 +111,34 @@ export const getExistingGuest = async (username: string, meeting: MeetingSelectS
*
* @param meeting
*/
export const createMeeting = async (meeting: MeetingInsertSchema) => {
await db.insert(meetings).values(meeting);
export const insertMeeting = async (meeting: MeetingInsertSchema) => {
const [dbMeeting] = await db.insert(meetings).values(meeting).returning();
await insertMeetingDates(dbMeeting);
};

export const getExistingMeeting = async (meetingId: string) => {
const [dbMeeting] = await db.select().from(meetings).where(eq(meetings.id, meetingId));

return dbMeeting;
};

export const insertMeetingDates = async (meeting: MeetingSelectSchema) => {
const currentDate = meeting.from_time;
currentDate.setDate(currentDate.getDate());

for (let i = 0; currentDate <= meeting.to_time; i++) {
const value: MeetingDateInsertSchema = { date: currentDate, meeting_id: meeting.id };
await db.insert(meetingDates).values(value);

currentDate.setDate(currentDate.getDate() + 1);
}
};

export const getExistingMeetingDates = async (meetingId: string) => {
const dbMeetingDates = await db
.select()
.from(meetingDates)
.where(eq(meetingDates.meeting_id, meetingId));

return dbMeetingDates;
};
1 change: 1 addition & 0 deletions src/lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,5 @@ export type GuestInsertSchema = typeof guests.$inferInsert;
export type AvailabilityInsertSchema = typeof availabilities.$inferInsert;
export type MeetingInsertSchema = typeof meetings.$inferInsert;
export type MeetingSelectSchema = typeof meetings.$inferSelect;
export type MeetingDateInsertSchema = typeof meetingDates.$inferInsert;
export type MeetingDateSelectSchema = typeof meetingDates.$inferSelect;
4 changes: 2 additions & 2 deletions src/lib/utils/ZotDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ export class ZotDate {
*/
static initializeAvailabilities(
selectedDates: ZotDate[],
earliestTime: number,
latestTime: number,
earliestTime: number = 480,
latestTime: number = 1050,
blockLength: number = 15,
): void {
const minuteRange = Math.abs(latestTime - earliestTime);
Expand Down
9 changes: 8 additions & 1 deletion src/routes/auth/guest/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { _getMeeting } from "../../availability/[slug]/+page.server";
import { guestSchema } from "$lib/config/zod-schemas";
import {
checkIfGuestUsernameExists,
// insertMeeting,
insertNewGuest,
insertNewMember,
} from "$lib/db/databaseUtils.server";
Expand Down Expand Up @@ -43,8 +44,14 @@ async function createGuest({ request }: { request: Request }) {
await insertNewGuest({
username: form.data.username,
id: id,
meeting_id: "e3cf0163-e172-40c5-955a-ae9fa1090dc2", // TODO replace with actual meeting id
meeting_id: form.data.meetingId,
});

// await insertMeeting({
// title: "test",
// from_time: new Date("2024-01-31T16:00:00.000Z"),
// to_time: new Date("2024-02-06T16:00:00.000Z"),
// });
} catch (error) {
console.error(error);

Expand Down
14 changes: 9 additions & 5 deletions src/routes/availability/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Actions } from "@sveltejs/kit";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import type { User } from "lucia";
import { superValidate } from "sveltekit-superforms/server";

Expand Down Expand Up @@ -27,18 +27,22 @@ export const load: PageServerLoad = (async ({ locals, params }) => {
return {
form: await superValidate(_loginSchema),
guestForm: await superValidate(guestLoginSchema),
availability: user ? await getAvailability(user) : null,
availability: user ? await getAvailability(user, params?.slug) : null,
meetingId: params?.slug as string | undefined,
defaultDates: (await getMeetingDates(params?.slug)) ?? [],
};
}) satisfies PageServerLoad;

const getAvailability = async (user: User) => {
const getAvailability = async (user: User, meetingId: string | undefined) => {
const availability = await db
.select()
.from(availabilities)
.where(eq(availabilities.member_id, user.id));
.innerJoin(meetingDates, eq(availabilities.meeting_day, meetingDates.id))
.where(
and(eq(availabilities.member_id, user.id), eq(meetingDates.meeting_id, meetingId ?? "")),
);

return availability.sort((a, b) => (a.day < b.day ? -1 : 1));
return availability.map((item) => item.availabilities).sort((a, b) => (a.day < b.day ? -1 : 1));
};

export const actions: Actions = {
Expand Down

0 comments on commit 0da71fd

Please sign in to comment.