percentile stat of 0 if no contributions
This commit is contained in:
@@ -1,91 +1,91 @@
|
|||||||
const db = require('../../models'),
|
const db = require('../../models'),
|
||||||
bcrypt = require('bcrypt-nodejs'),
|
bcrypt = require('bcrypt-nodejs'),
|
||||||
errorMaker = require('../helpers/error.maker'),
|
errorMaker = require('../helpers/error.maker'),
|
||||||
uuidv4 = require('uuid/v4');
|
uuidv4 = require('uuid/v4');
|
||||||
|
|
||||||
const Donor = db.Donor;
|
const Donor = db.Donor;
|
||||||
|
|
||||||
// Create/post a Donor
|
// Create/post a Donor
|
||||||
exports.createDonor = (req, res, next) => {
|
exports.createDonor = (req, res, next) => {
|
||||||
// see if user already in db
|
// see if user already in db
|
||||||
Donor.findAll({
|
Donor.findAll({
|
||||||
where: {
|
where: {
|
||||||
email: req.body.email
|
email: req.body.email
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(donors => {
|
.then(donors => {
|
||||||
if (donors.length >= 1) {
|
if (donors.length >= 1) {
|
||||||
return next(errorMaker(409, `Email Exists: ${req.body.email}`));
|
return next(errorMaker(409, `Email Exists: ${req.body.email}`));
|
||||||
} else {
|
} else {
|
||||||
// hash and store
|
// hash and store
|
||||||
bcrypt.hash(req.body.password, null, null, function(error, hash) {
|
bcrypt.hash(req.body.password, null, null, function(error, hash) {
|
||||||
// Store hash in your password DB.
|
// Store hash in your password DB.
|
||||||
if (error) {
|
if (error) {
|
||||||
return next(error);
|
return next(error);
|
||||||
} else {
|
} else {
|
||||||
Donor.create({
|
Donor.create({
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
first_name: req.body.first_name,
|
first_name: req.body.first_name,
|
||||||
middle_name: req.body.middle_name,
|
middle_name: req.body.middle_name,
|
||||||
last_name: req.body.last_name,
|
last_name: req.body.last_name,
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
password: hash, //hashed password
|
password: hash, //hashed password
|
||||||
age: req.body.age,
|
age: req.body.age,
|
||||||
phone: req.body.phone,
|
phone: req.body.phone,
|
||||||
address: req.body.address,
|
address: req.body.address,
|
||||||
city: req.body.city,
|
city: req.body.city,
|
||||||
state: req.body.state,
|
state: req.body.state,
|
||||||
country: req.body.country
|
country: req.body.country
|
||||||
})
|
})
|
||||||
.then(donor => {
|
.then(donor => {
|
||||||
// Send created donor to client
|
// Send created donor to client
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: 'Donor created',
|
message: 'Donor created',
|
||||||
donor
|
donor
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(error => next(error));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(error => next(error));
|
||||||
};
|
};
|
||||||
|
|
||||||
// FETCH all Donor
|
// FETCH all Donor
|
||||||
exports.getAllDonors = (req, res, next) => {
|
exports.getAllDonors = (req, res, next) => {
|
||||||
Donor.findAll().then(donors => {
|
Donor.findAll().then(donors => {
|
||||||
// Send all donors to Client
|
// Send all donors to Client
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
donors,
|
donors,
|
||||||
number_donors: donors.length
|
number_donors: donors.length
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find a Donor by Id
|
// Find a Donor by Id
|
||||||
exports.findDonorById = (req, res) => {
|
exports.findDonorById = (req, res) => {
|
||||||
Donor.findById(req.params.donor_id).then(donor => {
|
Donor.findById(req.params.donor_id).then(donor => {
|
||||||
res.send(donor);
|
res.send(donor);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete a Donor by Id
|
// Delete a Donor by Id
|
||||||
exports.deleteDonor = (req, res) => {
|
exports.deleteDonor = (req, res) => {
|
||||||
const id = req.params.donor_id;
|
const id = req.params.donor_id;
|
||||||
Donor.destroy({
|
Donor.destroy({
|
||||||
where: { id: id }
|
where: { id: id }
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
res.status(200).send('deleted successfully a donor with id = ' + id);
|
res.status(200).send('deleted successfully a donor with id = ' + id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// GET the quick stats for the dashboard
|
// GET the quick stats for the dashboard
|
||||||
exports.getDashboardData = async (req, res, next) => {
|
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,
|
select d.id,
|
||||||
d.first_name,
|
d.first_name,
|
||||||
sum(g.amount) as total_contributions
|
sum(g.amount) as total_contributions
|
||||||
@@ -94,22 +94,25 @@ exports.getDashboardData = async (req, res, next) => {
|
|||||||
group by d.id
|
group by d.id
|
||||||
order by total_contributions DESC
|
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) => {
|
donors.map((donor, index) => {
|
||||||
if (donor.id == donor_id) {
|
if (donor.id == donor_id) {
|
||||||
let percentile = (number_donors - index) / number_donors;
|
const donor_contributions = donor.total_contributions;
|
||||||
return res.status(200).json({
|
|
||||||
message: 'Successfully got the stats',
|
let percentile =
|
||||||
percentile
|
donor_contributions > 0 ? (number_donors - index) / number_donors : 0;
|
||||||
});
|
return res.status(200).json({
|
||||||
}
|
message: 'Successfully got the stats',
|
||||||
});
|
percentile
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// // Update a Donor
|
// // Update a Donor
|
||||||
|
|||||||
Reference in New Issue
Block a user