Add stop commit functionality
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user