properly sending payments/charges
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
const db = require("../../models"),
|
||||
errorMaker = require("../helpers/error.maker");
|
||||
const db = require('../../models'),
|
||||
errorMaker = require('../helpers/error.maker');
|
||||
|
||||
const { Charge, sequelize } = db;
|
||||
|
||||
// Find grants with causes, regions, and organizations by donor_id
|
||||
exports.getAllCharges = async (req, res, next) => {
|
||||
const donor_id = req.user.id;
|
||||
exports.getAllChargesbyDonor = async (req, res, next) => {
|
||||
const donor_id = req.user.id;
|
||||
|
||||
try {
|
||||
const charges = await db.sequelize.query(
|
||||
`
|
||||
try {
|
||||
const charges = await db.sequelize.query(
|
||||
`
|
||||
SELECT
|
||||
g.name,
|
||||
g.monthly,
|
||||
g.num_causes,
|
||||
g.num_regions,
|
||||
c.description,
|
||||
c.amount,
|
||||
c.payment_status,
|
||||
c.created_at
|
||||
FROM grants as g
|
||||
INNER JOIN charges as c on c.grant_id = g.id
|
||||
WHERE g.donor_id = :donor_id;
|
||||
`,
|
||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||
);
|
||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
charges
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
return res.status(200).json({
|
||||
charges
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
+127
-123
@@ -1,115 +1,117 @@
|
||||
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,
|
||||
Charge,
|
||||
Cause,
|
||||
Region,
|
||||
Organization,
|
||||
GrantOrganization,
|
||||
Project,
|
||||
sequelize
|
||||
Grant,
|
||||
Charge,
|
||||
Cause,
|
||||
Region,
|
||||
Organization,
|
||||
GrantOrganization,
|
||||
Project,
|
||||
sequelize
|
||||
} = db;
|
||||
// Create a grant for certain donor
|
||||
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 regions = organizations.map(org => org.primary_region);
|
||||
const causes = organizations.map(org => org.primary_cause);
|
||||
const regions = organizations.map(org => org.primary_region);
|
||||
|
||||
actual_total = Math.round(amount * 100) / 100;
|
||||
actual_total = Math.round(amount * 100) / 100;
|
||||
|
||||
if (!stripeToken) {
|
||||
throw errorMaker(400, "No stripe token given");
|
||||
}
|
||||
if (!stripeToken) {
|
||||
throw errorMaker(400, 'No stripe token given');
|
||||
}
|
||||
|
||||
let transaction;
|
||||
let transaction;
|
||||
|
||||
try {
|
||||
// get transaction
|
||||
transaction = await sequelize.transaction();
|
||||
try {
|
||||
// get transaction
|
||||
transaction = await sequelize.transaction();
|
||||
|
||||
const grant = await Grant.create(
|
||||
{
|
||||
donor_id: user.id,
|
||||
name,
|
||||
amount: actual_total,
|
||||
monthly,
|
||||
num_causes: causes.length,
|
||||
num_regions: regions.length
|
||||
},
|
||||
{
|
||||
transaction
|
||||
}
|
||||
);
|
||||
const grant = await Grant.create(
|
||||
{
|
||||
donor_id: user.id,
|
||||
name,
|
||||
amount: actual_total,
|
||||
monthly,
|
||||
num_causes: causes.length,
|
||||
num_regions: regions.length
|
||||
},
|
||||
{
|
||||
transaction
|
||||
}
|
||||
);
|
||||
|
||||
const grantsOrgs = organizations.map(org => {
|
||||
return {
|
||||
grant_id: grant.id,
|
||||
organization_id: org.id,
|
||||
amount: org.amount
|
||||
};
|
||||
});
|
||||
const grantsOrgs = organizations.map(org => {
|
||||
return {
|
||||
grant_id: grant.id,
|
||||
organization_id: org.id,
|
||||
amount: org.amount
|
||||
};
|
||||
});
|
||||
|
||||
// mapping between bundle and orgs
|
||||
await GrantOrganization.bulkCreate(grantsOrgs, {
|
||||
transaction
|
||||
});
|
||||
// mapping between bundle and orgs
|
||||
await GrantOrganization.bulkCreate(grantsOrgs, {
|
||||
transaction
|
||||
});
|
||||
|
||||
// one time charge
|
||||
const charge = await stripe.grantCharge({
|
||||
grant,
|
||||
stripeToken,
|
||||
stripeToken,
|
||||
organizations,
|
||||
monthly,
|
||||
amount,
|
||||
user
|
||||
});
|
||||
// one time charge
|
||||
const charge = await stripe.grantCharge({
|
||||
grant,
|
||||
stripeToken,
|
||||
stripeToken,
|
||||
organizations,
|
||||
monthly,
|
||||
amount,
|
||||
user
|
||||
});
|
||||
|
||||
await Charge.create(
|
||||
{
|
||||
id: charge.id,
|
||||
amount,
|
||||
description: "One time payment for grant",
|
||||
grant_id: grant.id
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
await Charge.create(
|
||||
{
|
||||
id: charge.id,
|
||||
amount,
|
||||
description: 'One time payment for grant',
|
||||
grant_id: grant.id,
|
||||
payment_status: 'paid'
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
// send email
|
||||
await sendgrid.paymentReceipt({
|
||||
organizations,
|
||||
total_amount: actual_total,
|
||||
receiver: user.email
|
||||
});
|
||||
// send email
|
||||
await sendgrid.paymentReceipt({
|
||||
organizations,
|
||||
total_amount: actual_total,
|
||||
receiver: user.email
|
||||
});
|
||||
|
||||
// commit
|
||||
await transaction.commit();
|
||||
// commit
|
||||
await transaction.commit();
|
||||
|
||||
return res.status(200).json({
|
||||
message: "Successfully charged or subscribed",
|
||||
grant
|
||||
});
|
||||
} catch (error) {
|
||||
if (error) await transaction.rollback();
|
||||
next(error);
|
||||
}
|
||||
return res.status(200).json({
|
||||
message: 'Successfully charged or subscribed',
|
||||
grant
|
||||
});
|
||||
} catch (error) {
|
||||
if (error) await transaction.rollback();
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Find grants with causes, regions, and organizations by donor_id
|
||||
exports.findGrantsByDonorId = async (req, res, next) => {
|
||||
const donor_id = req.user.id;
|
||||
exports.getGrantsByDonorId = async (req, res, next) => {
|
||||
const donor_id = req.user.id;
|
||||
|
||||
try {
|
||||
const grants = await db.sequelize.query(
|
||||
`
|
||||
try {
|
||||
const grants = await db.sequelize.query(
|
||||
`
|
||||
Select
|
||||
g.id,
|
||||
g.name,
|
||||
@@ -117,7 +119,9 @@ exports.findGrantsByDonorId = async (req, res, next) => {
|
||||
g.monthly,
|
||||
g.num_causes,
|
||||
g.num_regions,
|
||||
g.donor_id,
|
||||
g.donor_id,
|
||||
g.created_at,
|
||||
g.updated_at,
|
||||
JSON_ARRAYAGG(o.id) AS organizations_ids,
|
||||
JSON_ARRAYAGG(o.name) AS organization_names,
|
||||
JSON_ARRAYAGG(go.amount) AS organization_amounts,
|
||||
@@ -129,47 +133,47 @@ exports.findGrantsByDonorId = async (req, res, next) => {
|
||||
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 } }
|
||||
);
|
||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||
);
|
||||
|
||||
// adding in the organizations, causes, and regions for each grant into arrays
|
||||
grants.map(grant => {
|
||||
grant.organizations = [];
|
||||
// adding in the organizations, causes, and regions for each grant into arrays
|
||||
grants.map(grant => {
|
||||
grant.organizations = [];
|
||||
|
||||
for (var i = 0; i < grant.organization_names.length; i++) {
|
||||
grant.organizations.push({
|
||||
id: grant.organizations_ids[i],
|
||||
name: grant.organization_names[i],
|
||||
amount: grant.organization_amounts[i]
|
||||
});
|
||||
}
|
||||
for (var i = 0; i < grant.organization_names.length; i++) {
|
||||
grant.organizations.push({
|
||||
id: grant.organizations_ids[i],
|
||||
name: grant.organization_names[i],
|
||||
amount: grant.organization_amounts[i]
|
||||
});
|
||||
}
|
||||
|
||||
grant.monthly = grant.monthly == 1 ? true : false;
|
||||
});
|
||||
grant.monthly = grant.monthly == 1 ? true : false;
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
grants
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
return res.status(200).json({
|
||||
grants
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
exports.deleteGrant = (req, res, next) => {
|
||||
const { grant_id } = req.body;
|
||||
const user = req.user;
|
||||
const { grant_id } = req.body;
|
||||
const user = req.user;
|
||||
|
||||
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
|
||||
.then(result => {
|
||||
var message = "Already Deleted";
|
||||
if (result) {
|
||||
message = "Grant Deleted";
|
||||
}
|
||||
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
|
||||
.then(result => {
|
||||
var message = 'Already Deleted';
|
||||
if (result) {
|
||||
message = 'Grant Deleted';
|
||||
}
|
||||
|
||||
res.status(201).json({
|
||||
result,
|
||||
message
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
res.status(201).json({
|
||||
result,
|
||||
message
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
+26
-26
@@ -1,58 +1,58 @@
|
||||
const express = require("express"),
|
||||
router = express.Router(),
|
||||
checkAuth = require("../middleware/check-auth"),
|
||||
roles = require("../helpers/roles");
|
||||
const express = require('express'),
|
||||
router = express.Router(),
|
||||
checkAuth = require('../middleware/check-auth'),
|
||||
roles = require('../helpers/roles');
|
||||
|
||||
const {
|
||||
donor,
|
||||
grant,
|
||||
stripe,
|
||||
organization,
|
||||
charge
|
||||
} = require("../controllers");
|
||||
donor,
|
||||
grant,
|
||||
stripe,
|
||||
organization,
|
||||
charge
|
||||
} = require('../controllers');
|
||||
|
||||
// POST donor signup
|
||||
router.post("/", donor.createDonor);
|
||||
router.post('/', donor.createDonor);
|
||||
|
||||
// GET all Donors
|
||||
router.get("/", checkAuth(roles.ADMIN), donor.getAllDonors);
|
||||
router.get('/', checkAuth(roles.ADMIN), donor.getAllDonors);
|
||||
|
||||
// GET grants with causes, regions, charities by donor_id
|
||||
router.get("/grants/", checkAuth(roles.DONOR), grant.findGrantsByDonorId);
|
||||
router.get('/grants/', checkAuth(roles.DONOR), grant.getGrantsByDonorId);
|
||||
|
||||
/** POST Create grants with following body:
|
||||
* - List of id's of selected causes, regions, charities
|
||||
* - Monthly: true or false
|
||||
* - donor_id
|
||||
* */
|
||||
router.post("/grants/", checkAuth(roles.DONOR), grant.createGrant);
|
||||
router.post('/grants/', checkAuth(roles.DONOR), grant.createGrant);
|
||||
|
||||
// DELETE a grant of a donor
|
||||
router.delete(
|
||||
"/grants/",
|
||||
checkAuth(roles.DONOR),
|
||||
stripe.deleteGrant,
|
||||
grant.deleteGrant
|
||||
'/grants/',
|
||||
checkAuth(roles.DONOR),
|
||||
stripe.deleteGrant,
|
||||
grant.deleteGrant
|
||||
);
|
||||
|
||||
// POST to get suggested organizations to distribute to
|
||||
// running "the algorithm"
|
||||
router.post(
|
||||
"/organizations/",
|
||||
checkAuth(roles.DONOR),
|
||||
organization.findSuggestedCharities
|
||||
'/organizations/',
|
||||
checkAuth(roles.DONOR),
|
||||
organization.findSuggestedCharities
|
||||
);
|
||||
|
||||
// GET min and optimal amount to choose
|
||||
router.get(
|
||||
"/organizations/amounts",
|
||||
checkAuth(roles.DONOR),
|
||||
organization.findOptimalBundleAmounts
|
||||
'/organizations/amounts',
|
||||
checkAuth(roles.DONOR),
|
||||
organization.findOptimalBundleAmounts
|
||||
);
|
||||
|
||||
router.get("/dashboard", checkAuth(roles.DONOR), donor.getDashboardData);
|
||||
router.get('/dashboard', checkAuth(roles.DONOR), donor.getDashboardData);
|
||||
|
||||
router.get("/charges", checkAuth(roles.DONOR), charge.getAllCharges);
|
||||
router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor);
|
||||
|
||||
// // Retrieve a single Donor by Id
|
||||
// router.get('/:donor_id', donor.findById);
|
||||
|
||||
+16
-16
@@ -1,21 +1,21 @@
|
||||
"use strict";
|
||||
const PaymentStatus = require("./PaymentStatus");
|
||||
'use strict';
|
||||
const PaymentStatus = require('./PaymentStatus');
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Charge = sequelize.define("Charge", {
|
||||
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
|
||||
description: DataTypes.STRING,
|
||||
amount: DataTypes.INTEGER,
|
||||
const Charge = sequelize.define('Charge', {
|
||||
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
|
||||
description: DataTypes.STRING,
|
||||
amount: DataTypes.INTEGER,
|
||||
|
||||
payment_status: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: PaymentStatus(sequelize, DataTypes),
|
||||
key: "name"
|
||||
}
|
||||
}
|
||||
});
|
||||
payment_status: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: PaymentStatus(sequelize, DataTypes),
|
||||
key: 'name'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Charge;
|
||||
return Charge;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.bulkInsert('paymentstatuses', [
|
||||
{
|
||||
name: 'pending',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date()
|
||||
},
|
||||
{
|
||||
name: 'paid',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date()
|
||||
},
|
||||
{
|
||||
name: 'failed',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date()
|
||||
}
|
||||
]);
|
||||
},
|
||||
|
||||
down: (queryInterface, Sequelize) => {
|
||||
return queryInterface.bulkDelete('paymentstatuses', [
|
||||
{
|
||||
name: 'pending'
|
||||
},
|
||||
{
|
||||
name: 'paid'
|
||||
},
|
||||
{
|
||||
name: 'failed'
|
||||
}
|
||||
]);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user