Skip to content
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

transfered Firebase db calls from pbctfForm page to /api/registration/pbctf #121

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 102 additions & 3 deletions app/(default)/api/registration/pbctf/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,88 @@ import {
} from "firebase/firestore";
import { NextResponse } from "next/server";

// Add a new registration
//Check if USN exists
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const usn = searchParams.get("usn");
if (!usn) {
return NextResponse.json({ error: "usn is required" }, { status: 400 });
}
const q = query(
collection(db, "pbctf_registrations"),
where("participant1.usn", "==", usn)
);
const querySnapshot = await getDocs(q);
if (!querySnapshot.empty) {
return NextResponse.json(
{ message: "usn not registered", isUnique: true },
{ status: 200 }
);
}
const q2 = query(
collection(db, "pbctf_registrations"),
where("participant2.usn", "==", usn)
);
const querySnapshot2 = await getDocs(q2);

if (!querySnapshot2.empty) {
return NextResponse.json(
{ message: "usn not unique", isUnique: true },
{ status: 200 }
);
} else {
return NextResponse.json(
{ message: "usn already exists", isUnique: false },
{ status: 403 }
);
}
} catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}

export async function POST(request: Request) {
try {
const { searchParams } = new URL(request.url); // Extract query parameters
const action = searchParams.get("action"); // Determine the action from query params

if (action === "validateRecaptcha") {
return validateRecaptcha(request);
} else if (action === "addRegistration") {
return addRegistration(request);
} else {
return NextResponse.json(
{ error: "Invalid action specified" },
{ status: 400 }
);
}
} catch (error) {
console.error("Error processing request:", error);
return NextResponse.json(
{ error: "An error occurred", details: error },
{ status: 500 }
);
}
}

// Add a new registration
async function validateRecaptcha(request: Request) {
const formData = await request.json();
const { recaptcha_token } = formData;


const recaptchaToken = recaptcha_token;

const details = {
Expand All @@ -27,7 +103,6 @@ export async function POST(request: Request) {
},
};


if (!recaptchaToken) {
return NextResponse.json(
{
Expand Down Expand Up @@ -61,3 +136,27 @@ export async function POST(request: Request) {
// Return a response
return NextResponse.json({ message: "Recaptcha validated!" });
}

async function addRegistration(request: Request) {
try {
const data = await request.json();
if (!data || !data.participant1 || !data.participationType) {
return NextResponse.json(
{
error:
"Invalid data. Participant1 and participationType are required.",
},
{ status: 400 }
);
}
await addDoc(collection(db, "pbctf_registrations"), data);

return NextResponse.json({ message: "Registration successful!" });
} catch (error) {
console.error("Error adding registration:", error);
return NextResponse.json(
{ error: "Failed to add registration.", details: error },
{ status: 500 }
);
}
}
Loading