EventBuzz_Console/src/app/components/dashboard/registrationFormModule.ts

41 lines
1.5 KiB
TypeScript

import { formsApi } from "../../../services/api";
import type { Form, FormField, FormWithSubmissionCount } from "../../../services/api/types";
export const DEFAULT_REGISTRATION_FORM_NAME = "Registration Form";
export const DEFAULT_REGISTRATION_FORM_FIELDS: FormField[] = [
{ label: "First name", type: "text", required: true, mapsTo: "firstName", isFixed: true },
{ label: "Last name", type: "text", required: true, mapsTo: "lastName", isFixed: true },
{ label: "Email", type: "email", required: true, mapsTo: "email", isFixed: true },
];
/** Registration form linked to the event, or null when the default template should be used. */
export async function resolveEventRegistrationForm(
eventId: string,
linkedFormId?: string | null,
): Promise<FormWithSubmissionCount | null> {
const forms = await formsApi.listForEvent(eventId);
const registrationForms = forms.filter((f) => f.type === "registration");
const id = linkedFormId?.trim();
if (id) {
const linked = registrationForms.find((f) => f._id === id);
if (linked) return linked;
try {
const form = await formsApi.get(id);
if (form.type === "registration") {
const count = registrationForms.find((f) => f._id === form._id)?.submissionCount ?? 0;
return { ...form, submissionCount: count };
}
} catch {
// fall through to first registration form or default
}
}
return registrationForms[0] ?? null;
}
export function registrationFormDisplayName(form: Form | null): string {
return form?.name?.trim() || DEFAULT_REGISTRATION_FORM_NAME;
}