create_form #48
|
|
@ -1,5 +1,6 @@
|
|||
import type { APIRoute } from "astro";
|
||||
import { appendRegistrationToSheet } from "../lib/googleSheets";
|
||||
import { sendEmail } from "../lib/email";
|
||||
export const prerender = false;
|
||||
|
||||
const COLUMNS = [
|
||||
|
|
@ -75,8 +76,14 @@ export const POST: APIRoute = async ({ request }) => {
|
|||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
type TypeEmailData = "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO" | "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION";
|
||||
|
||||
export async function sendEmail(data: object, to: string, type: TypeEmailData) {
|
||||
const emailApiKey = import.meta.env.EMAIL_API_KEY;
|
||||
|
||||
const dataFinal = {
|
||||
...data,
|
||||
email: to,
|
||||
senderName: "Centro del Reino de Paz y Justicia",
|
||||
};
|
||||
|
||||
const emailResponse = await fetch("https://flows2.carpa.com/webhook/email/send", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": emailApiKey!,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: type,
|
||||
data: dataFinal,
|
||||
}),
|
||||
});
|
||||
|
||||
const responseBody = await emailResponse.text();
|
||||
|
||||
if (!emailResponse.ok) {
|
||||
console.error(`Error enviando correo (status ${emailResponse.status}): ${responseBody}`);
|
||||
throw new Error(`Error en servicio de correo: ${emailResponse.status}`);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue