From 74c1399c8a94ab01ed4ceb9cc99866f294a3ab0a Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 29 Sep 2018 23:16:05 +0530 Subject: [PATCH 1/3] adding event functions Signed-off-by: Gaurav --- functions/index.js | 180 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/functions/index.js b/functions/index.js index 4b078a9d..920e7246 100644 --- a/functions/index.js +++ b/functions/index.js @@ -143,3 +143,183 @@ app.post('/', isAuthenticated ,function(req,response) }); exports.signUp = functions.https.onRequest(app); + + + + + +const db = admin.database().ref(); + +exports.addEvent = functions.https.onRequest((req,res) => { + + let eventCategory = req.body.eventCategory; + let eventName = req.body.eventName; + let startTime = req.body.startTime; + let endTime = req.body.endTime; + let eventDescription = req.body.eventDescription; + + db.child(`events/${eventCategory}/${eventName}`).set({ + + name : eventName, + startTime : startTime, + endTime : endTime + }) + .then((snapshot) => { + console.log('done') + }).catch((err) => { + res.send(err) + }) + + db.child(`eventDescription/${eventCategory}/${eventName}`).set({ + + name : eventName, + eventDescription : eventDescription, + startTime : startTime, + endTime : endTime + }).then((snapshot) => { + return res.send(snapshot.val()) + }).catch((err) => { + return res.send(err) + }) +}) + +exports.getCategories = functions.https.onRequest((req,res) => { + + return db.child('events').once('value') + .then((snapshot) => { + + var data = {categories : []} + for(var i in snapshot.val()) + { + var obj = {}; + obj.name = i; + data.categories.push(obj); + } + return res.send(data); + }) + .catch((err) => { + return res.send(err); + }) +}) + +exports.getEventNames = functions.https.onRequest((req,res) => { + + if(req.query.category == 'all') + { + return db.child('events').once('value') + .then((snapshot) => { + + var data = {events : []} + var database=snapshot.val(); + for(var category in database) + { + for(let event in database[category]) + { + var obj = {}; + obj.category = category, + obj.name = database[category][event].name; + data.events.push(obj); + } + } + + return res.send(data); + }) + .catch((err) => { + return res.send(err); + }) + } + else if(req.query.category == 'one') + { + let cat = req.query.eventCategory; + return db.child(`events/${cat}`).once('value') + .then((snapshot) => { + + return res.send(snapshot); + }) + } + else { + return res.send("Invalid parameters."); + } +}) + +exports.getEventDescription = functions.https.onRequest((req,res) => { + + if(req.query.events == 'all') + { + return db.child('eventDescription').once('value') + .then((snapshot) => { + + var data = {eventDesciption : []} + var database = snapshot.val(); + + for(var category in snapshot.val()){ + + var events=database[category]; + + for(var event in events) + { + var obj = {}; + obj.category = category; + obj.name = events[event].name; + obj.eventDescription = events[event].eventDescription; + obj.startTime = events[event].startTime; + obj.endTime = events[event].endTime; + data.eventDesciption.push(obj); + } + } + return res.send(data); + }) + .catch((err) => { + return res.send(err) + }) + } + else if(req.query.events == 'one') + { + let eventCategory = req.query.eventCategory + let eventName = req.query.eventName + + if(eventCategory == null || eventName == null) { + res.send("Insufficient parameters.") + } + + db.child(`eventDescription/${eventCategory}/${eventName}`).once('value') + .then((snapshot) => { + if(snapshot == null) { + return res.send("Event Doesn't Exist."); + } + return res.send(snapshot.val()) + }) + .catch((err) => { + return res.send(err) + }) + } + else if(req.query.events == 'cat') + { + let categoryName = req.query.eventCategory; + if(categoryName == null) + { + return res.send("Insufficient Parameters."); + } + + db.child(`eventDescription/${categoryName}`).once('value') + .then((snapshot) => { + if(snapshot == null) { + return res.send("Invalid Category."); + } + + return res.send(snapshot.val()); + }) + } + +}) + +exports.getEventTimeline = functions.https.onRequest((req,res) => { + + return db.child('events').once('value') + .then((snapshot) => { + return res.send(snapshot.val()) + }) + .catch((err) => { + return res.send(err) + }) +}) From 6ee4256a4cb95df1764350a73bcbbb342ab802e6 Mon Sep 17 00:00:00 2001 From: Harshita Aggarwal Date: Sun, 30 Sep 2018 21:59:11 +0530 Subject: [PATCH 2/3] Adding Event Registration functionality --- functions/index.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/functions/index.js b/functions/index.js index 920e7246..8a10952b 100644 --- a/functions/index.js +++ b/functions/index.js @@ -323,3 +323,76 @@ exports.getEventTimeline = functions.https.onRequest((req,res) => { return res.send(err) }) }) + + +exports.eventRegister = functions.https.onRequest((req, response) => { + let access_token = req.query.accessToken; + let eventName = req.query.event; + + if(access_token == null || eventName == null) + { + response.send("Invalid Parameters."); + } + + const request = require('request'); + request('https://www.googleapis.com/plus/v1/people/me?access_token='+access_token, { json: true }, (err, res, body) => { + let data; + if(err) + { + return console.log(err); + } + console.log(body); + if(body.error != null) + { + console.log(body.error); + console.log('error in accessToken'); + data={ + authenticatedRequest:false, + }; + response.json(data); + } + + let email1 = body.emails[0].value; + let email = email1.replace(/\./g,','); + console.log(email); + let node = "userRegistrations/"+ eventName; + console.log(node); + // let db = database.ref(); + let db = admin.database().ref(); + + db.child(`${node}`).push(email); + + db.child("users/" + email + "/events").once('value') + .then((snapshot) => { + let eventString = snapshot.val(); + + let newEventString = null; + if(eventString == null) + { + newEventString = eventName; + } + else + { + newEventString = eventString + ", " + eventName; + } + + + db.child("/users/" + email).update({ + "events": newEventString + }) + .then(() => { + response.send("Registered"); + }) + .catch((error) => { + console.log(error); + response.send("Could not Register!"); + }) + + + }) + .catch(() => { + console.log(error); + response.send("Error"); + }) + }); +}); \ No newline at end of file From 37fb8d2196523ec63514aacb516b059ccf4d50d5 Mon Sep 17 00:00:00 2001 From: Harshita Aggarwal Date: Sun, 30 Sep 2018 23:11:04 +0530 Subject: [PATCH 3/3] Making Requested Changes to Event Registration functionality --- functions/index.js | 53 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/functions/index.js b/functions/index.js index 8a10952b..92bfd98d 100644 --- a/functions/index.js +++ b/functions/index.js @@ -1,14 +1,13 @@ const functions = require('firebase-functions'); const admin = require('firebase-admin'); const jwt = require('jsonwebtoken'); - +const request = require('request'); admin.initializeApp(); let database = admin.database(); //login start exports.googleLogin = functions.https.onRequest(function(req,response){ let accToken = req.query.accessToken; - const request = require('request'); request('https://www.googleapis.com/plus/v1/people/me?access_token='+accToken, { json: true }, (err, res, body) => { let data; if(err) @@ -331,17 +330,17 @@ exports.eventRegister = functions.https.onRequest((req, response) => { if(access_token == null || eventName == null) { - response.send("Invalid Parameters."); + response.json({"error":"Invalid Parameters. Need access token and event Name."}); } - const request = require('request'); + request('https://www.googleapis.com/plus/v1/people/me?access_token='+access_token, { json: true }, (err, res, body) => { let data; if(err) { return console.log(err); } - console.log(body); + if(body.error != null) { console.log(body.error); @@ -351,48 +350,52 @@ exports.eventRegister = functions.https.onRequest((req, response) => { }; response.json(data); } + + let email = getEmail(body); - let email1 = body.emails[0].value; - let email = email1.replace(/\./g,','); - console.log(email); let node = "userRegistrations/"+ eventName; - console.log(node); - // let db = database.ref(); - let db = admin.database().ref(); + db.child(`${node}`).push(email); - db.child("users/" + email + "/events").once('value') + db.child("users/" + email + "/registeredEvents").once('value') .then((snapshot) => { + let eventString = snapshot.val(); - let newEventString = null; + if(eventString == null) { newEventString = eventName; } else { - newEventString = eventString + ", " + eventName; + newEventString = eventString + "," + eventName; } - - db.child("/users/" + email).update({ - "events": newEventString + db.child("users/" + email).update({ + "registeredEvents": newEventString }) .then(() => { - response.send("Registered"); + response.json({status:"Successfully registered for "+eventName}); }) - .catch((error) => { - console.log(error); - response.send("Could not Register!"); + .catch((err) => { + console.log(err); + response.json({error:"Could not Register!"}); }) }) - .catch(() => { - console.log(error); - response.send("Error"); + .catch((err) => { + console.log(err); + response.json({"error":err}); }) }); -}); \ No newline at end of file +}); + +function getEmail(body) +{ + let email = body.emails[0].value; + let formattedEmail = email.replace(/\./g,','); + return formattedEmail; +} \ No newline at end of file