fixing amount for each separate organization
This commit is contained in:
@@ -7,7 +7,8 @@ module.exports = models => {
|
|||||||
Organization,
|
Organization,
|
||||||
Charge,
|
Charge,
|
||||||
PaymentPlan,
|
PaymentPlan,
|
||||||
OrgBank
|
OrgBank,
|
||||||
|
GrantsOrganizations
|
||||||
} = models;
|
} = models;
|
||||||
|
|
||||||
Donor.hasMany(Grant, { foreignKey: 'donor_id' });
|
Donor.hasMany(Grant, { foreignKey: 'donor_id' });
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ const Sequelize = require('sequelize'),
|
|||||||
UserModel = require('../models/user.model.js'),
|
UserModel = require('../models/user.model.js'),
|
||||||
PaymentPlanModel = require('../models/paymentplan.model'),
|
PaymentPlanModel = require('../models/paymentplan.model'),
|
||||||
ChargeModel = require('../models/charge.model'),
|
ChargeModel = require('../models/charge.model'),
|
||||||
OrgBankModel = require('../models/bank_info.model'),
|
OrgBankModel = require('../models/orgBank.model'),
|
||||||
|
GrantsOrganizationsModel = require('../models/grantsOrganizations.model'),
|
||||||
associate = require('./associate');
|
associate = require('./associate');
|
||||||
|
|
||||||
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
||||||
@@ -46,6 +47,7 @@ const User = UserModel(sequelize, Sequelize);
|
|||||||
const Charge = ChargeModel(sequelize, Sequelize);
|
const Charge = ChargeModel(sequelize, Sequelize);
|
||||||
const PaymentPlan = PaymentPlanModel(sequelize, Sequelize);
|
const PaymentPlan = PaymentPlanModel(sequelize, Sequelize);
|
||||||
const OrgBank = OrgBankModel(sequelize, Sequelize);
|
const OrgBank = OrgBankModel(sequelize, Sequelize);
|
||||||
|
const GrantsOrganizations = GrantsOrganizationsModel(sequelize, Sequelize);
|
||||||
db.Donor = Donor;
|
db.Donor = Donor;
|
||||||
db.Grant = Grant;
|
db.Grant = Grant;
|
||||||
db.Cause = Cause;
|
db.Cause = Cause;
|
||||||
@@ -55,6 +57,7 @@ db.User = User;
|
|||||||
db.Charge = Charge;
|
db.Charge = Charge;
|
||||||
db.PaymentPlan = PaymentPlan;
|
db.PaymentPlan = PaymentPlan;
|
||||||
db.OrgBank = OrgBank;
|
db.OrgBank = OrgBank;
|
||||||
|
db.GrantsOrganizations = GrantsOrganizations;
|
||||||
|
|
||||||
// make associations with created models
|
// make associations with created models
|
||||||
associate(db);
|
associate(db);
|
||||||
|
|||||||
@@ -6,39 +6,49 @@ const { Grant, Cause, Region, Organization, sequelize } = db;
|
|||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.create = (req, res, next) => {
|
exports.create = (req, res, next) => {
|
||||||
const donor_id = req.user.id;
|
const donor_id = req.user.id;
|
||||||
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
const { name, amounts, monthly, causes, regions, organizations } = req.body;
|
||||||
|
|
||||||
|
const total_amount = 100;
|
||||||
|
|
||||||
return sequelize
|
return sequelize
|
||||||
.transaction(function (t) {
|
.transaction(function(t) {
|
||||||
return Grant.create(
|
return Grant.create(
|
||||||
{
|
{
|
||||||
donor_id,
|
donor_id,
|
||||||
name,
|
name,
|
||||||
amount,
|
amount: total_amount,
|
||||||
monthly,
|
monthly,
|
||||||
num_causes: causes.length,
|
num_causes: causes.length,
|
||||||
num_regions: regions.length
|
num_regions: regions.length
|
||||||
},
|
},
|
||||||
{ transaction: t }
|
{
|
||||||
|
transaction: t
|
||||||
|
}
|
||||||
).then(grant => {
|
).then(grant => {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
grant.addCauses(causes, { transaction: t }),
|
grant.addCauses(causes, { transaction: t }),
|
||||||
grant.addRegions(regions, { transaction: t }),
|
grant.addRegions(regions, { transaction: t }),
|
||||||
grant.addOrganizations(organizations, { transaction: t })
|
organizations.map(org => {
|
||||||
|
grant.addOrganization(org.id, {
|
||||||
|
through: { amount: org.amount },
|
||||||
|
transaction: t
|
||||||
|
});
|
||||||
|
})
|
||||||
]).then(result => grant);
|
]).then(result => grant);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(function (grants) {
|
.then(function(grants) {
|
||||||
// transaction committed
|
// transaction committed
|
||||||
// res.status(201).json({
|
// res.status(201).json({
|
||||||
// grant,
|
// grant,
|
||||||
// message: 'Grant Created'
|
// message: 'Grant Created'
|
||||||
// });
|
// });
|
||||||
|
console.log(grants);
|
||||||
|
|
||||||
next(grants.dataValues);
|
next(grants.dataValues);
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.catch(function (error) {
|
.catch(function(error) {
|
||||||
// transaction rollback
|
// transaction rollback
|
||||||
next(error);
|
next(error);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,15 +22,15 @@ exports.grantCharge = async (grant, req, res, next) => {
|
|||||||
// check if already a stripe customer
|
// check if already a stripe customer
|
||||||
let { stripe_id, subscription_id } = donors[0];
|
let { stripe_id, subscription_id } = donors[0];
|
||||||
|
|
||||||
if (!stripe_id) {
|
// if (!stripe_id) {
|
||||||
const customer = await stripe.customers.create({
|
// const customer = await stripe.customers.create({
|
||||||
source: stripeToken,
|
// source: stripeToken,
|
||||||
email: user.email
|
// email: user.email
|
||||||
});
|
// });
|
||||||
stripe_id = await customer.id;
|
// stripe_id = await customer.id;
|
||||||
|
|
||||||
await Donor.update({ stripe_id }, { where: { id: user.id } });
|
// await Donor.update({ stripe_id }, { where: { id: user.id } });
|
||||||
}
|
// }
|
||||||
// TODO: if there is, then update with the given source
|
// TODO: if there is, then update with the given source
|
||||||
|
|
||||||
var result;
|
var result;
|
||||||
@@ -40,9 +40,17 @@ exports.grantCharge = async (grant, req, res, next) => {
|
|||||||
let charge = await stripe.charges.create({
|
let charge = await stripe.charges.create({
|
||||||
amount,
|
amount,
|
||||||
currency: 'usd',
|
currency: 'usd',
|
||||||
customer: stripe_id,
|
source: 'tok_visa',
|
||||||
description: 'One time payment for grant',
|
description: 'One time payment for grant',
|
||||||
statement_descriptor: 'one-time-grant'
|
statement_descriptor: 'one-time-grant',
|
||||||
|
transfer_group: 'Grant ' + grant_id
|
||||||
|
});
|
||||||
|
|
||||||
|
let transfers = stripe.transfers.create({
|
||||||
|
amount: 7000,
|
||||||
|
currency: 'usd',
|
||||||
|
destination: '{CONNECTED_STRIPE_ACCOUNT_ID}',
|
||||||
|
transfer_group: '{ORDER10}'
|
||||||
});
|
});
|
||||||
|
|
||||||
await Charge.create({
|
await Charge.create({
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
const GrantsOrganizations = sequelize.define('grants_organizations', {
|
||||||
|
id: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true
|
||||||
|
},
|
||||||
|
amount: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return GrantsOrganizations;
|
||||||
|
};
|
||||||
@@ -4,8 +4,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
},
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
return OrgBank;
|
return OrgBank;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user