Skip to content

Commit

Permalink
Merge pull request #182 from jmg2107/master
Browse files Browse the repository at this point in the history
Code Cleanup
  • Loading branch information
heatherpark authored Aug 26, 2016
2 parents a10211c + 1a82163 commit cb8a507
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 198 deletions.
1 change: 0 additions & 1 deletion gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ gulp.task("libs", () => {
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'reflect-metadata/Reflect.js',
// 'rxjs/bundles/Rx.min.js',
'rxjs/**',
'zone.js/dist/**',
'@angular/common/bundles/common.umd.min.js',
Expand Down
134 changes: 63 additions & 71 deletions server/requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var plus = google.plus('v1');

//ENV object will be used during production. But, for development,
//a client_secret.json file should be created with your own ID keys
//and stored in the server folder. Follow the structure of the ENV
//variable for the client_secret.json file and export it.

var ENV = {
"web": {
"client_id": process.env.GOOGLE_CLIENT_ID,
Expand Down Expand Up @@ -64,37 +69,34 @@ module.exports = {
access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
scope: scopes // If you only need one scope you can pass it as string
});
console.log("url is ", url);
res.status(200).send(url);

},

googleRedirect: function(req, res, next){
//come here we get the redirect. ask for a token to store.
//use req.params.code
console.log('code ', req.query.code);
//use req.query.code

oauth2Client.getToken(req.query.code, function(err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if(!err) {
oauth2Client.setCredentials(tokens);
console.log("tokens are ", tokens);

plus.people.get({ userId: 'me', auth: oauth2Client}, function(err, response){
console.log("plus res ", response);

req.session.username = response.emails[0].value;
// currentEmail = response.emails[0].value;

db.getUserByEmail(req.session.username)
.then(function(user){
//if user doesn't exist, create new user
//and redirect to login
console.log('returned user, ', user);

if(!user){
db.createUser(req.session.username, 'excited')
.then(function(user){
//redirect to create calendar
console.log('created new user, ', user);

res.redirect('/api/calendar/create')
})
.catch(function(err){
Expand All @@ -105,16 +107,13 @@ module.exports = {
//redirect to '/'
if(user.relationshipId){
//save tokens
//{route: aldkfoiefj}
console.log('tokens are ', tokens);

db.User.get(user.id)
.update({token: tokens})
.run()
.then(function(user){
console.log('updated user ', user);
res.redirect('/');
});

} else {
//if user does exist and doesn't have relationship
//redirect to login
Expand All @@ -130,9 +129,55 @@ module.exports = {
}); //getToken
},

//Add partner to link with
googleJoin: function(req,res, next){

//add partner to the user's Smitten calendar to read/write
var calID;
//create new user with incoming email
//connect them to a relationship

db.createUser(req.body.email, 'excited')
.then(function(){
return db.getRelationshipByEmail(req.session.username);
})
.then(function(relationship){
db.updateUser(req.body.email, {relationshipId: relationship.id});
calID = relationship.calendarId;
})
.then(function(){

calendar.acl.insert({
auth: oauth2Client,
calendarId: calID,
resource: {
id: 'user:' + req.body.email,
role: 'writer',
scope: {
type: 'user',
value: req.body.email
}
}

}, function(err, event){
if(err){
console.log("calendar Join error: ", err);
}
res.status(201).send(calID);

});

});
},

googleLogout : function(req, res, next){
req.session.destroy();
res.status(200).send();
},

calendarCreate: function(req, res, next){
//create a new Smitten calendar for the logged in google user
console.log("in calendarCreate");

calendar.calendars.insert({
auth: oauth2Client,
resource: {
Expand All @@ -142,9 +187,8 @@ module.exports = {
if(err){
console.log("calendar error: ", err);
}
console.log("Smitten calendar created ", event);
//add calendarID "event.id" entry to Users table

//add calendarID "event.id" entry to Users table
//add the calendar id to a relationship
//attach that relationshipid to a user

Expand All @@ -159,84 +203,36 @@ module.exports = {

},

//redirected here when loading calendar page.
//retrieve ID from database and send back to front end
calendarId: function(req,res,next){
db.getRelationshipByEmail(req.session.username)
.then(function(relationship){
console.log('calId is ', relationship.calendarId);
var body = {user: req.session.username, calId: relationship.calendarId};
bodyString = JSON.stringify(body);
res.status(200).send(bodyString);
})
},

//redirected here to send a text message with Text Magic API
calendarText: function(req, res, next){
console.log("text body", req.body);
var time = req.body.time + '-07:00';
var UTC = Date.parse(time);
console.log("UTC ", UTC);
UTC = UTC/1000;
console.log("UTCsubstr ", UTC);

text.Messages.send({text: req.body.text, phones: req.body.phone, sendingTime: UTC}, function(err, response){
console.log('Messages.send()', err, response);
res.status(201).send(response);
});

},

googleJoin: function(req,res, next){

//add partner to the user's Smitten calendar to read/write
var calID;
console.log("req.body.email is ", req.body.email);
//create new user with incoming email
//connect them to a relationship

db.createUser(req.body.email, 'excited')
.then(function(){
return db.getRelationshipByEmail(req.session.username);
})
.then(function(relationship){
db.updateUser(req.body.email, {relationshipId: relationship.id});
calID = relationship.calendarId;
console.log("calID ", calID);
})
.then(function(){

calendar.acl.insert({
auth: oauth2Client,
calendarId: calID,
resource: {
id: 'user:' + req.body.email,
role: 'writer',
scope: {
type: 'user',
value: req.body.email
}
}

}, function(err, event){
if(err){
console.log("calendar Join error: ", err);
}
console.log("insert user");
console.log("calID after user permissions ", calID);
res.status(201).send(calID);

});

});
},

calendarEventAdd : function(req, res, next){
//Add events to the Smitten Calendar
var calId;
console.log("event ", req.body);

db.getRelationshipByEmail(req.session.username)
.then(function(relationship){
calId = relationship.calendarId;
console.log("calId ", calId);
})
.then(function(){
calendar.events.insert({
Expand All @@ -248,14 +244,10 @@ module.exports = {
console.log('There was an error contacting the Calendar service: ' + err);
return;
}
console.log('Event created: %s', event.htmlLink);
res.status(201).send(event.htmlLink);
});
});
},

googleLogout : function(req, res, next){
req.session.destroy();
res.status(200).send();
}


};
9 changes: 4 additions & 5 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ var mixtapeRequestHandler = require('./request-handlers/mixtape.js');
var sentimentRequestHandler = require('./sentimentRequestHandler.js');

module.exports = function (app, express){

/** google routes **/
app.get('/api/google/login', requestHandler.googleLogin);
app.get('/googleOAuth/', requestHandler.googleRedirect);
app.post('/api/google/join', requestHandler.googleJoin);
app.get('/api/google/logout', requestHandler.googleLogout);

/** calendar routes **/
app.get('/api/calendar/create', requestHandler.calendarCreate);
app.post('/api/calendar/eventAdd', requestHandler.calendarEventAdd);
app.get('/api/calendar/calId', requestHandler.calendarId);
app.post('/api/calendar/text', requestHandler.calendarText);

/** post-it routes **/
app.post('/api/posts/:email', postRequestHandler.createPost);
app.get('/api/posts/relationship/:email', postRequestHandler.getRelationshipPosts);
app.delete('/api/posts/:id', postRequestHandler.deletePost);
Expand All @@ -28,10 +32,5 @@ module.exports = function (app, express){
/** sentiment **/
app.get('/api/sentiment', sentimentRequestHandler.sendBackData);

//tester routes
// app.post('/api/users', postRequestHandler.createUser);
// app.put('/api/users', postRequestHandler.updateUser);
// app.post('/api/relationship/:calendarId', postRequestHandler.createRelationship);
// app.get('/api/relationship/:email', postRequestHandler.getRelationship);

};
15 changes: 8 additions & 7 deletions src/app/containers/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import { Dialog } from 'primeng/primeng';
</li>
</ul>
</div>
<div>
<p-dialog header="Your event has been added!" [(visible)]="eventmade" modal="modal" showEffect="fade">
</p-dialog>
</div>
<div>
<p-dialog header="Your text has been sent!" [(visible)]="textsent" modal="modal" showEffect="fade">
</p-dialog>
Expand All @@ -47,16 +53,14 @@ export class Calendar {
calSrc = "";
trustedUrl;
textsent: boolean = false;
eventmade: boolean = false;
calLoaded: boolean = false;


loadCalendar() {
this.calendarService.getCalendarId()
.subscribe(res => {
console.log("body ", res._body);
var body = JSON.parse(res._body);
console.log("calendarId ", body["calId"]);
console.log("user ", body["user"]);
window.localStorage.setItem("user", body["user"]);
this.calSrc = "https://calendar.google.com/calendar/embed?src=" + body["calId"];
this.trustedUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.calSrc);
Expand All @@ -70,19 +74,16 @@ export class Calendar {


onEmitAddition(event: Object) {
console.log("hit onEmitAddition");
this.calendarService.addCalendarEvent(event)
.subscribe(res => {
console.log("event added ", res._body);
this.loadCalendar();
this.eventmade = true;
});
}

onEmitText(event: Object) {
console.log("hit onEmitText");
this.calendarService.addText(event)
.subscribe(res => {
console.log("text sent ", res._body);
this.textsent = true;
});
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/containers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import { LoginService } from '../services/index';
constructor (private loginService: LoginService) {}

onEmitLogin(event: Object) {
console.log("hit onEmitLogin");
this.loginService.googleLogin(event);
};

onEmitPartner(event: Object) {
console.log("hit onEmitPartner");
this.loginService.googlePartner(event);
};

Expand Down
1 change: 0 additions & 1 deletion src/app/containers/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import { Button } from 'primeng/primeng';
private loginService: LoginService) {
this.loginService.logout()
.subscribe(res => {
console.log("logged out");
});
}

Expand Down
5 changes: 0 additions & 5 deletions src/app/containers/wishes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ export class Wishes {
private posts = [];

constructor(private postService: PostService) {
console.log('constructor');
this.postService.getRelationshipPosts(window.localStorage.getItem('user'))
.subscribe(res => {
let newPosts = res.json();
console.log(newPosts);
newPosts.forEach(function(post, index){
post.index = index;
console.log(post);
});
this.posts = newPosts;
});
}

onCreatePost(post) {
console.log('old post', post);
this.postService.createPost(window.localStorage.getItem('user'), post)
.subscribe(res => {
var post = res.json();
Expand All @@ -51,7 +47,6 @@ export class Wishes {
}

onPostCompletion(post) {
console.log('hit post completion');
this.postService.deletePost(post.id)
.subscribe(res => console.log(res));
this.posts.splice(post.index, 1);
Expand Down
1 change: 0 additions & 1 deletion src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { App } from './app';
export { routes } from './routes';
export { Store } from './store';
// export { WishService, CalendarService, ApiService } from './services/index';
export { Wishes, Main, Calendar, Login } from './containers/index';
export { PostCard, CalendarInput } from './ui/index';

Expand Down
Loading

0 comments on commit cb8a507

Please sign in to comment.