Add complete commit endpoint

This commit is contained in:
Berkeley Martinez
2015-10-06 19:49:38 -07:00
parent 0b716c0057
commit 49c06b266e
3 changed files with 51 additions and 3 deletions

View File

@ -2,7 +2,9 @@ import _ from 'lodash';
import { Observable } from 'rx'; import { Observable } from 'rx';
import debugFactory from 'debug'; import debugFactory from 'debug';
import dedent from 'dedent'; import dedent from 'dedent';
import nonprofits from '../utils/commit.json'; import nonprofits from '../utils/commit.json';
import commitGoals from '../utils/commit-goals.json';
import { import {
unDasherize unDasherize
@ -57,6 +59,12 @@ export default function commit(app) {
stopCommit stopCommit
); );
router.post(
'/commit/complete-goal',
sendNonUserToCommit,
completeCommitment
);
app.use(router); app.use(router);
function commitToNonprofit(req, res, next) { function commitToNonprofit(req, res, next) {
@ -90,7 +98,9 @@ export default function commit(app) {
Object.assign( Object.assign(
{ {
title: 'Commit to a nonprofit. Commit to your goal.', title: 'Commit to a nonprofit. Commit to your goal.',
pledge pledge,
frontEndCert: commitGoals.frontEndCert,
fullStackCert: commitGoals.fullStackCert
}, },
nonprofit nonprofit
) )
@ -154,6 +164,40 @@ export default function commit(app) {
); );
} }
function completeCommitment(req, res, next) {
const { user } = req;
const { isFrontEndCert, isFullStackCert } = user;
observeQuery(user, 'pledge')
.flatMap(pledge => {
const { goal } = pledge;
if (!pledge) {
return Observable.just('No pledge found');
}
if (
isFrontEndCert && goal === commitGoals.frontEndCert ||
isFullStackCert && goal === commitGoals.fullStackCert
) {
pledge.isCompleted = true;
pledge.dateEnded = new Date();
return saveInstance(pledge);
}
return Observable.just(dedent`
You have not yet reached your goal of completing the ${goal}
Please retry when you have met the requirements.
`);
})
.subscribe(
msgOrPledge => {
if (typeof msgOrPledge === 'string') {
return res.send(msgOrPledge);
}
return res.send(true);
},
next
);
}
function stopCommit(req, res, next) { function stopCommit(req, res, next) {
const { user } = req; const { user } = req;

View File

@ -0,0 +1,4 @@
{
"frontEndCert": "front-end-development-certificate",
"fullStackCert": "full-stack-development-certificate"
}

View File

@ -23,10 +23,10 @@ block content
h4 Choose your goal: h4 Choose your goal:
.btn-group.btn-group-justified(data-toggle='buttons' role='group') .btn-group.btn-group-justified(data-toggle='buttons' role='group')
label.btn.btn-primary.btn-lg.active label.btn.btn-primary.btn-lg.active
input(type='radio' id='front-end-development-certificate' value='Front End Development Certification' name='goal' checked="checked") input(type='radio' id=frontEndCert value='Front End Development Certification' name='goal' checked="checked")
| Front End Development Certificate (takes about 400 hours) | Front End Development Certificate (takes about 400 hours)
label.btn.btn-primary.btn-lg label.btn.btn-primary.btn-lg
input(type='radio' id='full-stack-development-certificate' value='Full Stack Development Certification' name='goal') input(type='radio' id=fullStackCert value='Full Stack Development Certification' name='goal')
| Full Stack Development Certificate (takes about 800 hours) | Full Stack Development Certificate (takes about 800 hours)
.spacer .spacer
.row .row