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-google-analytics": "^1.0.3",
|
||||||
"astro-icon": "^1.1.5",
|
"astro-icon": "^1.1.5",
|
||||||
"dayjs": "^1.11.19",
|
"dayjs": "^1.11.19",
|
||||||
|
"googleapis": "^171.4.0",
|
||||||
"swiper": "^12.1.0",
|
"swiper": "^12.1.0",
|
||||||
"tailwindcss": "^4.1.18"
|
"tailwindcss": "^4.1.18"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
|
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 }) => {
|
||||||
|
|
@ -29,11 +30,22 @@ export const POST: APIRoute = async ({ request }) => {
|
||||||
telefono,
|
telefono,
|
||||||
email
|
email
|
||||||
};
|
};
|
||||||
console.log(dataSend)
|
|
||||||
const emailApiKey = import.meta.env.EMAIL_API_KEY;
|
const emailApiKey = import.meta.env.EMAIL_API_KEY;
|
||||||
|
|
||||||
|
const valuesForSheets = [
|
||||||
|
nombres,
|
||||||
|
apellidos,
|
||||||
|
pais,
|
||||||
|
lugar,
|
||||||
|
direccion,
|
||||||
|
telefono,
|
||||||
|
email,
|
||||||
|
new Date().toLocaleString()
|
||||||
|
];
|
||||||
|
|
||||||
// 🔹 Enviar a tu backend real
|
// 🔹 Enviar a tu backend real
|
||||||
const response = await fetch("http://155.138.215.11:3050/email/send", {
|
const [emailResponse] = await Promise.all([
|
||||||
|
fetch("http://155.138.215.11:3050/email/send", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|
@ -43,10 +55,12 @@ export const POST: APIRoute = async ({ request }) => {
|
||||||
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION",
|
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION",
|
||||||
data: dataSend,
|
data: dataSend,
|
||||||
}),
|
}),
|
||||||
});
|
}),
|
||||||
|
appendToSheet(valuesForSheets) // Guardar en Google Sheets
|
||||||
|
]);
|
||||||
|
|
||||||
console.log(response)
|
// console.log(emailResponse)
|
||||||
if (!response.ok) {
|
if (!emailResponse.ok) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 302,
|
status: 302,
|
||||||
headers: {
|
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