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 1 commit
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
73 changes: 73 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
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");
})
});
});