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
+121 -108
View File
@@ -1,103 +1,104 @@
const db = require('../../models'), const db = require("../../models"),
errorMaker = require('../helpers/error.maker'); errorMaker = require("../helpers/error.maker");
const stripe = require('./stripe.controller'); const stripe = require("./stripe.controller");
const sendgrid = require('./sendgrid.controller'); const sendgrid = require("./sendgrid.controller");
const { const {
Grant, Grant,
Cause, Cause,
Region, Region,
Organization, Organization,
GrantOrganization, GrantOrganization,
Project, Project,
sequelize sequelize
} = db; } = db;
// Create a grant for certain donor // Create a grant for certain donor
exports.createGrant = async (req, res, next) => { exports.createGrant = async (req, res, next) => {
const user = req.user; const user = req.user;
var { name, monthly, organizations, amount, stripeToken } = req.body; var { name, monthly, organizations, amount, stripeToken } = req.body;
const causes = organizations.map(org => org.primary_cause); const causes = organizations.map(org => org.primary_cause);
const regions = organizations.map(org => org.primary_region); const regions = organizations.map(org => org.primary_region);
actual_total = Math.round(amount * 100) / 100; actual_total = Math.round(amount * 100) / 100;
if (!stripeToken) { if (!stripeToken) {
throw errorMaker(400, 'No stripe token given'); throw errorMaker(400, "No stripe token given");
} }
let transaction; let transaction;
try { try {
// get transaction // get transaction
transaction = await sequelize.transaction(); transaction = await sequelize.transaction();
const grant = await Grant.create( const grant = await Grant.create(
{ {
donor_id: user.id, donor_id: user.id,
name, name,
amount: actual_total, amount: actual_total,
monthly, monthly,
num_causes: causes.length, num_causes: causes.length,
num_regions: regions.length num_regions: regions.length
}, },
{ {
transaction transaction
} }
); );
const grantsOrgs = organizations.map(org => { const grantsOrgs = organizations.map(org => {
return { return {
grant_id: grant.id, grant_id: grant.id,
organization_id: org.id, organization_id: org.id,
amount: org.amount amount: org.amount
}; };
}); });
// mapping between bundle and orgs // mapping between bundle and orgs
await GrantOrganization.bulkCreate(grantsOrgs, { await GrantOrganization.bulkCreate(grantsOrgs, {
transaction transaction
}); });
// one time charge // one time charge
await stripe.grantCharge({ await stripe.grantCharge({
grant, grant,
stripeToken, stripeToken,
stripeToken, stripeToken,
organizations, organizations,
monthly, monthly,
amount, amount,
user user
}); });
// send email // send email
await sendgrid.paymentReceipt({ await sendgrid.paymentReceipt({
organizations, organizations,
total_amount: actual_total, total_amount: actual_total,
receiver: user.email receiver: user.email
}); });
// commit // commit
await transaction.commit(); await transaction.commit();
return res.status(200).json({ return res.status(200).json({
message: 'Successfully charged or subscribed', message: "Successfully charged or subscribed",
grant grant
}); });
} catch (error) { } catch (error) {
if (error) await transaction.rollback(); if (error) await transaction.rollback();
next(error); next(error);
} }
}; };
// Find grants with causes, regions, and organizations by donor_id // Find grants with causes, regions, and organizations by donor_id
exports.findGrantsByDonorId = async (req, res, next) => { exports.findGrantsByDonorId = async (req, res, next) => {
const donor_id = req.user.id; const donor_id = req.user.id;
try { try {
const grants = await db.sequelize.query(` const grants = await db.sequelize.query(
`
Select Select
g.id, g.id,
g.name, g.name,
@@ -106,42 +107,54 @@ exports.findGrantsByDonorId = async (req, res, next) => {
g.num_causes, g.num_causes,
g.num_regions, g.num_regions,
g.donor_id, g.donor_id,
g.created_at, GROUP_CONCAT(DISTINCT o.id SEPARATOR ',') AS organization_ids,
g.updated_at, GROUP_CONCAT(DISTINCT o.name SEPARATOR ',') AS organization_names
o.id as organization_id, from grants as g
o.primary_cause, inner join GrantOrganizations as go on go.grant_id = g.id
o.primary_region left join organizations as o on o.id = go.organization_id
from grants as g where donor_id = :donor_id
left join GrantOrganizations as go on go.grant_id = g.id group by g.id, g.name, g.amount, g.monthly, g.num_causes, g.num_regions, g.donor_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, replacements: { donor_id } }
`, { );
type: db.sequelize.QueryTypes.SELECT
});
return res.status(200).json({ grants.map(grant => {
grants grant.organizations = [];
})
} catch (error) { org_ids = grant.organization_ids.toString().split(",");
next(error); 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);
}
}; };
exports.deleteGrant = (req, res, next) => { exports.deleteGrant = (req, res, next) => {
const { grant_id } = req.body; const { grant_id } = req.body;
const user = req.user; const user = req.user;
Grant.destroy({ where: { id: grant_id, donor_id: user.id } }) Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
.then(result => { .then(result => {
var message = 'Already Deleted'; var message = "Already Deleted";
if (result) { if (result) {
message = 'Grant Deleted'; message = "Grant Deleted";
} }
res.status(201).json({ res.status(201).json({
result, result,
message message
}); });
}) })
.catch(error => next(error)); .catch(error => next(error));
}; };