// 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('
No Data
'); // } } } res.end("