add validation by email

This commit is contained in:
David Ascanio 2026-02-14 17:57:47 -03:00
parent ce1bf25806
commit 76a2919278
2 changed files with 10 additions and 16 deletions

View File

@ -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,

View File

@ -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<boolean> {
export async function emailExists(email: string): Promise<boolean> {
const formula = `=COUNTIF(G:G,"${email}")`;
// Escribir fórmula en celda auxiliar
@ -39,20 +39,6 @@ async function emailExists(email: string): Promise<boolean> {
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',