database to google sheets added send to google apis from cloud console
This commit is contained in:
parent
667334bae5
commit
04ec0df522
File diff suppressed because it is too large
Load Diff
|
|
@ -19,6 +19,7 @@
|
|||
"astro-google-analytics": "^1.0.3",
|
||||
"astro-icon": "^1.1.5",
|
||||
"dayjs": "^1.11.19",
|
||||
"googleapis": "^171.4.0",
|
||||
"swiper": "^12.1.0",
|
||||
"tailwindcss": "^4.1.18"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,18 +1,19 @@
|
|||
import type { APIRoute } from "astro";
|
||||
import { appendToSheet } from "../lib/googleSheets";
|
||||
export const prerender = false;
|
||||
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
try {
|
||||
// 🔹 Leer datos del formulario
|
||||
const formData = await request.formData();
|
||||
// 🔹 Leer datos del formulario
|
||||
const formData = await request.formData();
|
||||
|
||||
const nombres = formData.get("nombres")?.toString().trim();
|
||||
const apellidos = formData.get("apellidos")?.toString().trim();
|
||||
const pais = formData.get("pais")?.toString().trim();
|
||||
const lugar = formData.get("lugar")?.toString().trim();
|
||||
const direccion = formData.get("direccion")?.toString().trim();
|
||||
const telefono = formData.get("telefono")?.toString().trim();
|
||||
const email = formData.get("email")?.toString().trim();
|
||||
const nombres = formData.get("nombres")?.toString().trim();
|
||||
const apellidos = formData.get("apellidos")?.toString().trim();
|
||||
const pais = formData.get("pais")?.toString().trim();
|
||||
const lugar = formData.get("lugar")?.toString().trim();
|
||||
const direccion = formData.get("direccion")?.toString().trim();
|
||||
const telefono = formData.get("telefono")?.toString().trim();
|
||||
const email = formData.get("email")?.toString().trim();
|
||||
|
||||
|
||||
// 🔹 Validación básica
|
||||
|
|
@ -21,32 +22,45 @@ export const POST: APIRoute = async ({ request }) => {
|
|||
}
|
||||
|
||||
const dataSend = {
|
||||
nombres,
|
||||
apellidos,
|
||||
pais,
|
||||
lugar,
|
||||
direccion,
|
||||
telefono,
|
||||
email
|
||||
};
|
||||
console.log(dataSend)
|
||||
nombres,
|
||||
apellidos,
|
||||
pais,
|
||||
lugar,
|
||||
direccion,
|
||||
telefono,
|
||||
email
|
||||
};
|
||||
const emailApiKey = import.meta.env.EMAIL_API_KEY;
|
||||
|
||||
// 🔹 Enviar a tu backend real
|
||||
const response = await fetch("http://155.138.215.11:3050/email/send", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": emailApiKey!,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION",
|
||||
data: dataSend,
|
||||
}),
|
||||
});
|
||||
const valuesForSheets = [
|
||||
nombres,
|
||||
apellidos,
|
||||
pais,
|
||||
lugar,
|
||||
direccion,
|
||||
telefono,
|
||||
email,
|
||||
new Date().toLocaleString()
|
||||
];
|
||||
|
||||
console.log(response)
|
||||
if (!response.ok) {
|
||||
// 🔹 Enviar a tu backend real
|
||||
const [emailResponse] = await Promise.all([
|
||||
fetch("http://155.138.215.11:3050/email/send", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": emailApiKey!,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION",
|
||||
data: dataSend,
|
||||
}),
|
||||
}),
|
||||
appendToSheet(valuesForSheets) // Guardar en Google Sheets
|
||||
]);
|
||||
|
||||
// console.log(emailResponse)
|
||||
if (!emailResponse.ok) {
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
import { google } from 'googleapis';
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
credentials: {
|
||||
client_email: import.meta.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
|
||||
private_key: import.meta.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||
},
|
||||
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||
});
|
||||
|
||||
const sheets = google.sheets({ version: 'v4', auth });
|
||||
const SPREADSHEET_ID = import.meta.env.GOOGLE_SHEET_ID;
|
||||
|
||||
export async function appendToSheet(values: any[]) {
|
||||
try {
|
||||
await sheets.spreadsheets.values.append({
|
||||
spreadsheetId: SPREADSHEET_ID,
|
||||
range: 'DATABASE!A2:H2',
|
||||
valueInputOption: 'RAW',
|
||||
requestBody: {
|
||||
values: [values],
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error guardando en Sheets:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue