ghostguild-org/server/api/events/[id]/check-registration.post.js

49 lines
1.2 KiB
JavaScript

import Event from "../../../models/event";
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, "id");
const body = await readBody(event);
const { email } = body;
if (!email) {
throw createError({
statusCode: 400,
statusMessage: "Email is required",
});
}
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",
});
}
});