Merge pull request 'add validation by email' (#9) from duplicate-record-validation into main
Reviewed-on: #9
This commit is contained in:
commit
971b413c5a
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Reference in New Issue