|
| 1 | +import connectDB from "@/database/db"; |
| 2 | +import { NextResponse } from "next/server"; |
| 3 | +import Signup from "@/database/models/signupSchema"; |
| 4 | +import { ObjectId } from "mongodb"; |
| 5 | +import { sign } from "crypto"; |
| 6 | + |
| 7 | +/** |
| 8 | + * gets all signups from the database |
| 9 | + * returns all signups in the database as a JSON response |
| 10 | + * if an error occurs, returns a JSON response with an error message and status code 500 |
| 11 | + */ |
| 12 | + |
| 13 | +export async function GET(): Promise<NextResponse> { |
| 14 | + // Attempt to connect to the database |
| 15 | + await connectDB(); |
| 16 | + |
| 17 | + try { |
| 18 | + const signups = await Signup.find(); |
| 19 | + return NextResponse.json( |
| 20 | + { |
| 21 | + signups: signups, |
| 22 | + }, |
| 23 | + { status: 200 }, |
| 24 | + ); |
| 25 | + } catch (err) { |
| 26 | + console.error("Error fetching signups:", err); |
| 27 | + return NextResponse.json( |
| 28 | + { |
| 29 | + message: "Failed to fetch signups.", |
| 30 | + error: err instanceof Error ? err.message : "An unknown error occurred.", |
| 31 | + }, |
| 32 | + { status: 500 }, |
| 33 | + ); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/* |
| 38 | + * creates a new signup in the database |
| 39 | + * request must require the following fields: |
| 40 | + * shiftId: string; |
| 41 | + profileId: string; |
| 42 | + waiver: boolean; |
| 43 | + */ |
| 44 | + |
| 45 | +export async function POST(request: Request): Promise<NextResponse> { |
| 46 | + await connectDB(); |
| 47 | + |
| 48 | + try { |
| 49 | + const body = await request.json(); |
| 50 | + |
| 51 | + // ensure required fields are present |
| 52 | + |
| 53 | + // "timestamp" is not required in the request body because it will be generated automatically when the signup is created |
| 54 | + |
| 55 | + const requiredFields = ["shiftId", "profileId", "waiver"]; |
| 56 | + |
| 57 | + for (const field of requiredFields) { |
| 58 | + if (!body[field]) { |
| 59 | + // if waiver is false, it will be sent as "false" in the request body, which is a falsy value in JavaScript. To account for this, we need to check if the field is explicitly undefined rather than just falsy. |
| 60 | + if (field === "waiver" && body[field] === false) { |
| 61 | + continue; // skip the check for waiver if it's false |
| 62 | + } |
| 63 | + return NextResponse.json({ message: `Missing required field: ${field}` }, { status: 400 }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const newSignup = new Signup({ |
| 68 | + signupId: crypto.randomUUID(), |
| 69 | + shiftId: body.shiftId, |
| 70 | + profileId: body.profileId, |
| 71 | + waiver: body.waiver, |
| 72 | + timestamp: new Date(), |
| 73 | + }); |
| 74 | + |
| 75 | + const saved = await newSignup.save(); |
| 76 | + |
| 77 | + return NextResponse.json( |
| 78 | + { |
| 79 | + signup: saved, |
| 80 | + }, |
| 81 | + { status: 201 }, |
| 82 | + ); |
| 83 | + } catch (err) { |
| 84 | + console.error("Error creating signup:", err); |
| 85 | + return NextResponse.json( |
| 86 | + { |
| 87 | + message: "Failed to create signup.", |
| 88 | + error: err instanceof Error ? err.message : "An unknown error occurred.", |
| 89 | + }, |
| 90 | + { status: 500 }, |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments