Compare commits

..

No commits in common. "971b413c5a3e5c7577dc80aed987790da18d1927" and "a209513a688994f48b152637f482307a47f744a9" have entirely different histories.

2 changed files with 16 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import type { APIRoute } from "astro"; import type { APIRoute } from "astro";
import { appendToSheet, emailExists } from "../lib/googleSheets"; import { appendToSheet } from "../lib/googleSheets";
export const prerender = false; export const prerender = false;
export const POST: APIRoute = async ({ request }) => { export const POST: APIRoute = async ({ request }) => {
@ -21,14 +21,6 @@ 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;
export async function emailExists(email: string): Promise<boolean> { 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,6 +39,20 @@ export 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',