Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding event functions #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 253 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,256 @@ app.post('/', isAuthenticated ,function(req,response)
});

exports.signUp = functions.https.onRequest(app);





const db = admin.database().ref();

exports.addEvent = functions.https.onRequest((req,res) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comments? Please explain this function in comments


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;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make generic body design of event data. Many new fields can be introduced later on. So accept form data as json and parse it here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okkk

db.child(`events/${eventCategory}/${eventName}`).set({

name : eventName,
startTime : startTime,
endTime : endTime
})
.then((snapshot) => {
console.log('done')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify the success message to "Added {event} to timeline successfully".

}).catch((err) => {
res.send(err)
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appropriate error statement: Error occurred while adding to timeline


db.child(`eventDescription/${eventCategory}/${eventName}`).set({

name : eventName,
eventDescription : eventDescription,
startTime : startTime,
endTime : endTime
}).then((snapshot) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to generic implementation

return res.send(snapshot.val())
}).catch((err) => {
return res.send(err)
})
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appropriate log messages.


exports.getCategories = functions.https.onRequest((req,res) => {

return db.child('events').once('value')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hard-code these strings. Declare global variables for references.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to declare them global . this is the general uniform format of writing code .

.then((snapshot) => {

var data = {categories : []}
for(var i in snapshot.val())
{
var obj = {};
obj.name = i;
data.categories.push(obj);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response should be like :
{
"categories": ["a", "b"]
}

}
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')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hard-code these strings. Declare global variables for references.

.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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this as previous: {
"managerial": ["a", "b"],
"programming" : ["a","b"]
}

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cat? change it

return db.child(`events/${cat}`).once('value')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hard-code these strings. Declare global variables for references.

.then((snapshot) => {

return res.send(snapshot);
})
}
else {
return res.send("Invalid parameters.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print parameter also.

}
})

exports.getEventDescription = functions.https.onRequest((req,res) => {

if(req.query.events == 'all')
Copy link
Owner

@devgrpnitkkr devgrpnitkkr Sep 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. Dead code. Never all events are required. Use cases are:

  1. Single event
  2. All events for one category.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok

{
return db.child('eventDescription').once('value')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hard-code these strings. Declare global variables for references.

.then((snapshot) => {

var data = {eventDesciption : []}
var database = snapshot.val();

for(var category in snapshot.val()){

var events=database[category];

for(var event in events)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build separate function for this

{
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no processing required

})
.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.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print event category and event name values

}

db.child(`eventDescription/${eventCategory}/${eventName}`).once('value')
.then((snapshot) => {
if(snapshot == null) {
return res.send("Event Doesn't Exist.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print values

}
return res.send(snapshot.val())
})
.catch((err) => {
return res.send(err)
})
}
else if(req.query.events == 'cat')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cat? and hardcoded?

{
let categoryName = req.query.eventCategory;
if(categoryName == null)
{
return res.send("Insufficient Parameters.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change error message.

}

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)
})
})


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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require statements on top.

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to log complete 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,',');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make seperate function for this and rename this variable.

console.log(email);
let node = "userRegistrations/"+ eventName;
console.log(node);
// let db = database.ref();
let db = admin.database().ref();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove


db.child(`${node}`).push(email);

db.child("users/" + email + "/events").once('value')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registeredEvents

.then((snapshot) => {
let eventString = snapshot.val();

let newEventString = null;
if(eventString == null)
{
newEventString = eventName;
}
else
{
newEventString = eventString + ", " + eventName;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove space

}


db.child("/users/" + email).update({
"events": newEventString
})
.then(() => {
response.send("Registered");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return json

})
.catch((error) => {
console.log(error);
response.send("Could not Register!");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json

})


})
.catch(() => {
console.log(error);
response.send("Error");
})
});
});