diff --git a/server/boot/certificate.js b/server/boot/certificate.js index b084d9f4b8..400d756bcc 100644 --- a/server/boot/certificate.js +++ b/server/boot/certificate.js @@ -18,6 +18,10 @@ import { fullStackChallangeId } from '../utils/constantStrings.json'; +import { + completeCommitment$ +} from '../utils/commit'; + const debug = debugFactory('freecc:certification'); const sendMessageToNonUser = ifNoUserSend( 'must be logged in to complete.' @@ -114,7 +118,20 @@ export default function certificate(app) { completedDate: new Date(), challengeType }); - return saveUser(user); + return saveUser(user) + // If user has commited to nonprofit, + // this will complete his pledge + .flatMap( + user => completeCommitment$(user), + (user, pledgeOrMessage) => { + if (typeof pledgeOrMessage === 'string') { + debug(pledgeOrMessage); + } + // we are only interested in the user object + // so we ignore return from completeCommitment$ + return user; + } + ); } return Observable.just(user); }) diff --git a/server/boot/commit.js b/server/boot/commit.js index b15a02468b..4b04950c63 100644 --- a/server/boot/commit.js +++ b/server/boot/commit.js @@ -4,7 +4,10 @@ import debugFactory from 'debug'; import dedent from 'dedent'; import nonprofits from '../utils/commit.json'; -import commitGoals from '../utils/commit-goals.json'; +import { + commitGoals, + completeCommitment$ +} from '../utils/commit'; import { unDasherize @@ -116,7 +119,7 @@ export default function commit(app) { const { nonprofit: nonprofitName = 'girl develop it', amount = '5', - goal = 'Front End Development Certification' + goal = commitGoals.frontEndCert } = req.query; const nonprofit = findNonprofit(nonprofitName); @@ -166,27 +169,8 @@ 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. - `); - }) + return completeCommitment$(user) .subscribe( msgOrPledge => { if (typeof msgOrPledge === 'string') { diff --git a/server/utils/commit.js b/server/utils/commit.js new file mode 100644 index 0000000000..6eb48d139b --- /dev/null +++ b/server/utils/commit.js @@ -0,0 +1,36 @@ +import dedent from 'dedent'; +import debugFactory from 'debug'; +import { Observable } from 'rx'; + +import commitGoals from './commit-goals.json'; +const debug = debugFactory('freecc:utils/commit'); + +export { commitGoals }; + +export function completeCommitment$(user) { + const { isFrontEndCert, isFullStackCert } = user; + return Observable.fromNodeCallback(user.pledge, user)() + .flatMap(pledge => { + if (!pledge) { + return Observable.just('No pledge found'); + } + + const { goal } = pledge; + + if ( + isFrontEndCert && goal === commitGoals.frontEndCert || + isFullStackCert && goal === commitGoals.fullStackCert + ) { + debug('marking goal complete'); + pledge.isCompleted = true; + pledge.dateEnded = new Date(); + pledge.formerUserId = pledge.userId; + pledge.userId = null; + return Observable.fromNodeCallback(pledge.save, pledge)(); + } + return Observable.just(dedent` + You have not yet reached your goal of completing the ${goal} + Please retry when you have met the requirements. + `); + }); +} diff --git a/server/views/commit/index.jade b/server/views/commit/index.jade index 591c285919..52528046f0 100644 --- a/server/views/commit/index.jade +++ b/server/views/commit/index.jade @@ -23,10 +23,10 @@ block content h4 Choose your goal: .btn-group.btn-group-justified(data-toggle='buttons' role='group') label.btn.btn-primary.btn-lg.active - input(type='radio' id=frontEndCert value='Front End Development Certification' name='goal' checked="checked") + input(type='radio' id=frontEndCert value=frontEndCert name='goal' checked="checked") | Front End Development Certificate (takes about 400 hours) label.btn.btn-primary.btn-lg - input(type='radio' id=fullStackCert value='Full Stack Development Certification' name='goal') + input(type='radio' id=fullStackCert value=fullStackCert name='goal') | Full Stack Development Certificate (takes about 800 hours) .spacer .row