diff --git a/src/pages/api/emailPostulacion/send.ts b/src/pages/api/emailPostulacion/send.ts index cc735dc..72ec10d 100644 --- a/src/pages/api/emailPostulacion/send.ts +++ b/src/pages/api/emailPostulacion/send.ts @@ -1,5 +1,5 @@ import type { APIRoute } from "astro"; -import { appendToSheet } from "../lib/googleSheets"; +import { appendToSheet, emailExists } from "../lib/googleSheets"; export const prerender = false; export const POST: APIRoute = async ({ request }) => { @@ -21,6 +21,14 @@ export const POST: APIRoute = async ({ request }) => { return Response.redirect("/?error=datos", 303); } + const exists = await emailExists(email); + if (exists) { + return Response.json({ + success: false, + message: 'El email ya existe en la base de datos', + }) + } + const dataSend = { nombres, apellidos, diff --git a/src/pages/api/lib/googleSheets.ts b/src/pages/api/lib/googleSheets.ts index f333aaa..b963bda 100644 --- a/src/pages/api/lib/googleSheets.ts +++ b/src/pages/api/lib/googleSheets.ts @@ -11,7 +11,7 @@ const auth = new google.auth.GoogleAuth({ const sheets = google.sheets({ version: 'v4', auth }); const SPREADSHEET_ID = import.meta.env.GOOGLE_SHEET_ID; -async function emailExists(email: string): Promise { +export async function emailExists(email: string): Promise { const formula = `=COUNTIF(G:G,"${email}")`; // Escribir fórmula en celda auxiliar @@ -39,20 +39,6 @@ async function emailExists(email: string): Promise { export async function appendToSheet(values: any[]) { try { - const email = values[6]; // índice donde está el email en tu array - - if (!email) { - throw new Error('Email requerido'); - } - - const exists = await emailExists(email); - if (exists) { - return { - success: false, - message: 'El email ya existe en la base de datos', - }; - } - await sheets.spreadsheets.values.append({ spreadsheetId: SPREADSHEET_ID, range: 'DATABASE!A2:H2',