| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | import _ from 'lodash'; | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  | import { Observable } from 'rx'; | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | import debugFactory from 'debug'; | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  | import dedent from 'dedent'; | 
					
						
							| 
									
										
										
										
											2015-10-06 19:49:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | import nonprofits from '../utils/commit.json'; | 
					
						
							| 
									
										
										
										
											2015-10-06 21:08:24 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   commitGoals, | 
					
						
							|  |  |  |   completeCommitment$ | 
					
						
							|  |  |  | } from '../utils/commit'; | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   unDasherize | 
					
						
							|  |  |  | } from '../utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   observeQuery, | 
					
						
							|  |  |  |   saveInstance | 
					
						
							|  |  |  | } from '../utils/rx'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   ifNoUserRedirectTo | 
					
						
							|  |  |  | } from '../utils/middleware'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-28 12:41:37 -08:00
										 |  |  | const sendNonUserToSignIn = ifNoUserRedirectTo( | 
					
						
							| 
									
										
										
										
											2016-05-03 15:06:20 +00:00
										 |  |  |   '/signin', | 
					
						
							| 
									
										
										
										
											2015-12-29 11:43:06 -08:00
										 |  |  |   'You must be signed in to commit to a nonprofit.', | 
					
						
							|  |  |  |   'info' | 
					
						
							| 
									
										
										
										
											2015-12-28 12:41:37 -08:00
										 |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  | const sendNonUserToCommit = ifNoUserRedirectTo( | 
					
						
							|  |  |  |   '/commit', | 
					
						
							| 
									
										
										
										
											2015-12-29 11:43:06 -08:00
										 |  |  |   'You must be signed in to update commit', | 
					
						
							|  |  |  |   'info' | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2015-12-28 12:41:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | const debug = debugFactory('fcc:commit'); | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  | function findNonprofit(name) { | 
					
						
							|  |  |  |   let nonprofit; | 
					
						
							|  |  |  |   if (name) { | 
					
						
							|  |  |  |     nonprofit = _.find(nonprofits, (nonprofit) => { | 
					
						
							|  |  |  |       return name === nonprofit.name; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 17:40:20 -08:00
										 |  |  |   nonprofit = nonprofit || nonprofits[ _.random(0, nonprofits.length - 1) ]; | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  |   return nonprofit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  | export default function commit(app) { | 
					
						
							|  |  |  |   const router = app.loopback.Router(); | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   const api = app.loopback.Router(); | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |   const { Pledge } = app.models; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  |   router.get( | 
					
						
							|  |  |  |     '/commit', | 
					
						
							|  |  |  |     commitToNonprofit | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |   router.get( | 
					
						
							|  |  |  |     '/commit/pledge', | 
					
						
							| 
									
										
										
										
											2015-12-28 12:41:37 -08:00
										 |  |  |     sendNonUserToSignIn, | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |     pledge | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 00:05:26 -07:00
										 |  |  |   router.get( | 
					
						
							|  |  |  |     '/commit/directory', | 
					
						
							|  |  |  |     renderDirectory | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  |     '/commit/stop-commitment', | 
					
						
							|  |  |  |     sendNonUserToCommit, | 
					
						
							|  |  |  |     stopCommit | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2015-10-06 19:49:38 -07:00
										 |  |  |     '/commit/complete-goal', | 
					
						
							|  |  |  |     sendNonUserToCommit, | 
					
						
							|  |  |  |     completeCommitment | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  |   app.use(api); | 
					
						
							| 
									
										
										
										
											2016-07-05 18:37:35 -07:00
										 |  |  |   app.use('/:lang', router); | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |   function commitToNonprofit(req, res, next) { | 
					
						
							|  |  |  |     const { user } = req; | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |     let nonprofitName = unDasherize(req.query.nonprofit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('looking for nonprofit', nonprofitName); | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |     const nonprofit = findNonprofit(nonprofitName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Observable.just(user) | 
					
						
							|  |  |  |       .flatMap(user => { | 
					
						
							|  |  |  |         if (user) { | 
					
						
							|  |  |  |           debug('getting user pledge'); | 
					
						
							|  |  |  |           return observeQuery(user, 'pledge'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return Observable.just(); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         pledge => { | 
					
						
							|  |  |  |           if (pledge) { | 
					
						
							|  |  |  |             debug('found previous pledge'); | 
					
						
							| 
									
										
										
										
											2018-01-12 14:16:33 -08:00
										 |  |  |             req.flash( | 
					
						
							|  |  |  |               'info', | 
					
						
							|  |  |  |               dedent`
 | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |                 Looks like you already have a pledge to ${pledge.displayName}. | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |                 Clicking "Commit" here will replace your old commitment. If you | 
					
						
							|  |  |  |                 do change your commitment, please remember to cancel your | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |                 previous recurring donation directly with ${pledge.displayName}. | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |               `
 | 
					
						
							| 
									
										
										
										
											2018-01-12 14:16:33 -08:00
										 |  |  |             ); | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |           } | 
					
						
							|  |  |  |           res.render( | 
					
						
							|  |  |  |             'commit/', | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |             { | 
					
						
							|  |  |  |               title: 'Commit to a nonprofit. Commit to your goal.', | 
					
						
							|  |  |  |               pledge, | 
					
						
							|  |  |  |               ...commitGoals, | 
					
						
							|  |  |  |               ...nonprofit | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-10-06 17:52:57 -07:00
										 |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |   function pledge(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |     const { user } = req; | 
					
						
							|  |  |  |     const { | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  |       nonprofit: nonprofitName = 'girl develop it', | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |       amount = '5', | 
					
						
							| 
									
										
										
										
											2017-12-21 01:15:23 +00:00
										 |  |  |       goal = commitGoals.respWebDesignCert | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |     } = req.query; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  |     const nonprofit = findNonprofit(nonprofitName); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |     observeQuery(user, 'pledge') | 
					
						
							|  |  |  |       .flatMap(oldPledge => { | 
					
						
							|  |  |  |         // create new pledge for user
 | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  |         const pledge = Pledge( | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |           { | 
					
						
							|  |  |  |             amount, | 
					
						
							|  |  |  |             goal, | 
					
						
							|  |  |  |             userId: user.id, | 
					
						
							|  |  |  |             ...nonprofit | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2015-10-06 14:54:59 -07:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (oldPledge) { | 
					
						
							|  |  |  |           debug('user already has pledge, creating a new one'); | 
					
						
							|  |  |  |           // we orphan last pledge since a user only has one pledge at a time
 | 
					
						
							|  |  |  |           oldPledge.userId = ''; | 
					
						
							|  |  |  |           oldPledge.formerUser = user.id; | 
					
						
							|  |  |  |           oldPledge.endDate = new Date(); | 
					
						
							|  |  |  |           oldPledge.isOrphaned = true; | 
					
						
							|  |  |  |           return saveInstance(oldPledge) | 
					
						
							|  |  |  |             .flatMap(() => { | 
					
						
							|  |  |  |               return saveInstance(pledge); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return saveInstance(pledge); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							| 
									
										
										
										
											2015-10-07 02:11:23 -07:00
										 |  |  |         ({ displayName, goal, amount }) => { | 
					
						
							| 
									
										
										
										
											2018-01-12 14:16:33 -08:00
										 |  |  |           req.flash( | 
					
						
							|  |  |  |             'success', | 
					
						
							|  |  |  |             dedent`
 | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |               Congratulations, you have committed to giving | 
					
						
							| 
									
										
										
										
											2015-10-07 02:11:23 -07:00
										 |  |  |               ${displayName} $${amount} each month until you have completed | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |               your ${goal}. Please remember to cancel your pledge directly | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |               with ${displayName} once you finish. | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |             `
 | 
					
						
							| 
									
										
										
										
											2018-01-12 14:16:33 -08:00
										 |  |  |           ); | 
					
						
							| 
									
										
										
										
											2015-10-06 12:27:53 -07:00
										 |  |  |           res.redirect('/' + user.username); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2015-10-06 00:13:51 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 00:05:26 -07:00
										 |  |  |   function renderDirectory(req, res) { | 
					
						
							|  |  |  |     res.render('commit/directory', { | 
					
						
							|  |  |  |       title: 'Commit Directory', | 
					
						
							|  |  |  |       nonprofits | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 19:49:38 -07:00
										 |  |  |   function completeCommitment(req, res, next) { | 
					
						
							|  |  |  |     const { user } = req; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 21:08:24 -07:00
										 |  |  |     return completeCommitment$(user) | 
					
						
							| 
									
										
										
										
											2015-10-06 19:49:38 -07:00
										 |  |  |       .subscribe( | 
					
						
							|  |  |  |         msgOrPledge => { | 
					
						
							|  |  |  |           if (typeof msgOrPledge === 'string') { | 
					
						
							|  |  |  |             return res.send(msgOrPledge); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           return res.send(true); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  |   function stopCommit(req, res, next) { | 
					
						
							|  |  |  |     const { user } = req; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     observeQuery(user, 'pledge') | 
					
						
							|  |  |  |       .flatMap(pledge => { | 
					
						
							|  |  |  |         if (!pledge) { | 
					
						
							|  |  |  |           return Observable.just(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pledge.formerUserId = pledge.userId; | 
					
						
							|  |  |  |         pledge.userId = null; | 
					
						
							|  |  |  |         pledge.isOrphaned = true; | 
					
						
							|  |  |  |         pledge.dateEnded = new Date(); | 
					
						
							|  |  |  |         return saveInstance(pledge); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         pledge => { | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |           let msg = dedent`
 | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |             You have successfully stopped your pledge. Please | 
					
						
							|  |  |  |             remember to cancel your recurring donation directly | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |             with the nonprofit if you haven't already done so. | 
					
						
							|  |  |  |           `;
 | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  |           if (!pledge) { | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |             msg = dedent`
 | 
					
						
							| 
									
										
										
										
											2016-12-29 16:51:51 -08:00
										 |  |  |               It doesn't look like you had an active pledge, so | 
					
						
							| 
									
										
										
										
											2016-07-01 00:25:01 -07:00
										 |  |  |               there's no pledge to stop. | 
					
						
							|  |  |  |             `;
 | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-01-12 14:16:33 -08:00
										 |  |  |           req.flash('info', msg); | 
					
						
							| 
									
										
										
										
											2015-10-06 23:33:17 -07:00
										 |  |  |           return res.redirect(`/${user.username}`); | 
					
						
							| 
									
										
										
										
											2015-10-06 18:22:12 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-05 21:13:11 -07:00
										 |  |  | } |