refactoring models and create grant to async await

This commit is contained in:
Arjun Patel
2019-03-16 23:49:11 -07:00
parent e5c204890e
commit 5587c296e4
6 changed files with 97 additions and 79 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ module.exports = models => {
donor.hasMany(Grant, { foreignKey: 'donor_id' }); donor.hasMany(Grant, { foreignKey: 'donor_id' });
Grant.belongsToMany(Organization, { Grant.belongsToMany(Organization, {
through: 'grants_organizations', through: 'Grant_Organization',
foreignKey: 'grant_id', foreignKey: 'grant_id',
otherKey: 'organization_id' otherKey: 'organization_id'
}); });
+37 -37
View File
@@ -4,21 +4,16 @@ const db = require('../../../models'),
const stripe = require('./stripe.controller'); const stripe = require('./stripe.controller');
const sendgrid = require('../sendgrid.controller'); const sendgrid = require('../sendgrid.controller');
const { Grant, Cause, Region, Organization, sequelize } = db; const { Grant, Cause, Region, Organization, GrantOrganization, sequelize } = db;
console.log(db);
// Create a grant for certain donor // Create a grant for certain donor
exports.create = (req, res, next) => { exports.create = async (req, res, next) => {
const user = req.user; const user = req.user;
var { var { name, monthly, organizations, amount, stripeToken } = req.body;
name,
monthly, const causes = organizations.map(org => org.primary_cause);
causes, const regions = organizations.map(org => org.primary_region);
regions,
organizations,
amount,
stripeToken
} = req.body;
actual_total = Math.round(amount * 100) / 100; actual_total = Math.round(amount * 100) / 100;
@@ -26,9 +21,13 @@ exports.create = (req, res, next) => {
throw errorMaker(400, 'No stripe token given'); throw errorMaker(400, 'No stripe token given');
} }
return sequelize let transaction;
.transaction(function(t) {
return Grant.create( try {
// get transaction
transaction = await sequelize.transaction();
const grant = await Grant.create(
{ {
donor_id: user.id, donor_id: user.id,
name, name,
@@ -38,25 +37,24 @@ exports.create = (req, res, next) => {
num_regions: regions.length num_regions: regions.length
}, },
{ {
transaction: t transaction
} }
).then(grant => { );
return Promise.all([
grant.addCauses(causes, { transaction: t }), const grantsOrgs = organizations.map(org => {
grant.addRegions(regions, { transaction: t }), return {
organizations.map(org => { grant_id: grant.id,
grant.addOrganization(org.id, { organization_id: org.id,
through: { amount: org.amount }, amount: org.amount
transaction: t };
}); });
})
]).then(result => grant); await GrantOrganization.bulkCreate(grantsOrgs, {
transaction
}); });
})
.then(function(grants) { await stripe.grantCharge({
// transaction committed grant,
stripe.grantCharge({
grant: grants.dataValues,
stripeToken, stripeToken,
stripeToken, stripeToken,
organizations, organizations,
@@ -65,21 +63,23 @@ exports.create = (req, res, next) => {
user user
}); });
sendgrid.paymentReceipt({ await sendgrid.paymentReceipt({
organizations, organizations,
total_amount: actual_total, total_amount: actual_total,
receiver: user.email receiver: user.email
}); });
// 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: grants.dataValues grant
}); });
}) } catch (error) {
.catch(function(error) { if (error) await transaction.rollback();
// 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
-6
View File
@@ -45,11 +45,5 @@ module.exports = (sequelize, DataTypes) => {
country: DataTypes.STRING country: DataTypes.STRING
}); });
Donor.associate = models => {
Donor.hasMany(models.Grant, {
foreignKey: 'donor_id'
});
};
return Donor; return Donor;
}; };
+11 -6
View File
@@ -1,3 +1,5 @@
const Donor = require('./Donor');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Grant = sequelize.define('Grant', { const Grant = sequelize.define('Grant', {
id: { id: {
@@ -25,16 +27,19 @@ module.exports = (sequelize, DataTypes) => {
num_regions: { num_regions: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false
},
donor_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: Donor(sequelize, DataTypes),
key: 'id'
}
} }
}); });
Grant.associate = models => { Grant.associate = models => {
Grant.belongsToMany(models.Organization, {
through: 'grants_organizations',
foreignKey: 'grant_id',
otherKey: 'organization_id'
});
Grant.hasMany(models.Charge, { foreignKey: 'grant_id' }); Grant.hasMany(models.Charge, { foreignKey: 'grant_id' });
}; };
+21 -2
View File
@@ -1,14 +1,33 @@
const Organization = require('./Organization'),
Grant = require('./Grant');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const GrantsOrganizations = sequelize.define('Grants_Organizations', { const GrantOrganization = sequelize.define('GrantOrganization', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
}, },
grant_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: Grant(sequelize, DataTypes),
key: 'id'
}
},
organization_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: Organization(sequelize, DataTypes),
key: 'id'
}
},
amount: { amount: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false
} }
}); });
return GrantsOrganizations; return GrantOrganization;
}; };
+1 -1
View File
@@ -51,7 +51,7 @@ module.exports = (sequelize, DataTypes) => {
} }
}, },
number_people_impacted: { number_people_impacted: {
type: DataTypes.STRING, type: DataTypes.INTEGER,
defaultValue: 0 defaultValue: 0
} }
}); });