ghostguild-org/server/api/events/[id]/check-registration.post.js
Jennie Robinson Faber 025c1a180f Add Zod validation to all API endpoints and remove debug test route
Adds schema-based input validation across helcim, events, members,
series, admin, and updates API endpoints. Removes the peer-support
debug test endpoint. Adds validation test coverage.
2026-03-01 17:04:26 +00:00

42 lines
1.1 KiB
JavaScript

import Event from "../../../models/event";
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, "id");
const body = await validateBody(event, checkRegistrationSchema);
const { email } = body;
try {
// Check if id is a valid ObjectId or treat as slug
const isObjectId = /^[0-9a-fA-F]{24}$/.test(id);
const query = isObjectId
? { $or: [{ _id: id }, { slug: id }] }
: { slug: id };
const eventDoc = await Event.findOne(query);
if (!eventDoc) {
throw createError({
statusCode: 404,
statusMessage: "Event not found",
});
}
// Check if the email exists in the registrations array
const isRegistered = eventDoc.registrations.some(
(registration) =>
registration.email.toLowerCase() === email.toLowerCase(),
);
return {
isRegistered,
eventId: eventDoc._id,
eventTitle: eventDoc.title,
};
} catch (error) {
console.error("Error checking registration:", error);
throw createError({
statusCode: 500,
statusMessage: "Failed to check registration status",
});
}
});