Skip to content

Commit e270170

Browse files
authored
Merge pull request #30 from hack4impact-calpoly/signup-schema-endpoints
Signup schema endpoints made
2 parents 7b61e76 + f15b9f9 commit e270170

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import connectDB from "@/database/db";
2+
import { NextResponse } from "next/server";
3+
import Signup from "@/database/models/signupSchema";
4+
5+
/**
6+
* gets 1 signup from the database based on the signupId
7+
* returns all signups in the database as a JSON response
8+
* if an error occurs, returns a JSON response w
9+
signupId: new ObjectId().toString(),ith an error message and status code 500
10+
*/
11+
12+
type IParams = {
13+
params: {
14+
signupId: String;
15+
};
16+
};
17+
18+
export async function GET(request: Request, { params }: IParams): Promise<NextResponse> {
19+
// Attempt to connect to the database
20+
await connectDB();
21+
22+
const { signupId } = params;
23+
24+
try {
25+
const signup = await Signup.findOne({ signupId: signupId });
26+
// check if signup exists
27+
if (!signup) {
28+
return NextResponse.json({ message: "Signup not found." }, { status: 404 });
29+
}
30+
// if signup exists, return it
31+
return NextResponse.json(
32+
{
33+
signup: signup,
34+
},
35+
{ status: 200 },
36+
);
37+
} catch (err) {
38+
console.error("Error fetching signup:", err);
39+
return NextResponse.json({
40+
message: "Failed to fetch signup.",
41+
error: err instanceof Error ? err.message : "An unknown error occurred.",
42+
status: 500,
43+
});
44+
}
45+
}

src/app/api/signup/route.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import mongoose, { Schema } from "mongoose";
2+
3+
/*
4+
figma shows that liability form is yes/no
5+
so used a boolean for the waiver field. If it's true, the user has signed the waiver; if it's false, they haven't
6+
7+
Using Date for timestamp because it will automatically store the date and time when the signup is created, which can be useful for tracking when users signed up for shifts.
8+
9+
*/
10+
11+
type signup = {
12+
signupId: String;
13+
shiftId: String;
14+
profileId: String;
15+
waiver: Boolean;
16+
timestamp: Date;
17+
};
18+
19+
const signupSchema = new Schema<signup>({
20+
signupId: { type: String, required: true },
21+
shiftId: { type: String, required: true },
22+
profileId: { type: String, required: true },
23+
waiver: { type: Boolean, required: true },
24+
timestamp: { type: Date, required: true },
25+
});
26+
27+
const Signup = mongoose.models["signup"] || mongoose.model("signup", signupSchema, "signup");
28+
29+
export default Signup;

0 commit comments

Comments
 (0)