getting suggested amount and getting charities
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
donor: require('./donor.controller'),
|
||||
grant: require('./grant.controller'),
|
||||
organization: require('./organization.controller')
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
const db = require('../../config/db.config.js'),
|
||||
errorMaker = require('../../helpers/error.maker');
|
||||
|
||||
const { Grant, Cause, Region, Organization } = db;
|
||||
|
||||
const log10 = val => {
|
||||
return Math.log(val) / Math.log(10);
|
||||
};
|
||||
|
||||
// GET suggested organizations based on chosen causes + regions
|
||||
exports.findSuggested = (req, res, next) => {
|
||||
const { amount, causes, regions } = req.body;
|
||||
|
||||
// formula for max amount of organizations to choose
|
||||
const max_orgs = Math.floor(1.5 * log10(amount));
|
||||
|
||||
const QUERY =
|
||||
'SELECT * from organizations ' +
|
||||
'WHERE primary_cause IN (:causes) and primary_region IN (:regions) ' +
|
||||
'LIMIT :max_orgs';
|
||||
db.sequelize
|
||||
.query(QUERY, {
|
||||
replacements: { causes, regions, max_orgs },
|
||||
type: db.Sequelize.QueryTypes.SELECT
|
||||
})
|
||||
.then(orgs => {
|
||||
res.status(200).json({
|
||||
orgs,
|
||||
num_items: orgs.length,
|
||||
max_orgs,
|
||||
distribution: amount / orgs.length
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
// GET min and optimal amounts for the chosen causes + regions
|
||||
exports.findAmounts = (req, res, next) => {
|
||||
const { num_causes, num_regions } = req.body;
|
||||
|
||||
const feature_factor = parseInt(num_causes) + parseFloat(num_regions / 2);
|
||||
|
||||
// calculate min amount for chosen causes and regions
|
||||
const min_amount = Math.ceil(feature_factor * 15);
|
||||
|
||||
// calculate optimal for chosen causes and regions
|
||||
const optimal_amount = Math.ceil(Math.pow(5, feature_factor));
|
||||
|
||||
res.status(200).json({
|
||||
min_amount,
|
||||
optimal_amount
|
||||
});
|
||||
};
|
||||
+26
-12
@@ -2,27 +2,41 @@ const express = require('express'),
|
||||
router = express.Router(),
|
||||
checkAuth = require('../middleware/check-auth');
|
||||
|
||||
const donor = require('../controllers/donor/donor.controller.js');
|
||||
const grant = require('../controllers/donor/grant.controller.js');
|
||||
const controllers = require('../controllers/donor');
|
||||
|
||||
// Signup route
|
||||
router.post('/', donor.create);
|
||||
// POST Signup route
|
||||
router.post('/', controllers.donor.create);
|
||||
|
||||
// Retrieve all Donors
|
||||
router.get('/', checkAuth, donor.findAll);
|
||||
// GET Retrieve all Donors
|
||||
router.get('/', checkAuth, controllers.donor.findAll);
|
||||
|
||||
// Retrieve grants with causes and regions and charities details by donor_id
|
||||
router.get('/grants/:donor_id', checkAuth, grant.findByDonorId);
|
||||
// GET Retrieve grants with causes and regions and charities details by donor_id
|
||||
router.get('/grants/:donor_id', checkAuth, controllers.grant.findByDonorId);
|
||||
|
||||
/** Create grants with following body:
|
||||
/** POST Create grants with following body:
|
||||
* - list of id's of selected causes and regions
|
||||
* - FINAL list of id's of selected organizations
|
||||
* - donor_id
|
||||
* */
|
||||
router.post('/grants/:donor_id', checkAuth, grant.create);
|
||||
router.post('/grants/:donor_id', checkAuth, controllers.grant.create);
|
||||
|
||||
// Delete a grant of a donor
|
||||
router.delete('/grants/:grant_id', checkAuth, grant.delete);
|
||||
// DELECT a grant of a donor
|
||||
router.delete('/grants/:grant_id', checkAuth, controllers.grant.delete);
|
||||
|
||||
// GET suggested organizations to distribute to
|
||||
// running "the algorithm"
|
||||
router.get(
|
||||
'/organizations/',
|
||||
checkAuth,
|
||||
controllers.organization.findSuggested
|
||||
);
|
||||
|
||||
// GET min and optimal amount to choose
|
||||
router.get(
|
||||
'/organizations/amounts',
|
||||
checkAuth,
|
||||
controllers.organization.findAmounts
|
||||
);
|
||||
|
||||
// // Retrieve a single Donor by Id
|
||||
// router.get('/:donor_id', donor.findById);
|
||||
|
||||
Reference in New Issue
Block a user