Files
2023-02-17 06:57:54 -06:00

74 lines
2.6 KiB
JavaScript

const { getTime, getDate, getFormat, getCompareDate, getFormatMs } = require('util-tiempo')
const { GoogleSpreadsheet } = require('google-spreadsheet');
const fs = require('fs');
const RESPONSES_SHEET_ID = '1tVRX1ojXJadsjRUJ-pNv8DZgziaIMcAdsMtPmWQRBcM'; //Generar en google.cloud
const doc = new GoogleSpreadsheet(RESPONSES_SHEET_ID);
const CREDENTIALS = JSON.parse(fs.readFileSync('./implementaciones/credenciales.json'));
async function ingresarDatos(numero, mensaje, sentido){
let Fecha = getDate();
let Hora = getTime(Date.now(), {local: 'es-MX', timeZone: 'America/Mexico_City', hour12:false});
elNum = numero.replace('@c.us', '')
if(elNum.substring(0,3) == '521') { elNum = elNum.replace('521', '52') }
let rows = [{
Numero: elNum.trim(),
Mensaje: mensaje,
Sentido: sentido,
Fecha: Fecha,
Hora: Hora
}];
try{
await doc.useServiceAccountAuth({
client_email: CREDENTIALS.client_email,
private_key: CREDENTIALS.private_key
});
await doc.loadInfo();
let sheet = doc.sheetsByTitle['Mensajes'];
// console.log("SHEET=", sheet)
for (let index = 0; index < rows.length; index++) {
const row = rows[index];
await sheet.addRow(row);
console.log(`Datos guardados - ${sentido} (sheets.js)`)
}
}
catch{
console.log("Error Sheets")
}
}
async function leerDatos(telsim){
await doc.useServiceAccountAuth({
client_email: CREDENTIALS.client_email,
private_key: CREDENTIALS.private_key
});
await doc.loadInfo();
let sheet = doc.sheetsByIndex[0];
let nya;
let cont = 0
let found = false
nya = [];
let rows = await sheet.getRows();
console.log(rows.length)
// for (let index = 0; index < rows.length; index++) {
// const row = rows[index];
// if (row.Telefono == telsim) {
// nya['Nombre'] = row.Nombre+' '+row.Apellido
// nya['Direccion'] = row.Direccion+' '+row.Planta+', '+row.CP
// nya['Estado'] = row.Estado
// }
// }
while (!found && cont < rows.length){ //Usamos while para que no recorra TODOS los registros y se pare encuanto lo encuentre.
const row = rows[cont];
console.log(row.Nombre, found, cont)
if (row.Telefono == telsim) {
nya['Nombre'] = row.Nombre+' '+row.Apellido
nya['Direccion'] = row.Direccion+', '+row.Planta+', CP:'+row.CP
nya['Estado'] = row.Estado
found = true
}
cont++
}
return nya
};
module.exports = {ingresarDatos,leerDatos};