changin query for the grants and organizations associated

This commit is contained in:
Arjun Patel
2019-03-23 20:40:30 -07:00
parent 9c42496e22
commit 80beb1a825
+33 -20
View File
@@ -1,8 +1,8 @@
const db = require('../../models'),
errorMaker = require('../helpers/error.maker');
const db = require("../../models"),
errorMaker = require("../helpers/error.maker");
const stripe = require('./stripe.controller');
const sendgrid = require('./sendgrid.controller');
const stripe = require("./stripe.controller");
const sendgrid = require("./sendgrid.controller");
const {
Grant,
@@ -25,7 +25,7 @@ exports.createGrant = async (req, res, next) => {
actual_total = Math.round(amount * 100) / 100;
if (!stripeToken) {
throw errorMaker(400, 'No stripe token given');
throw errorMaker(400, "No stripe token given");
}
let transaction;
@@ -83,7 +83,7 @@ exports.createGrant = async (req, res, next) => {
await transaction.commit();
return res.status(200).json({
message: 'Successfully charged or subscribed',
message: "Successfully charged or subscribed",
grant
});
} catch (error) {
@@ -97,7 +97,8 @@ exports.findGrantsByDonorId = async (req, res, next) => {
const donor_id = req.user.id;
try {
const grants = await db.sequelize.query(`
const grants = await db.sequelize.query(
`
Select
g.id,
g.name,
@@ -106,22 +107,34 @@ exports.findGrantsByDonorId = async (req, res, next) => {
g.num_causes,
g.num_regions,
g.donor_id,
g.created_at,
g.updated_at,
o.id as organization_id,
o.primary_cause,
o.primary_region
GROUP_CONCAT(DISTINCT o.id SEPARATOR ',') AS organization_ids,
GROUP_CONCAT(DISTINCT o.name SEPARATOR ',') AS organization_names
from grants as g
left join GrantOrganizations as go on go.grant_id = g.id
left join organizations as o on go.organization_id = o.id
where donor_id = "0568eef6-f3cb-480b-933c-d32d540bface";
`, {
type: db.sequelize.QueryTypes.SELECT
inner join GrantOrganizations as go on go.grant_id = g.id
left join organizations as o on o.id = go.organization_id
where donor_id = :donor_id
group by g.id, g.name, g.amount, g.monthly, g.num_causes, g.num_regions, g.donor_id;
`,
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
);
grants.map(grant => {
grant.organizations = [];
org_ids = grant.organization_ids.toString().split(",");
org_names = grant.organization_names.toString().split(",");
for (var i = 0; i < org_names.length; i++) {
grant.organizations.push({
id: org_ids[i],
name: org_names[i]
});
}
});
return res.status(200).json({
grants
})
});
} catch (error) {
next(error);
}
@@ -133,9 +146,9 @@ exports.deleteGrant = (req, res, next) => {
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
.then(result => {
var message = 'Already Deleted';
var message = "Already Deleted";
if (result) {
message = 'Grant Deleted';
message = "Grant Deleted";
}
res.status(201).json({