Add stop commit functionality

This commit is contained in:
Berkeley Martinez
2015-10-06 18:22:12 -07:00
parent d132978df9
commit 0b716c0057
2 changed files with 49 additions and 2 deletions

View File

@@ -18,6 +18,10 @@ import {
} from '../utils/middleware';
const sendNonUserToFront = ifNoUserRedirectTo('/');
const sendNonUserToCommit = ifNoUserRedirectTo(
'/commit',
'Must be signed in to update commit'
);
const debug = debugFactory('freecc:commit');
function findNonprofit(name) {
@@ -47,6 +51,12 @@ export default function commit(app) {
pledge
);
router.post(
'/commit/stop-commitment',
sendNonUserToCommit,
stopCommit
);
app.use(router);
function commitToNonprofit(req, res, next) {
@@ -143,4 +153,32 @@ export default function commit(app) {
next
);
}
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 => {
let msg = `You have successfully stopped your pledge.`;
if (!pledge) {
msg = `No pledge found for user ${user.username}.`;
}
req.flash('errors', { msg });
return res.redirect('/commit');
},
next
);
}
}