82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
import * as z from 'zod';
|
|
|
|
import {
|
|
apiError,
|
|
apiSuccess,
|
|
emailSchema,
|
|
requiredString,
|
|
} from '@kit/next/route-helpers';
|
|
import { getLogger } from '@kit/shared/logger';
|
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
|
|
|
const MembershipApplySchema = z.object({
|
|
accountId: requiredString('Konto-ID'),
|
|
firstName: requiredString('Vorname'),
|
|
lastName: requiredString('Nachname'),
|
|
email: emailSchema,
|
|
phone: z.string().optional(),
|
|
street: z.string().optional(),
|
|
postalCode: z.string().optional(),
|
|
city: z.string().optional(),
|
|
dateOfBirth: z.string().optional(),
|
|
message: z.string().optional(),
|
|
});
|
|
|
|
export async function POST(request: Request) {
|
|
const logger = await getLogger();
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const parsed = MembershipApplySchema.safeParse(body);
|
|
|
|
if (!parsed.success) {
|
|
return apiError(parsed.error.issues[0]?.message ?? 'Ungültige Eingabe');
|
|
}
|
|
|
|
const {
|
|
accountId,
|
|
firstName,
|
|
lastName,
|
|
email,
|
|
phone,
|
|
street,
|
|
postalCode,
|
|
city,
|
|
dateOfBirth,
|
|
message,
|
|
} = parsed.data;
|
|
|
|
const supabase = getSupabaseServerAdminClient();
|
|
|
|
const { error } = await supabase.from('membership_applications').insert({
|
|
account_id: accountId,
|
|
first_name: firstName,
|
|
last_name: lastName,
|
|
email,
|
|
phone: phone || null,
|
|
street: street || null,
|
|
postal_code: postalCode || null,
|
|
city: city || null,
|
|
date_of_birth: dateOfBirth || null,
|
|
message: message || null,
|
|
status: 'submitted',
|
|
});
|
|
|
|
if (error) {
|
|
logger.error(
|
|
{ error, context: 'membership-apply-insert' },
|
|
'[membership-apply] Insert error',
|
|
);
|
|
return apiError('Bewerbung fehlgeschlagen', 500);
|
|
}
|
|
|
|
return apiSuccess({ message: 'Bewerbung erfolgreich eingereicht' });
|
|
} catch (err) {
|
|
logger.error(
|
|
{ error: err, context: 'membership-apply' },
|
|
'[membership-apply] Error',
|
|
);
|
|
return apiError('Serverfehler', 500);
|
|
}
|
|
}
|