| 
									
										
										
										
											2016-04-07 13:00:51 -07:00
										 |  |  | import request from 'request'; | 
					
						
							| 
									
										
										
										
											2018-06-28 23:04:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  | import { homeLocation } from '../../../config/env'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 13:00:51 -07:00
										 |  |  | import constantStrings from '../utils/constantStrings.json'; | 
					
						
							| 
									
										
										
										
											2016-06-23 20:05:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const githubClient = process.env.GITHUB_ID; | 
					
						
							|  |  |  | const githubSecret = process.env.GITHUB_SECRET; | 
					
						
							| 
									
										
										
										
											2015-06-01 16:48:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  | module.exports = function(app) { | 
					
						
							| 
									
										
										
										
											2016-04-07 13:00:51 -07:00
										 |  |  |   const router = app.loopback.Router(); | 
					
						
							|  |  |  |   const User = app.models.User; | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   router.get('/api/github', githubCalls); | 
					
						
							| 
									
										
										
										
											2020-02-08 13:29:10 -05:00
										 |  |  |   router.get('/u/:email', unsubscribeDeprecated); | 
					
						
							|  |  |  |   router.get('/unsubscribe/:email', unsubscribeDeprecated); | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |   router.get('/ue/:unsubscribeId', unsubscribeById); | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |   router.get( | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |     '/the-fastest-web-page-on-the-internet', | 
					
						
							|  |  |  |     theFastestWebPageOnTheInternet | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |   router.get('/unsubscribed/:unsubscribeId', unsubscribedWithId); | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   router.get('/unsubscribed', unsubscribed); | 
					
						
							| 
									
										
										
										
											2020-03-06 17:51:58 +01:00
										 |  |  |   router.get('/resubscribe/:unsubscribeId', resubscribe); | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |   router.get('/nonprofits', nonprofits); | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   router.get('/coding-bootcamp-cost-calculator', bootcampCalculator); | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |   app.use(router); | 
					
						
							| 
									
										
										
										
											2015-06-03 16:31:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 10:23:35 -06:00
										 |  |  |   function theFastestWebPageOnTheInternet(req, res) { | 
					
						
							|  |  |  |     res.render('resources/the-fastest-web-page-on-the-internet', { | 
					
						
							|  |  |  |       title: 'This is the fastest web page on the internet' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-31 17:30:27 -07:00
										 |  |  |   function bootcampCalculator(req, res) { | 
					
						
							|  |  |  |     res.render('resources/calculator', { | 
					
						
							|  |  |  |       title: 'Coding Bootcamp Cost Calculator' | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-20 18:40:43 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |   function nonprofits(req, res) { | 
					
						
							|  |  |  |     res.render('resources/nonprofits', { | 
					
						
							| 
									
										
										
										
											2015-10-31 02:59:09 -07:00
										 |  |  |       title: 'Your Nonprofit Can Get Pro Bono Code' | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 13:29:10 -05:00
										 |  |  |   function unsubscribeDeprecated(req, res) { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |     req.flash( | 
					
						
							|  |  |  |       'info', | 
					
						
							|  |  |  |       'We are no longer able to process this unsubscription request. ' + | 
					
						
							|  |  |  |         'Please go to your settings to update your email preferences' | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     res.redirectWithFlash(homeLocation); | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:30:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |   function unsubscribeById(req, res, next) { | 
					
						
							|  |  |  |     const { unsubscribeId } = req.params; | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |     if (!unsubscribeId) { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |       req.flash('info', 'We could not find an account to unsubscribe'); | 
					
						
							|  |  |  |       return res.redirectWithFlash(homeLocation); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |     return User.find({ where: { unsubscribeId } }, (err, users) => { | 
					
						
							|  |  |  |       if (err || !users.length) { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |         req.flash('info', 'We could not find an account to unsubscribe'); | 
					
						
							|  |  |  |         return res.redirectWithFlash(homeLocation); | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |       const updates = users.map(user => { | 
					
						
							|  |  |  |         return new Promise((resolve, reject) => | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           user.updateAttributes( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               sendQuincyEmail: false | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             err => { | 
					
						
							|  |  |  |               if (err) { | 
					
						
							|  |  |  |                 reject(err); | 
					
						
							|  |  |  |               } else { | 
					
						
							|  |  |  |                 resolve(); | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           ) | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |         ); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return Promise.all(updates) | 
					
						
							|  |  |  |         .then(() => { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           req.flash( | 
					
						
							|  |  |  |             'success', | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |             "We've successfully updated your email preferences." | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           ); | 
					
						
							|  |  |  |           return res.redirectWithFlash( | 
					
						
							|  |  |  |             `${homeLocation}/unsubscribed/${unsubscribeId}` | 
					
						
							|  |  |  |           ); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:01:26 +05:30
										 |  |  |         }) | 
					
						
							|  |  |  |         .catch(next); | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |   function unsubscribed(req, res) { | 
					
						
							|  |  |  |     res.render('resources/unsubscribed', { | 
					
						
							|  |  |  |       title: 'You have been unsubscribed' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |   function unsubscribedWithId(req, res) { | 
					
						
							|  |  |  |     const { unsubscribeId } = req.params; | 
					
						
							|  |  |  |     return res.render('resources/unsubscribed', { | 
					
						
							|  |  |  |       title: 'You have been unsubscribed', | 
					
						
							|  |  |  |       unsubscribeId | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function resubscribe(req, res, next) { | 
					
						
							| 
									
										
										
										
											2018-08-03 01:28:49 +05:30
										 |  |  |     const { unsubscribeId } = req.params; | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |     if (!unsubscribeId) { | 
					
						
							|  |  |  |       req.flash( | 
					
						
							|  |  |  |         'info', | 
					
						
							|  |  |  |         'We we unable to process this request, please check and try againÍ' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       res.redirect(homeLocation); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return User.find({ where: { unsubscribeId } }, (err, users) => { | 
					
						
							|  |  |  |       if (err || !users.length) { | 
					
						
							|  |  |  |         req.flash('info', 'We could not find an account to resubscribe'); | 
					
						
							|  |  |  |         return res.redirectWithFlash(homeLocation); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const [user] = users; | 
					
						
							|  |  |  |       return new Promise((resolve, reject) => | 
					
						
							|  |  |  |         user.updateAttributes( | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |             sendQuincyEmail: true | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           }, | 
					
						
							|  |  |  |           err => { | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |             if (err) { | 
					
						
							|  |  |  |               reject(err); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               resolve(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |       ) | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |         .then(() => { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |           req.flash( | 
					
						
							|  |  |  |             'success', | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |             "We've successfully updated your email preferences. Thank you " + | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |               'for resubscribing.' | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |           return res.redirectWithFlash(homeLocation); | 
					
						
							| 
									
										
										
										
											2018-08-02 11:34:35 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         .catch(next); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |   function githubCalls(req, res, next) { | 
					
						
							|  |  |  |     var githubHeaders = { | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'User-Agent': constantStrings.gitHubUserAgent | 
					
						
							| 
									
										
										
										
											2015-06-01 18:30:43 -04:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |       port: 80 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     request( | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'https://api.github.com/repos/freecodecamp/', | 
					
						
							|  |  |  |         'freecodecamp/pulls?client_id=', | 
					
						
							| 
									
										
										
										
											2016-06-23 20:05:30 -07:00
										 |  |  |         githubClient, | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |         '&client_secret=', | 
					
						
							| 
									
										
										
										
											2016-06-23 20:05:30 -07:00
										 |  |  |         githubSecret | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |       ].join(''), | 
					
						
							|  |  |  |       githubHeaders, | 
					
						
							|  |  |  |       function(err, status1, pulls) { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |         if (err) { | 
					
						
							|  |  |  |           return next(err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         pulls = pulls | 
					
						
							|  |  |  |           ? Object.keys(JSON.parse(pulls)).length | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |           : "Can't connect to github"; | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |         return request( | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             'https://api.github.com/repos/freecodecamp/', | 
					
						
							|  |  |  |             'freecodecamp/issues?client_id=', | 
					
						
							| 
									
										
										
										
											2016-06-23 20:05:30 -07:00
										 |  |  |             githubClient, | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |             '&client_secret=', | 
					
						
							| 
									
										
										
										
											2016-06-23 20:05:30 -07:00
										 |  |  |             githubSecret | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |           ].join(''), | 
					
						
							|  |  |  |           githubHeaders, | 
					
						
							| 
									
										
										
										
											2015-10-07 00:28:42 -07:00
										 |  |  |           function(err, status2, issues) { | 
					
						
							| 
									
										
										
										
											2018-09-06 14:59:52 +01:00
										 |  |  |             if (err) { | 
					
						
							|  |  |  |               return next(err); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             issues = | 
					
						
							|  |  |  |               pulls === parseInt(pulls, 10) && issues | 
					
						
							|  |  |  |                 ? Object.keys(JSON.parse(issues)).length - pulls | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |                 : "Can't connect to GitHub"; | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |             return res.send({ | 
					
						
							| 
									
										
										
										
											2015-06-03 16:19:23 -07:00
										 |  |  |               issues: issues, | 
					
						
							|  |  |  |               pulls: pulls | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; |