-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: hook up actions #127
Open
KevinWu098
wants to merge
17
commits into
main
Choose a base branch
from
kwu/hook-up-actions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fb619bd
feat (wip): scaffold
KevinWu098 303fc19
feat: calendar scaffolded
KevinWu098 d267d04
refactor: move files and code around
KevinWu098 6bc836a
fix: selection
KevinWu098 2ea68a2
feat: misc.
KevinWu098 b713c86
feat: dev
KevinWu098 5957a08
feat: setup route
KevinWu098 a9203fd
style: misc.
KevinWu098 28c9092
feat: server action for create
KevinWu098 651ebb0
feat: port group availabilities
KevinWu098 32b87e8
feat: dev
KevinWu098 96f6370
feat: hook up group stuff
KevinWu098 b9cb105
Merge branch 'kwu/calendar' into kwu/hook-things-up
KevinWu098 04d3019
feat: update nav
KevinWu098 fc228d1
feat: save availability action
KevinWu098 b474345
feat: hook up actions
KevinWu098 dd6dcd5
Merge branch 'main' into kwu/hook-up-actions
KevinWu098 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,112 @@ | ||
"use server"; | ||
|
||
import { db } from "@/db"; | ||
import { | ||
availabilities, | ||
AvailabilityInsertSchema, | ||
meetingDates, | ||
MeetingDateSelectSchema, | ||
membersInMeeting, | ||
} from "@/db/schema"; | ||
import { getExistingMeeting } from "@/lib/db/databaseUtils"; | ||
import { ZotDate } from "@/lib/zotdate"; | ||
import { eq, sql } from "drizzle-orm"; | ||
|
||
interface saveAvailabilityProps { | ||
meetingId: string; | ||
availabilityDates: Pick<ZotDate, "day" | "availability">[]; | ||
} | ||
|
||
export async function saveAvailability({ | ||
meetingId, | ||
availabilityDates, | ||
}: saveAvailabilityProps) { | ||
// const user: User | null = locals.user; | ||
|
||
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 = | ||
// user?.id ?? | ||
// ( | ||
// await getExistingGuest( | ||
// formData.get("username") as string, | ||
// await getExistingMeeting(meetingId) | ||
// ) | ||
// ).id; | ||
|
||
const memberId = "123"; | ||
|
||
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 { | ||
status: 200, | ||
body: { | ||
message: "Saved successfully", | ||
}, | ||
}; | ||
} catch (error) { | ||
console.log("Error saving availabilities:", error); | ||
return { | ||
status: 500, | ||
body: { | ||
error: "Failed to save", | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export async function _getMeetingDates( | ||
meetingId: string | ||
): Promise<MeetingDateSelectSchema[]> { | ||
const dbMeeting = await getExistingMeeting(meetingId); | ||
const dbMeetingDates = await db | ||
.select() | ||
.from(meetingDates) | ||
.where(eq(meetingDates.meeting_id, dbMeeting.id)); | ||
|
||
return dbMeetingDates.sort((a, b) => (a.date < b.date ? -1 : 1)); | ||
} |
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,71 @@ | ||
"use server"; | ||
|
||
import { getUserIdFromSession, insertMeeting } from "@/lib/db/databaseUtils"; | ||
import { CreateMeetingPostParams } from "@/lib/types/meetings"; | ||
|
||
export async function createMeeting(newMeeting: CreateMeetingPostParams) { | ||
try { | ||
const { | ||
title, | ||
description, | ||
fromTime, | ||
toTime, | ||
meetingDates, | ||
sessionId, | ||
} = newMeeting; | ||
|
||
console.log( | ||
"Creating meeting:", | ||
title, | ||
description, | ||
fromTime, | ||
toTime, | ||
meetingDates | ||
); | ||
|
||
// Validate the input data | ||
if (fromTime >= toTime) { | ||
return { error: "From time must be before to time" }; | ||
} | ||
|
||
if (!meetingDates || meetingDates.length === 0) { | ||
return { error: "At least one date must be provided" }; | ||
} | ||
|
||
// Limit the number of dates to prevent abuse | ||
if (meetingDates.length > 100) { | ||
return { error: "Too many dates provided" }; | ||
} | ||
|
||
// Sort the dates in UTC order | ||
const sortedDates = meetingDates | ||
.map((dateString) => new Date(dateString)) | ||
.sort((a, b) => a.getTime() - b.getTime()); | ||
|
||
// Retrieve the host ID if the session ID is provided | ||
const host_id = sessionId | ||
? await getUserIdFromSession(sessionId) | ||
: undefined; | ||
|
||
// Prepare the meeting data | ||
const meeting = { | ||
title, | ||
description: description || "", | ||
from_time: fromTime, | ||
to_time: toTime, | ||
host_id, | ||
}; | ||
|
||
// Insert the meeting into the database | ||
const meetingId = await insertMeeting(meeting, sortedDates); | ||
|
||
// Return the created meeting ID | ||
return { meetingId }; | ||
} catch (err) { | ||
const error = err as Error; | ||
console.error("Error creating meeting:", error.message); | ||
|
||
// Return a 500 error response | ||
return { error: `Error creating meeting: ${error.message}` }; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { Creation } from "@/components/creation/creation"; | ||
|
||
export default function Home() { | ||
return <div className="p-4">ZotMeet</div>; | ||
return <Creation />; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this function outside of the server action file, as this will otherwise also be exposed to be callable by the client