124 lines
3.0 KiB
TypeScript
124 lines
3.0 KiB
TypeScript
import type { APIRoute } from "astro";
|
|
import { appendRegistrationToSheet } from "../lib/googleSheets";
|
|
import { sendEmail } from "../lib/email";
|
|
export const prerender = false;
|
|
|
|
const COLUMNS = [
|
|
"aceptacion_reglamento",
|
|
"nombre_completo",
|
|
"apellido",
|
|
"documentos",
|
|
"correo",
|
|
"reglamento_firma",
|
|
"fecha_nacimiento",
|
|
"nacionalidad",
|
|
"sexo",
|
|
"direccion_completa",
|
|
"ciudad",
|
|
"estado",
|
|
"pais",
|
|
"postal",
|
|
"telefono",
|
|
"whatsapp",
|
|
"profesion",
|
|
"lugar_trabajo_actual",
|
|
"nivel_academico",
|
|
"idioma",
|
|
"idioma_nivel_Espanol",
|
|
"idioma_nivel_Ingles",
|
|
"idioma_nivel_Hebreo",
|
|
"idioma_nivel_Portugues",
|
|
"idioma_nivel_Otro",
|
|
"idioma_otro",
|
|
"voluntariado_anterior",
|
|
"caso_si",
|
|
"areas_colaborar",
|
|
"areas_otra",
|
|
"dias_disponibles",
|
|
"horario_preferido",
|
|
"fuera_ciudad",
|
|
"misiones_internacionales",
|
|
"condicion_medica",
|
|
"condicion_medica_cual",
|
|
"alergias",
|
|
"medicamentos",
|
|
"emergencia_nombre",
|
|
"emergencia_parentesco",
|
|
"emergencia_telefono",
|
|
"emergencia_correo",
|
|
"confirmar_nombre",
|
|
"confirmar_firma",
|
|
"declaracion_voluntario",
|
|
"confirmacion",
|
|
];
|
|
|
|
function flattenValue(val: unknown): string {
|
|
if (Array.isArray(val)) {
|
|
return val.join(", ");
|
|
}
|
|
if (val === null || val === undefined) {
|
|
return "";
|
|
}
|
|
return String(val);
|
|
}
|
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
try {
|
|
const body = await request.json();
|
|
const formData = body.formData || {};
|
|
|
|
const honeypot = body.honeypot || "";
|
|
if (honeypot) {
|
|
return Response.json(
|
|
{ success: false, message: "Solicitud rechazada" },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const turnstileToken = body.turnstileToken || "";
|
|
const turnstileSecret = import.meta.env.TURNSTILE_SECRET_KEY;
|
|
if (turnstileSecret && turnstileToken) {
|
|
const verify = await fetch(
|
|
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
{
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
body: `secret=${turnstileSecret}&response=${turnstileToken}`,
|
|
}
|
|
);
|
|
const outcome = await verify.json();
|
|
if (!outcome.success) {
|
|
return Response.json(
|
|
{ success: false, message: "Captcha inválido" },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
}
|
|
|
|
const timestamp = new Date().toLocaleString("es-ES", {
|
|
timeZone: "America/Puerto_Rico",
|
|
});
|
|
|
|
const row = COLUMNS.map((key) => flattenValue(formData[key]));
|
|
row.push(timestamp);
|
|
|
|
const result = await appendRegistrationToSheet(row);
|
|
|
|
await sendEmail({
|
|
nombres: formData.nombre,
|
|
apellidos: formData.apellido,
|
|
}, formData.correo, "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION");
|
|
|
|
return Response.json(result);
|
|
} catch (error) {
|
|
console.error("Error en /api/formulario/send:", error);
|
|
return Response.json(
|
|
{
|
|
success: false,
|
|
message: error instanceof Error ? error.message : "Error al enviar el formulario",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
};
|