322 lines
12 KiB
JavaScript
322 lines
12 KiB
JavaScript
// const { sequelize,Test} = require('../models')
|
|
const { logMsg } = require('../services/logger.js');
|
|
const { GoogleSpreadsheet } = require('google-spreadsheet');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
// const { log } = require('util');
|
|
require('dotenv').config();
|
|
let dropdown = [];
|
|
let column_name;
|
|
let doc;
|
|
let CREDENTIALS;
|
|
// use in addRow and getRow api
|
|
let tempData = [];
|
|
const filePath = path.join(__dirname, 'data.json');
|
|
const datafromjson = require('./data.json');
|
|
const { log } = require('console');
|
|
// const { col } = require('sequelize');
|
|
|
|
let number; // Declare number as a global variable
|
|
|
|
async function fetchSheetData() {
|
|
try {
|
|
let sheet1 = doc.sheetsByIndex[0];
|
|
let rows1 = await sheet1.getRows();
|
|
number = rows1.length+1; // Assign the value to the global variable 'number'
|
|
|
|
// You can now use the 'number' variable as a global variable.
|
|
console.log(`Number of rows: ${number}`);
|
|
} catch (error) {
|
|
console.error('Error fetching sheet data:', error);
|
|
}
|
|
}
|
|
|
|
// Call the async function to fetch and work with sheet data.
|
|
|
|
|
|
// You can use 'number' outside of the function.
|
|
console.log(`Global number variable: ${number}`);
|
|
|
|
exports.Welcome = (req, res) =>
|
|
{
|
|
// number=0;
|
|
console.log(datafromjson.length);
|
|
try
|
|
{
|
|
const Msg = "Welcom....!";
|
|
console.log(Msg);
|
|
logMsg.info("Api Call Success");
|
|
res.send({'status':200 , message:"Success." ,'data': Msg});
|
|
|
|
} catch(err) {
|
|
res.status(500).send({'status':404,
|
|
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
|
|
}); } //end of try catch
|
|
|
|
};
|
|
|
|
exports.loginPage = async (req, res ) =>
|
|
{
|
|
try
|
|
{
|
|
res.render( 'login' ,function (err, html ) { res.send(html) })
|
|
} catch(err) {
|
|
res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|
|
exports.adduser = async (req, res ) =>
|
|
{
|
|
try
|
|
{
|
|
res.render( 'adduser' ,function (err, html ) { res.send(html) })
|
|
} catch(err) {
|
|
res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|
|
exports.addjson = async (req, res ) =>
|
|
{
|
|
try
|
|
{
|
|
|
|
const getvalue = req.body.data;
|
|
// console.log(req.body.data);
|
|
let newData={"email":getvalue};
|
|
fs.readFile(filePath, 'utf8', (readErr, data) => {
|
|
if (readErr) {
|
|
console.error('Error reading file:', readErr);
|
|
return;
|
|
}
|
|
let existingData = [];
|
|
|
|
try {
|
|
// Parse the existing JSON data (if any)
|
|
existingData = JSON.parse(data);
|
|
} catch (parseErr) {
|
|
console.error('Error parsing existing data:', parseErr);
|
|
}
|
|
|
|
// Append the new data to the existing array
|
|
existingData.push(newData);
|
|
|
|
// Write the updated data back to the JSON file
|
|
fs.writeFile(filePath, JSON.stringify(existingData, null, 2), (writeErr) => {
|
|
if (writeErr) {
|
|
console.error('Error writing to file:', writeErr);
|
|
} else {
|
|
console.log('Data appended and saved to file successfully.');
|
|
}
|
|
});
|
|
})
|
|
if(rows[0]['Gatnayak'] == tempData[0].email ) {res.send({'status':200 , message:"Success." ,'data': "success" , 'statusCode' : 1 }); }
|
|
else{res.send({'status':200 , message:"Success." ,'data': "success", 'statusCode' : 0 }); }
|
|
} catch(err) {
|
|
res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|
|
exports.getRow = async (req, res) =>
|
|
{
|
|
try
|
|
{
|
|
fetchSheetData();
|
|
for (let index = 0; index < datafromjson.length; index++) {
|
|
// console.log(req.query.email);
|
|
// email = req.query.email;
|
|
const email=req.body.email;
|
|
req.session.user = { email };
|
|
// console.log(req.session.user.email);
|
|
// console.log(email);
|
|
aadhaemail= datafromjson[0].email;
|
|
let emaill = datafromjson[index].email;
|
|
if (email == emaill) {
|
|
const empData = [];
|
|
tempData = ['fdd'];
|
|
await doc.loadInfo();
|
|
let sheet = doc.sheetsByIndex[0];
|
|
let rows = await sheet.getRows();
|
|
// const number = rows.length+1;
|
|
category = []; age_Category = [];
|
|
basthi = []; varga = [];
|
|
milan_shaka = []; poruppu = [];
|
|
blood_group = []; ganavesh_2w_4w_basg = [];
|
|
// new_index = 0;
|
|
|
|
category.push('Redmi','Samsung','ONEPLUS');
|
|
age_Category.push('18-21','22-25','26-30');
|
|
basthi.push('Niruha/Kashaya Basthi','Anuvasana Basthi');
|
|
varga.push('Rasi','Hora','Chaturthamsa','Dasamsa','Shodasamsa');
|
|
milan_shaka.push('');
|
|
poruppu.push('');
|
|
blood_group.push('A+','A-','B+','B-','AB+','AB-','O+','O-');
|
|
ganavesh_2w_4w_basg.push('')
|
|
for (let index = 0; index < rows.length; index++) {
|
|
const row = rows[index];
|
|
category.push(row['Category']);
|
|
age_Category.push(row['Age Category']);
|
|
basthi.push(row['Basthi']);
|
|
varga.push(row['Varga']);
|
|
milan_shaka.push(row['Milan / Shaka']);
|
|
poruppu.push(row['Poruppu']);
|
|
blood_group.push(row['Blood Group']);
|
|
ganavesh_2w_4w_basg.push(row['Ganavesh']);
|
|
// if (index == rows.length-1) {
|
|
// new_index = Number(row.Sl) + 1;
|
|
// }
|
|
};
|
|
dropdown ={
|
|
"category": [...new Set(category)],
|
|
"age_Category" : [...new Set(age_Category)],
|
|
"basthi" : [...new Set(basthi)],
|
|
"varga" : [...new Set(varga)],
|
|
"milan_shaka" : [...new Set(milan_shaka)],
|
|
"poruppu" : [...new Set(poruppu)],
|
|
"blood_group" : [...new Set(blood_group)],
|
|
"ganavesh_2w_4w_basg" : [...new Set(ganavesh_2w_4w_basg)],
|
|
// "new_index" : new_index
|
|
}
|
|
for (let index = 0; index < rows.length; index++) {
|
|
const row = rows[index];
|
|
if(index == 0){ column_name = row._sheet.headerValues; }
|
|
if (row.email == email) {
|
|
var obj = {}
|
|
for (let i = 0; i < column_name.length; i++){
|
|
Object.assign( obj , { [column_name[i]] : row[column_name[i]] });
|
|
}
|
|
tempData.push(obj);
|
|
}
|
|
};
|
|
// if (tempData.length > 0) {
|
|
for (let index = 0; index < rows.length; index++) {
|
|
const row = rows[index];
|
|
// console.log(rows[0]._rawData[22]);
|
|
// if (row == email || row.Gatnayak == tempData[0]['email'] ) {
|
|
if(true){
|
|
// console.log(row.);
|
|
var obj = {}
|
|
for (let i = 0; i < column_name.length; i++){
|
|
Object.assign( obj , { [column_name[i]] : row[column_name[i]] });
|
|
}
|
|
empData.push(obj);
|
|
}
|
|
};
|
|
|
|
res.render( 'displaydata' , { "data" : empData , "dropdown" : dropdown , aadhaemail,email,number} , function (err, html ) { res.send(html) });
|
|
// }
|
|
// else{
|
|
// res.send('<p style="text-align:center;font-size: 4em;top: 510px;font-style:bold">No Data</p>');
|
|
// }
|
|
}
|
|
}
|
|
|
|
res.end("<h1 style='text-align:center;font-size:40px'>No Data</h1>");
|
|
}catch(err) {
|
|
res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" });
|
|
}
|
|
};
|
|
|
|
|
|
exports.addRow = async (req, res) =>{
|
|
try{
|
|
newData = req.body.data;
|
|
console.log(newData);
|
|
var obj = {}
|
|
for (let i = 0; i < column_name.length; i++){
|
|
Object.assign( obj , { [column_name[i]] : newData[i] });
|
|
}
|
|
let rows = [obj];
|
|
await doc.loadInfo();
|
|
let sheet = doc.sheetsByIndex[0];
|
|
for (let index = 0; index < rows.length; index++) {
|
|
const row = rows[index];
|
|
await sheet.addRow(row);
|
|
}
|
|
if(rows[0]['Gatnayak'] == tempData[0].email ) {res.send({'status':200 , message:"Success." ,'data': "success" , 'statusCode' : 1 }); }
|
|
else{res.send({'status':200 , message:"Success." ,'data': "success", 'statusCode' : 0 }); }
|
|
number++;
|
|
} catch(err) {
|
|
res.status(500).send({'status':404,
|
|
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
|
|
}); } //end of try catch
|
|
|
|
};
|
|
|
|
exports.updateRow = async (req, res ) =>
|
|
{
|
|
try
|
|
{
|
|
Olddata = req.body.Old;
|
|
Newdata = req.body.New;
|
|
// console.log('old'+Olddata[1]);// this will give old number same
|
|
// console.log('new'+Newdata[1]);// this will give new number sam
|
|
Old = {}; New = {};
|
|
|
|
column_name.forEach(async(element , index) => {
|
|
Object.assign( Old , { [element] : Olddata[index]});//oldata
|
|
Object.assign( New , { [element] : Newdata[index]});//newdata
|
|
});
|
|
arrKeys = [ ];
|
|
arrKeys = Object.keys(Old);//keys -> heading values -> datas
|
|
var updateVal = [];
|
|
for (let i = 0; i < arrKeys.length ; i++) {
|
|
if (Old[arrKeys[i]] != New[arrKeys[i]]) {
|
|
// if (Old[arrKeys[i]] == New[arrKeys[i]]) {
|
|
obj = { "keyValue" : arrKeys[i] , oldValue : Old[arrKeys[i]] , newValue : New[arrKeys[i]] };
|
|
updateVal.push(obj)
|
|
obj = { };
|
|
}
|
|
}
|
|
console.log(updateVal);
|
|
//console.log(updateVal[0].newValue); // this is the value for compare and update the value in sheet
|
|
// load the documents info
|
|
await doc.loadInfo();
|
|
// Index of the sheet
|
|
let sheet = doc.sheetsByIndex[0]; //it will detials of googlesheet
|
|
let rows = await sheet.getRows();
|
|
// console.log(rows[0]._rawData); //this is give the googlesheet values by changin the [] value to get different
|
|
updateVal.forEach(async(element) => {
|
|
// console.log(rows.length);//check in
|
|
// console.log(rows[0]._rawData[4]); //this will get the phone number
|
|
for (let index = 0; index < rows.length; index++) {
|
|
const row = rows[index]._rawData[1];
|
|
|
|
// console.log("Row->"+row);
|
|
// if (updateVal[0].newValue) {
|
|
//if (row[element.keyValue] === element.oldValue) {//row[element.keyValue]-> categry value one by one
|
|
if(Newdata[1]==row){
|
|
rows[index][element.keyValue] = element.newValue;
|
|
await rows[index].save();
|
|
break;
|
|
}
|
|
// }
|
|
};
|
|
});
|
|
res.send({'status':200 , message:"Success." ,'data': "Msg"})
|
|
} catch(err) {
|
|
res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|
|
(async function ( req , res ) {
|
|
const private_keyy = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');
|
|
|
|
// spreadsheet key is the long id in the sheets URL
|
|
const RESPONSES_SHEET_ID = process.env.RESPONSES_SHEET_ID;
|
|
// Create a new document
|
|
doc = new GoogleSpreadsheet(RESPONSES_SHEET_ID);
|
|
// use service account creds
|
|
await doc.useServiceAccountAuth({ client_email: process.env.CLIENT_EMAIL , private_key: private_keyy });
|
|
// load the documents info
|
|
await doc.loadInfo();
|
|
// Index of the sheet
|
|
let sheet = doc.sheetsByIndex[0];
|
|
// Get all the rows
|
|
let rows = await sheet.getRows();
|
|
})();
|
|
|