staging #94
|
|
@ -25,6 +25,7 @@ pnpm-debug.log*
|
|||
/generated/prisma
|
||||
prisma/*.db
|
||||
prisma/dev.db
|
||||
data/
|
||||
|
||||
# opencode - agentes y scripts locales (no subir a producción)
|
||||
opencode/
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import type { APIRoute } from "astro";
|
||||
import { appendRegistrationToSheet } from "../lib/googleSheets";
|
||||
import { sendEmailCf } from "../lib/email";
|
||||
import { emailExistsInRegistry, addEmailToRegistry } from "../lib/emailsRegistry";
|
||||
export const prerender = false;
|
||||
|
||||
const COLUMNS = [
|
||||
|
|
@ -101,6 +102,13 @@ export const POST: APIRoute = async ({ request }) => {
|
|||
timeZone: "America/Puerto_Rico",
|
||||
});
|
||||
|
||||
if (emailExistsInRegistry(formData.correo)) {
|
||||
return Response.json(
|
||||
{ success: false, message: "Este correo ya está registrado. No se permiten 2 registros." },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
const row = COLUMNS.map((key) => flattenValue(formData[key]));
|
||||
row.push(timestamp);
|
||||
|
||||
|
|
@ -111,6 +119,8 @@ export const POST: APIRoute = async ({ request }) => {
|
|||
apellidos: formData.apellido,
|
||||
}, formData.correo, "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION");
|
||||
|
||||
addEmailToRegistry(formData.correo);
|
||||
|
||||
return Response.json(result);
|
||||
} catch (error) {
|
||||
console.error("Error en /api/formulario/send:", error);
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ export async function sendEmailCf(data: Record<string, string>, to: string, type
|
|||
text,
|
||||
});
|
||||
|
||||
if (response.permanent_bounces.includes(to)) {
|
||||
if (!response.delivered.includes(to)) {
|
||||
console.error("Cloudflare email not delivered", response);
|
||||
throw new Error("El correo no fue entregado");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const REGISTRY_PATH = path.resolve(process.cwd(), "data", "emails.txt");
|
||||
|
||||
export function emailExistsInRegistry(email: string): boolean {
|
||||
try {
|
||||
if (!fs.existsSync(REGISTRY_PATH)) return false;
|
||||
const content = fs.readFileSync(REGISTRY_PATH, "utf-8");
|
||||
return content.split("\n").some((line) => line.trim().toLowerCase() === email.trim().toLowerCase());
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function addEmailToRegistry(email: string): void {
|
||||
const dir = path.dirname(REGISTRY_PATH);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
fs.appendFileSync(REGISTRY_PATH, email.trim().toLowerCase() + "\n");
|
||||
}
|
||||
Loading…
Reference in New Issue