Compare commits

..

2 Commits

2 changed files with 10 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import type { APIRoute } from "astro"; import type { APIRoute } from "astro";
import { appendToSheet } from "../lib/googleSheets"; import { appendToSheet, emailExists } from "../lib/googleSheets";
export const prerender = false; export const prerender = false;
export const POST: APIRoute = async ({ request }) => { export const POST: APIRoute = async ({ request }) => {
@ -21,6 +21,14 @@ export const POST: APIRoute = async ({ request }) => {
return Response.redirect("/?error=datos", 303); 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 = { const dataSend = {
nombres, nombres,
apellidos, apellidos,

View File

@ -11,7 +11,7 @@ const auth = new google.auth.GoogleAuth({
const sheets = google.sheets({ version: 'v4', auth }); const sheets = google.sheets({ version: 'v4', auth });
const SPREADSHEET_ID = import.meta.env.GOOGLE_SHEET_ID; 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}")`; const formula = `=COUNTIF(G:G,"${email}")`;
// Escribir fórmula en celda auxiliar // Escribir fórmula en celda auxiliar
@ -39,20 +39,6 @@ async function emailExists(email: string): Promise<boolean> {
export async function appendToSheet(values: any[]) { export async function appendToSheet(values: any[]) {
try { 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({ await sheets.spreadsheets.values.append({
spreadsheetId: SPREADSHEET_ID, spreadsheetId: SPREADSHEET_ID,
range: 'DATABASE!A2:H2', range: 'DATABASE!A2:H2',