2015-07-27 02:12:56 +00:00
|
|
|
var Rx = require('rx');
|
2015-06-23 02:23:07 +00:00
|
|
|
var debug = require('debug')('freecc:nonprofits');
|
|
|
|
var observeMethod = require('../utils/rx').observeMethod;
|
|
|
|
var unDasherize = require('../utils').unDasherize;
|
|
|
|
var dasherize = require('../utils').dasherize;
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
module.exports = function(app) {
|
2015-06-03 23:31:42 +00:00
|
|
|
var router = app.loopback.Router();
|
2015-06-03 23:19:23 +00:00
|
|
|
var Nonprofit = app.models.Nonprofit;
|
2015-06-23 02:23:07 +00:00
|
|
|
var findNonprofits = observeMethod(Nonprofit, 'find');
|
|
|
|
var findOneNonprofit = observeMethod(Nonprofit, 'findOne');
|
2015-03-31 01:02:50 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
router.get('/nonprofits/directory', nonprofitsDirectory);
|
|
|
|
router.get('/nonprofits/:nonprofitName', returnIndividualNonprofit);
|
|
|
|
|
2015-06-03 23:31:42 +00:00
|
|
|
app.use(router);
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
function nonprofitsDirectory(req, res, next) {
|
2015-07-27 02:12:56 +00:00
|
|
|
findNonprofits({
|
|
|
|
order: 'moneySaved DESC'
|
|
|
|
})
|
|
|
|
.flatMap(
|
|
|
|
(nonprofits = []) => {
|
|
|
|
// turn array of nonprofits into observable array
|
|
|
|
return Rx.Observable.from(nonprofits)
|
|
|
|
.pluck('moneySaved')
|
|
|
|
.reduce((sum, moneySaved = 0) => sum + moneySaved, 0);
|
|
|
|
},
|
|
|
|
(nonprofits = [], totalSavings) => ({ nonprofits, totalSavings })
|
|
|
|
)
|
|
|
|
.subscribe(({ nonprofits, totalSavings }) => {
|
2015-06-03 23:19:23 +00:00
|
|
|
res.render('nonprofits/directory', {
|
|
|
|
title: 'Nonprofits we help',
|
2015-06-26 06:35:48 +00:00
|
|
|
nonprofits: nonprofits,
|
|
|
|
totalSavings: totalSavings.toString().replace(/000$/, ',000')
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
2015-06-23 02:23:07 +00:00
|
|
|
},
|
|
|
|
next
|
2015-06-03 23:19:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnIndividualNonprofit(req, res, next) {
|
|
|
|
var dashedName = req.params.nonprofitName;
|
2015-06-23 02:23:07 +00:00
|
|
|
var nonprofitName = unDasherize(dashedName);
|
|
|
|
var query = { where: { name: {
|
|
|
|
like: nonprofitName,
|
|
|
|
options: 'i'
|
|
|
|
} } };
|
|
|
|
|
|
|
|
debug('looking for %s', nonprofitName);
|
|
|
|
debug('query', query);
|
|
|
|
findOneNonprofit(query).subscribe(
|
|
|
|
function(nonprofit) {
|
|
|
|
if (!nonprofit) {
|
2015-06-03 23:19:23 +00:00
|
|
|
req.flash('errors', {
|
|
|
|
msg: "404: We couldn't find a nonprofit with that name. " +
|
|
|
|
'Please double check the name.'
|
|
|
|
});
|
|
|
|
return res.redirect('/nonprofits');
|
|
|
|
}
|
|
|
|
|
2015-06-23 02:23:07 +00:00
|
|
|
var dashedNameFull = dasherize(nonprofit.name);
|
2015-06-03 23:19:23 +00:00
|
|
|
if (dashedNameFull !== dashedName) {
|
|
|
|
return res.redirect('../nonprofit/' + dashedNameFull);
|
|
|
|
}
|
2015-06-23 02:23:07 +00:00
|
|
|
|
2015-08-01 05:06:07 +00:00
|
|
|
//We need to create logic that verifies completion. Defaulting to false for now.
|
|
|
|
//var buttonActive = false;
|
|
|
|
//if (
|
|
|
|
// req.user &&
|
|
|
|
// req.user.completedCoursewares.length > 63
|
|
|
|
//) {
|
|
|
|
// var hasShownInterest =
|
|
|
|
// nonprofit.interestedCampers.filter(function(user) {
|
|
|
|
// return user.username === req.user.username;
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// if (hasShownInterest.length === 0) {
|
|
|
|
// buttonActive = true;
|
|
|
|
// }
|
|
|
|
//}
|
2015-05-21 18:07:40 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
res.render('nonprofits/show', {
|
|
|
|
dashedName: dashedNameFull,
|
|
|
|
title: nonprofit.name,
|
|
|
|
logoUrl: nonprofit.logoUrl,
|
|
|
|
estimatedHours: nonprofit.estimatedHours,
|
|
|
|
projectDescription: nonprofit.projectDescription,
|
|
|
|
|
|
|
|
approvedOther:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('other') > -1,
|
|
|
|
approvedWebsite:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('website') > -1,
|
|
|
|
|
|
|
|
approvedDonor:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('donor') > -1,
|
|
|
|
approvedInventory:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('inventory') > -1,
|
|
|
|
|
|
|
|
approvedVolunteer:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('volunteer') > -1,
|
|
|
|
approvedForm:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('form') > -1,
|
|
|
|
|
|
|
|
approvedCommunity:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('community') > -1,
|
|
|
|
approvedELearning:
|
|
|
|
nonprofit.approvedDeliverables.indexOf('eLearning') > -1,
|
|
|
|
|
|
|
|
websiteLink: nonprofit.websiteLink,
|
|
|
|
imageUrl: nonprofit.imageUrl,
|
|
|
|
whatDoesNonprofitDo: nonprofit.whatDoesNonprofitDo,
|
|
|
|
interestedCampers: nonprofit.interestedCampers,
|
|
|
|
assignedCampers: nonprofit.assignedCampers,
|
2015-08-01 05:06:07 +00:00
|
|
|
buttonActive: false,
|
2015-06-26 06:35:48 +00:00
|
|
|
moneySaved: nonprofit.moneySaved,
|
2015-06-03 23:19:23 +00:00
|
|
|
currentStatus: nonprofit.currentStatus
|
2015-05-21 18:07:40 +00:00
|
|
|
});
|
2015-06-23 02:23:07 +00:00
|
|
|
},
|
|
|
|
next
|
2015-05-21 18:07:40 +00:00
|
|
|
);
|
2015-04-02 05:31:08 +00:00
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
};
|