-
Notifications
You must be signed in to change notification settings - Fork 3
K1-19 Endpoints and Schema for Events #13
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,72 @@ | ||
| import connectDB from "@/database/db"; | ||
| import Event from "@/database/eventSchema"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import mongoose from "mongoose"; | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| try { | ||
| await connectDB(); | ||
|
|
||
| const { searchParams } = new URL(request.url); | ||
| const anonUserId = searchParams.get("anonUserId"); | ||
| const event = searchParams.get("event"); | ||
| const sessionId = searchParams.get("sessionId"); | ||
|
|
||
| const filter: Record<string, any> = {}; | ||
|
|
||
| if (anonUserId) filter.anonUserId = anonUserId; | ||
| if (event) filter.event = event; | ||
| if (sessionId) { | ||
| if (!mongoose.Types.ObjectId.isValid(sessionId)) { | ||
| return NextResponse.json({ error: "Invalid sessionId" }, { status: 400 }); | ||
| } | ||
| filter.sessionId = new mongoose.Types.ObjectId(sessionId); | ||
| } | ||
|
|
||
| const events = await Event.find(filter).sort({ ts: -1 }); | ||
|
|
||
| return NextResponse.json(events); | ||
| } catch (error) { | ||
| console.error("GET /api/events error:", error); | ||
| return NextResponse.json({ error: "Failed to fetch events" }, { status: 500 }); | ||
| } | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| await connectDB(); | ||
|
|
||
| const body = await request.json(); | ||
| const { eventId, ts, anonUserId, sessionId, event, props } = body; | ||
|
|
||
| if (!eventId || !anonUserId || !sessionId || !event) { | ||
| return NextResponse.json( | ||
| { error: "Missing required fields: eventId, anonUserId, sessionId, event" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| if (!mongoose.Types.ObjectId.isValid(sessionId)) { | ||
| return NextResponse.json({ error: "Invalid sessionId" }, { status: 400 }); | ||
| } | ||
|
|
||
| const newEvent = new Event({ | ||
| eventId, | ||
| ts: ts ? new Date(ts) : new Date(), | ||
| anonUserId, | ||
| sessionId: new mongoose.Types.ObjectId(sessionId), | ||
| event, | ||
| props: props ?? {}, | ||
| }); | ||
|
|
||
| const saved = await newEvent.save(); | ||
|
|
||
| return NextResponse.json(saved, { status: 201 }); | ||
| } catch (error: any) { | ||
| if (error.code === 11000) { | ||
| return NextResponse.json({ error: "an event with this eventId already exists" }, { status: 409 }); | ||
| } | ||
| console.error("POST /api/events error:", error); | ||
| return NextResponse.json({ error: "failed to create an event" }, { status: 500 }); | ||
| } | ||
| } | ||
This file contains hidden or 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,17 @@ | ||
| import mongoose, { Schema } from "mongoose"; | ||
|
|
||
| const EventSchema = new Schema({ | ||
| eventId: { type: String, required: true }, | ||
| ts: { type: Date, required: true, default: Date.now }, | ||
| anonUserId: { type: String, required: true }, | ||
| sessionId: { type: Schema.Types.ObjectId, ref: "Session", required: true }, | ||
| event: { type: String, required: true }, | ||
| props: { | ||
| gameId: { type: String, default: null }, | ||
| durationMs: { type: Number, default: null }, | ||
| result: { type: String, default: null }, | ||
| }, | ||
| }); | ||
|
|
||
| // uses the "events" collection in the testing database | ||
| export default mongoose.models.Event || mongoose.model("Event", EventSchema, "events"); |
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.
Nice check for if the sessionid is valid! Can you also add a check for if the session exists? Something like this:
const sessionExists = await Session.exists({ _id: sessionId });
if (!sessionExists) {
return NextResponse.json({ error: "Session not found" }, { status: 404 });
}