|
|
|
@ -1,4 +1,4 @@
|
|
|
|
// import Cloudflare from "cloudflare";
|
|
|
|
import Cloudflare from "cloudflare";
|
|
|
|
|
|
|
|
|
|
|
|
type TypeEmailData = "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO" | "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION";
|
|
|
|
type TypeEmailData = "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO" | "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION";
|
|
|
|
|
|
|
|
|
|
|
|
@ -32,74 +32,75 @@ export async function sendEmail(data: object, to: string, type: TypeEmailData) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
// CÓDIGO COMENTADO — Cloudflare (Email Workers)
|
|
|
|
// Cloudflare (Email Sending)
|
|
|
|
// Descomentar para volver a Cloudflare en lugar de n8n/Brevo
|
|
|
|
// Usar sendEmailCf() en lugar de sendEmail() (n8n/Brevo)
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
|
|
// const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = {
|
|
|
|
const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = {
|
|
|
|
// CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: {
|
|
|
|
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: {
|
|
|
|
// subject: "Confirmación de inscripción al voluntariado",
|
|
|
|
subject: "Confirmación de inscripción al voluntariado",
|
|
|
|
// url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_postulacion_user.html",
|
|
|
|
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_postulacion_user.html",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: {
|
|
|
|
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: {
|
|
|
|
// subject: "Notificación de contacto institucional",
|
|
|
|
subject: "Notificación de contacto institucional",
|
|
|
|
// url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_info.html",
|
|
|
|
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_info.html",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// const TEMPLATE_CACHE = new Map<string, string>();
|
|
|
|
const TEMPLATE_CACHE = new Map<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
// async function fetchTemplate(type: TypeEmailData): Promise<string> {
|
|
|
|
async function fetchTemplate(type: TypeEmailData): Promise<string> {
|
|
|
|
// const cached = TEMPLATE_CACHE.get(type);
|
|
|
|
const cached = TEMPLATE_CACHE.get(type);
|
|
|
|
// if (cached) return cached;
|
|
|
|
if (cached) return cached;
|
|
|
|
|
|
|
|
|
|
|
|
// const { url } = TEMPLATE_URLS[type];
|
|
|
|
const { url } = TEMPLATE_URLS[type];
|
|
|
|
// const res = await fetch(url);
|
|
|
|
const res = await fetch(url);
|
|
|
|
// if (!res.ok) {
|
|
|
|
if (!res.ok) {
|
|
|
|
// throw new Error(`Error descargando template ${type}: HTTP ${res.status}`);
|
|
|
|
throw new Error(`Error descargando template ${type}: HTTP ${res.status}`);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// const html = await res.text();
|
|
|
|
const html = await res.text();
|
|
|
|
// TEMPLATE_CACHE.set(type, html);
|
|
|
|
TEMPLATE_CACHE.set(type, html);
|
|
|
|
// return html;
|
|
|
|
return html;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// function renderTemplate(template: string, data: Record<string, string>): string {
|
|
|
|
function renderTemplate(template: string, data: Record<string, string>): string {
|
|
|
|
// let html = template;
|
|
|
|
let html = template;
|
|
|
|
// for (const [key, value] of Object.entries(data)) {
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
// html = html.replaceAll(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value ?? "");
|
|
|
|
html = html.replaceAll(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value ?? "");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// return html;
|
|
|
|
return html;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// function stripHtml(html: string): string {
|
|
|
|
function stripHtml(html: string): string {
|
|
|
|
// return html.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
return html.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// export async function sendEmailCf(data: Record<string, string>, to: string, type: TypeEmailData) {
|
|
|
|
export async function sendEmailCf(data: Record<string, string>, to: string, type: TypeEmailData) {
|
|
|
|
// const apiToken = import.meta.env.CLOUDFLARE_API_TOKEN;
|
|
|
|
const apiToken = import.meta.env.CLOUDFLARE_API_TOKEN;
|
|
|
|
// const accountId = import.meta.env.CLOUDFLARE_ACCOUNT_ID;
|
|
|
|
const accountId = import.meta.env.CLOUDFLARE_ACCOUNT_ID;
|
|
|
|
|
|
|
|
|
|
|
|
// const client = new Cloudflare({ apiToken });
|
|
|
|
const client = new Cloudflare({ apiToken });
|
|
|
|
// const { subject } = TEMPLATE_URLS[type];
|
|
|
|
const { subject } = TEMPLATE_URLS[type];
|
|
|
|
// const rawHtml = await fetchTemplate(type);
|
|
|
|
const rawHtml = await fetchTemplate(type);
|
|
|
|
// const html = renderTemplate(rawHtml, data);
|
|
|
|
const html = renderTemplate(rawHtml, data);
|
|
|
|
// const text = stripHtml(html);
|
|
|
|
const text = stripHtml(html);
|
|
|
|
|
|
|
|
|
|
|
|
// const response = await client.emailSending.send({
|
|
|
|
const response = await client.emailSending.send({
|
|
|
|
// account_id: accountId,
|
|
|
|
account_id: accountId,
|
|
|
|
// from: "welcome@mail.centrodelreinodepazyjusticia.com",
|
|
|
|
from: "welcome@mail.centrodelreinodepazyjusticia.com",
|
|
|
|
// to,
|
|
|
|
to,
|
|
|
|
// subject,
|
|
|
|
subject,
|
|
|
|
// html,
|
|
|
|
html,
|
|
|
|
// text,
|
|
|
|
text,
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// if (!response.delivered) {
|
|
|
|
const deliveredOrQueued = [...response.delivered, ...response.queued];
|
|
|
|
// console.error("Cloudflare email not delivered", response);
|
|
|
|
if (!deliveredOrQueued.includes(to) || response.permanent_bounces.includes(to)) {
|
|
|
|
// throw new Error("El correo no fue entregado");
|
|
|
|
console.error("Cloudflare email not delivered", response);
|
|
|
|
// }
|
|
|
|
throw new Error("El correo no fue entregado");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
// ALTERNATIVA: Brevo (SDK directo)
|
|
|
|
// ALTERNATIVA: Brevo (SDK directo)
|
|
|
|
|