-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: guest availability saving (#110)
* fix: π correct type import * feat: β¨ read time start and end from db * fix: π remove guestForm from pageData, correct type * feat: β¨ update data on mount * feat: β¨ read meeting name from db * fix: π correct guest availability saving * fix: π resolve schema issues * fix: π correct data fetching types * chore: π§ remove default guest from store
- Loading branch information
1 parent
d7919d5
commit fb1c5eb
Showing
8 changed files
with
117 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { sql } from "drizzle-orm"; | ||
|
||
import { _getMeetingDates } from "../../../availability/[slug]/+page.server"; | ||
|
||
import { getExistingGuest, getExistingMeeting } from "$lib/db/databaseUtils.server"; | ||
import { db } from "$lib/db/drizzle"; | ||
import { | ||
availabilities, | ||
membersInMeeting, | ||
type AvailabilityInsertSchema, | ||
type MeetingDateSelectSchema, | ||
} from "$lib/db/schema"; | ||
import type { ZotDate } from "$lib/utils/ZotDate"; | ||
|
||
export async function POST({ request }: { request: Request }) { | ||
const formData = await request.formData(); | ||
|
||
const availabilityDates: ZotDate[] = JSON.parse( | ||
(formData.get("availabilityDates") as string) ?? "[]", | ||
); | ||
const meetingId = (formData.get("meetingId") as string) ?? ""; | ||
|
||
let dbMeetingDates: MeetingDateSelectSchema[] = []; | ||
|
||
try { | ||
dbMeetingDates = await _getMeetingDates(meetingId); | ||
} catch (e) { | ||
console.log("Error getting meeting dates:", e); | ||
} | ||
|
||
if (!dbMeetingDates || dbMeetingDates.length === 0) return; | ||
|
||
try { | ||
const memberId = ( | ||
await getExistingGuest( | ||
formData.get("username") as string, | ||
await getExistingMeeting(meetingId), | ||
) | ||
).id; | ||
|
||
const insertDates: AvailabilityInsertSchema[] = availabilityDates.map((date, index) => ({ | ||
day: new Date(date.day).toISOString(), | ||
member_id: memberId, | ||
meeting_day: dbMeetingDates[index].id as string, // Type-cast since id is guaranteed if a meetingDate exists | ||
availability_string: date.availability.map((bool) => (bool ? "1" : "0")).join(""), | ||
})); | ||
|
||
await db.transaction(async (tx) => { | ||
await Promise.all([ | ||
tx | ||
.insert(availabilities) | ||
.values(insertDates) | ||
.onConflictDoUpdate({ | ||
target: [availabilities.member_id, availabilities.meeting_day], | ||
set: { | ||
// `excluded` refers to the row currently in conflict | ||
availability_string: sql.raw(`excluded.availability_string`), | ||
}, | ||
}), | ||
tx | ||
.insert(membersInMeeting) | ||
.values({ memberId, meetingId, attending: "maybe" }) | ||
.onConflictDoNothing(), | ||
]); | ||
}); | ||
|
||
return new Response("Saved guest availability", { | ||
status: 200, | ||
}); | ||
} catch (e) { | ||
console.log("Error saving availabilities:", e); | ||
return new Response("Error saving availabilities", { | ||
status: 500, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters