Add logic to load nonprofit on query

Add logic to model update
This commit is contained in:
Berkeley Martinez
2015-10-06 12:27:53 -07:00
parent bc6a9c6db2
commit c8da944caf
5 changed files with 129 additions and 50 deletions

View File

@ -1,16 +1,33 @@
import _ from 'lodash';
import debugFactory from 'debug';
import dedent from 'dedent';
import nonprofits from '../utils/commit.json';
import {
unDasherize
} from '../utils';
import {
observeQuery,
saveInstance
} from '../utils/rx';
import {
ifNoUserRedirectTo
} from '../utils/middleware';
const sendNonUserToFront = ifNoUserRedirectTo('/');
const debug = debugFactory('freecc:commit');
export default function commit(app) {
const router = app.loopback.Router();
const { Pledge } = app.models;
router.get(
'/commit',
commitToNonprofit
);
router.get(
'/commit/pledge',
sendNonUserToFront,
@ -20,27 +37,72 @@ export default function commit(app) {
app.use(router);
function commitToNonprofit(req, res) {
res.render('commit/', {
title: 'Commit to a nonprofit. Commit to your goal.'
});
let nonprofitName = unDasherize(req.query.nonprofit);
let nonprofit;
debug('looking for nonprofit', nonprofitName);
if (nonprofitName) {
nonprofit = _.find(nonprofits, (nonprofit) => {
return nonprofitName === nonprofit.name;
});
}
nonprofit = nonprofit || nonprofits[0];
res.render(
'commit/',
Object.assign(
{ title: 'Commit to a nonprofit. Commit to your goal.' },
nonprofit
)
);
}
function pledge(req, res) {
function pledge(req, res, next) {
const { user } = req;
const {
nonprofit = 'girl develop it',
amount = '5',
goal = 'front end certification'
goal = 'Front End Development Certification'
} = req.query;
req.flash('success', {
msg: dedent`
Congratulations, you have commit to giving ${nonprofit} ${amount}
dollars a month until you have reached your goal
of completing your ${goal}
`
});
observeQuery(user, 'pledge')
.flatMap(oldPledge => {
// create new pledge for user
const pledge = Pledge({
nonprofit,
amount,
goal,
userId: user.id
});
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(
({ nonprofit, goal, amount }) => {
req.flash('success', {
msg: dedent`
Congratulations, you have committed to giving
${nonprofit} $${amount} each month until you have completed
your ${goal}.
`
});
res.redirect('/' + user.username);
},
next
);
res.redirect('/' + user.username);
}
}