From 05616547396201d0395a415b92a04be14a754967 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Wed, 1 May 2019 02:29:08 -0700 Subject: [PATCH] donor stat with percentile --- app/controllers/donor.controller.js | 33 +++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/controllers/donor.controller.js b/app/controllers/donor.controller.js index c0587a7..f85abbb 100644 --- a/app/controllers/donor.controller.js +++ b/app/controllers/donor.controller.js @@ -82,14 +82,33 @@ exports.deleteDonor = (req, res) => { // GET the quick stats for the dashboard exports.getDashboardData = async (req, res, next) => { - console.log(req.user); - const rows = await db.sequelize.query('Select * from donors', { - type: db.sequelize.QueryTypes.SELECT - }); + const donor_id = req.user.id; - res.status(200).json({ - message: 'Successfully got the stats', - rows + const donors = await db.sequelize.query( + ` + select d.id, + d.first_name, + sum(g.amount) as total_contributions + from donors as d + left join grants as g on g.donor_id = d.id + group by d.id + order by total_contributions DESC + `, + { + type: db.sequelize.QueryTypes.SELECT + } + ); + + 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 + }); + } }); };