From f5dd9811520e694cb92a9badc3dd4ca0c98cc55d Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sat, 19 Jan 2019 11:26:18 -0800 Subject: [PATCH] getting suggested amount and getting charities --- app/controllers/donor/index.js | 5 ++ .../donor/organization.controller.js | 53 +++++++++++++++++++ app/routes/donor.route.js | 38 ++++++++----- 3 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 app/controllers/donor/index.js create mode 100644 app/controllers/donor/organization.controller.js diff --git a/app/controllers/donor/index.js b/app/controllers/donor/index.js new file mode 100644 index 0000000..77a4459 --- /dev/null +++ b/app/controllers/donor/index.js @@ -0,0 +1,5 @@ +module.exports = { + donor: require('./donor.controller'), + grant: require('./grant.controller'), + organization: require('./organization.controller') +}; diff --git a/app/controllers/donor/organization.controller.js b/app/controllers/donor/organization.controller.js new file mode 100644 index 0000000..c9e85fd --- /dev/null +++ b/app/controllers/donor/organization.controller.js @@ -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 + }); +}; diff --git a/app/routes/donor.route.js b/app/routes/donor.route.js index 93c958c..232fa7f 100644 --- a/app/routes/donor.route.js +++ b/app/routes/donor.route.js @@ -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);