From 98485a0da0d98ffe0a9b36440229f9bc671cddaa Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 7 May 2019 14:47:25 -0700 Subject: [PATCH] percentile stat of 0 if no contributions --- app/controllers/donor.controller.js | 161 ++++++++++++++-------------- 1 file changed, 82 insertions(+), 79 deletions(-) diff --git a/app/controllers/donor.controller.js b/app/controllers/donor.controller.js index f85abbb..4026f5f 100644 --- a/app/controllers/donor.controller.js +++ b/app/controllers/donor.controller.js @@ -1,91 +1,91 @@ const db = require('../../models'), - bcrypt = require('bcrypt-nodejs'), - errorMaker = require('../helpers/error.maker'), - uuidv4 = require('uuid/v4'); + bcrypt = require('bcrypt-nodejs'), + errorMaker = require('../helpers/error.maker'), + uuidv4 = require('uuid/v4'); const Donor = db.Donor; // Create/post a Donor exports.createDonor = (req, res, next) => { - // see if user already in db - Donor.findAll({ - where: { - email: req.body.email - } - }) - .then(donors => { - if (donors.length >= 1) { - return next(errorMaker(409, `Email Exists: ${req.body.email}`)); - } else { - // hash and store - bcrypt.hash(req.body.password, null, null, function(error, hash) { - // Store hash in your password DB. - if (error) { - return next(error); - } else { - Donor.create({ - id: uuidv4(), - first_name: req.body.first_name, - middle_name: req.body.middle_name, - last_name: req.body.last_name, - email: req.body.email, - password: hash, //hashed password - age: req.body.age, - phone: req.body.phone, - address: req.body.address, - city: req.body.city, - state: req.body.state, - country: req.body.country - }) - .then(donor => { - // Send created donor to client - return res.status(201).json({ - message: 'Donor created', - donor - }); - }) - .catch(error => next(error)); - } - }); - } - }) - .catch(error => next(error)); + // see if user already in db + Donor.findAll({ + where: { + email: req.body.email + } + }) + .then(donors => { + if (donors.length >= 1) { + return next(errorMaker(409, `Email Exists: ${req.body.email}`)); + } else { + // hash and store + bcrypt.hash(req.body.password, null, null, function(error, hash) { + // Store hash in your password DB. + if (error) { + return next(error); + } else { + Donor.create({ + id: uuidv4(), + first_name: req.body.first_name, + middle_name: req.body.middle_name, + last_name: req.body.last_name, + email: req.body.email, + password: hash, //hashed password + age: req.body.age, + phone: req.body.phone, + address: req.body.address, + city: req.body.city, + state: req.body.state, + country: req.body.country + }) + .then(donor => { + // Send created donor to client + return res.status(201).json({ + message: 'Donor created', + donor + }); + }) + .catch(error => next(error)); + } + }); + } + }) + .catch(error => next(error)); }; // FETCH all Donor exports.getAllDonors = (req, res, next) => { - Donor.findAll().then(donors => { - // Send all donors to Client - res.status(200).json({ - donors, - number_donors: donors.length - }); - }); + Donor.findAll().then(donors => { + // Send all donors to Client + res.status(200).json({ + donors, + number_donors: donors.length + }); + }); }; // Find a Donor by Id exports.findDonorById = (req, res) => { - Donor.findById(req.params.donor_id).then(donor => { - res.send(donor); - }); + Donor.findById(req.params.donor_id).then(donor => { + res.send(donor); + }); }; // Delete a Donor by Id exports.deleteDonor = (req, res) => { - const id = req.params.donor_id; - Donor.destroy({ - where: { id: id } - }).then(() => { - res.status(200).send('deleted successfully a donor with id = ' + id); - }); + const id = req.params.donor_id; + Donor.destroy({ + where: { id: id } + }).then(() => { + res.status(200).send('deleted successfully a donor with id = ' + id); + }); }; // GET the quick stats for the dashboard exports.getDashboardData = async (req, res, next) => { - const donor_id = req.user.id; + const donor_id = req.user.id; - const donors = await db.sequelize.query( - ` + const donors = await db.sequelize.query( + ` select d.id, d.first_name, sum(g.amount) as total_contributions @@ -94,22 +94,25 @@ exports.getDashboardData = async (req, res, next) => { group by d.id order by total_contributions DESC `, - { - type: db.sequelize.QueryTypes.SELECT - } - ); + { + type: db.sequelize.QueryTypes.SELECT + } + ); - let number_donors = donors.length; + let number_donors = donors.length; - donors.map((donor, index) => { - if (donor.id == donor_id) { - let percentile = (number_donors - index) / number_donors; - return res.status(200).json({ - message: 'Successfully got the stats', - percentile - }); - } - }); + donors.map((donor, index) => { + if (donor.id == donor_id) { + const donor_contributions = donor.total_contributions; + + let percentile = + donor_contributions > 0 ? (number_donors - index) / number_donors : 0; + return res.status(200).json({ + message: 'Successfully got the stats', + percentile + }); + } + }); }; // // Update a Donor