const { sequelize,Test} = require('../models') const { logMsg } = require('../services/logger.js'); const { GoogleSpreadsheet } = require('google-spreadsheet'); const fs = require('fs'); require('dotenv').config(); let dropdown = []; let column_name; let doc; // use in addRow and getRow api let tempData = []; exports.Welcome = (req, res) => { try { const Msg = "Welcome....!"; 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.getRow = async (req, res) => { try { email = req.query.email; // arr for ejs const empData = []; // clear array tempData = []; // clear temp data tempData = []; await doc.loadInfo(); // Index of the sheet let sheet = doc.sheetsByIndex[0]; // Get all the rows let rows = await sheet.getRows(); // dropdown array for each dropdown column category = []; age_Category = []; basthi = []; varga = []; milan_shaka = []; poruppu = []; blood_group = []; ganavesh_2w_4w_basg = []; new_index = 0; category.push(''); age_Category.push(''); basthi.push(''); varga.push(''); milan_shaka.push(''); poruppu.push(''); blood_group.push(''); ganavesh_2w_4w_basg.push('') for (let index = 0; index < rows.length; index++) { const row = rows[index]; category.push(row['Çategory']); 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]; // get sheet column name for dynamic insertion if(index == 0){ column_name = row._sheet.headerValues; } if (row.email == email) { // store login person data from sheet because get person id that is used to match other person manager_id var obj = {} for (let i = 0; i < column_name.length; i++){ // get email position in dynamic array 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]; if (row.email == email || row.Gatnayak == tempData[0]['email'] ) { var obj = {} for (let i = 0; i < column_name.length; i++){ // get email position in dynamic array Object.assign( obj , { [column_name[i]] : row[column_name[i]] }); } empData.push(obj); } }; res.render( 'displaydata' , { "data" : empData , "dropdown" : dropdown} , function (err, html ) { res.send(html) }); } else { res.send('

No Data

'); } } catch(err) { res.send({'status':404,message: err.message || "Some error occurred while retrieving data." ,'data': "No data" }); } //end of try catch }; exports.addRow = async (req, res) => { try { newData = req.body.data; // key value mapping var obj = {} for (let i = 0; i < column_name.length; i++){ // get email position in dynamic array Object.assign( obj , { [column_name[i]] : newData[i] }); } let rows = [obj]; // load the documents info await doc.loadInfo(); // Index of the sheet 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]['Sl.'] ) { res.send({'status':200 , message:"Success." ,'data': "success" , 'statusCode' : 1 }); } else{ res.send({'status':200 , message:"Success." ,'data': "success", 'statusCode' : 0 }); } } 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; Old = {}; New = {}; column_name.forEach(async(element , index) => { Object.assign( Old , { [element] : Olddata[index]}); Object.assign( New , { [element] : Newdata[index]}); }); arrKeys = [ ]; arrKeys = Object.keys(Old) var updateVal = []; for (let i = 0; i < arrKeys.length ; 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); // load the documents info await doc.loadInfo(); // Index of the sheet let sheet = doc.sheetsByIndex[0]; let rows = await sheet.getRows(); updateVal.forEach(async(element) => { for (let index = 0; index < rows.length; index++) { const row = rows[index]; if (element.keyValue != "Sl.") { if (row[element.keyValue] === element.oldValue) { 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 ) { // 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: process.env.PRIVATE_KEY }); // 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(); })();