properly sending payments/charges

This commit is contained in:
Arjun Patel
2019-04-02 09:47:35 -07:00
parent 02c1b2c603
commit f963ae45da
5 changed files with 223 additions and 182 deletions
+5 -5
View File
@@ -1,10 +1,10 @@
const db = require("../../models"), const db = require('../../models'),
errorMaker = require("../helpers/error.maker"); errorMaker = require('../helpers/error.maker');
const { Charge, sequelize } = db; const { Charge, sequelize } = db;
// Find grants with causes, regions, and organizations by donor_id // Find grants with causes, regions, and organizations by donor_id
exports.getAllCharges = async (req, res, next) => { exports.getAllChargesbyDonor = async (req, res, next) => {
const donor_id = req.user.id; const donor_id = req.user.id;
try { try {
@@ -13,9 +13,9 @@ exports.getAllCharges = async (req, res, next) => {
SELECT SELECT
g.name, g.name,
g.monthly, g.monthly,
g.num_causes, c.description,
g.num_regions,
c.amount, c.amount,
c.payment_status,
c.created_at c.created_at
FROM grants as g FROM grants as g
INNER JOIN charges as c on c.grant_id = g.id INNER JOIN charges as c on c.grant_id = g.id
+15 -11
View File
@@ -1,8 +1,8 @@
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,
@@ -26,7 +26,7 @@ exports.createGrant = async (req, res, next) => {
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;
@@ -77,8 +77,9 @@ exports.createGrant = async (req, res, next) => {
{ {
id: charge.id, id: charge.id,
amount, amount,
description: "One time payment for grant", description: 'One time payment for grant',
grant_id: grant.id grant_id: grant.id,
payment_status: 'paid'
}, },
{ transaction } { transaction }
); );
@@ -94,17 +95,18 @@ exports.createGrant = async (req, res, next) => {
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();
console.log(error);
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.getGrantsByDonorId = async (req, res, next) => {
const donor_id = req.user.id; const donor_id = req.user.id;
try { try {
@@ -118,6 +120,8 @@ 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,
g.updated_at,
JSON_ARRAYAGG(o.id) AS organizations_ids, JSON_ARRAYAGG(o.id) AS organizations_ids,
JSON_ARRAYAGG(o.name) AS organization_names, JSON_ARRAYAGG(o.name) AS organization_names,
JSON_ARRAYAGG(go.amount) AS organization_amounts, JSON_ARRAYAGG(go.amount) AS organization_amounts,
@@ -161,9 +165,9 @@ exports.deleteGrant = (req, res, next) => {
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({
+13 -13
View File
@@ -1,7 +1,7 @@
const express = require("express"), const express = require('express'),
router = express.Router(), router = express.Router(),
checkAuth = require("../middleware/check-auth"), checkAuth = require('../middleware/check-auth'),
roles = require("../helpers/roles"); roles = require('../helpers/roles');
const { const {
donor, donor,
@@ -9,27 +9,27 @@ const {
stripe, stripe,
organization, organization,
charge charge
} = require("../controllers"); } = require('../controllers');
// POST donor signup // POST donor signup
router.post("/", donor.createDonor); router.post('/', donor.createDonor);
// GET all Donors // 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 // 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: /** POST Create grants with following body:
* - List of id's of selected causes, regions, charities * - List of id's of selected causes, regions, charities
* - Monthly: true or false * - Monthly: true or false
* - donor_id * - donor_id
* */ * */
router.post("/grants/", checkAuth(roles.DONOR), grant.createGrant); router.post('/grants/', checkAuth(roles.DONOR), grant.createGrant);
// DELETE a grant of a donor // DELETE a grant of a donor
router.delete( router.delete(
"/grants/", '/grants/',
checkAuth(roles.DONOR), checkAuth(roles.DONOR),
stripe.deleteGrant, stripe.deleteGrant,
grant.deleteGrant grant.deleteGrant
@@ -38,21 +38,21 @@ router.delete(
// POST to get suggested organizations to distribute to // POST to get suggested organizations to distribute to
// running "the algorithm" // running "the algorithm"
router.post( router.post(
"/organizations/", '/organizations/',
checkAuth(roles.DONOR), checkAuth(roles.DONOR),
organization.findSuggestedCharities organization.findSuggestedCharities
); );
// GET min and optimal amount to choose // GET min and optimal amount to choose
router.get( router.get(
"/organizations/amounts", '/organizations/amounts',
checkAuth(roles.DONOR), checkAuth(roles.DONOR),
organization.findOptimalBundleAmounts 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 // // Retrieve a single Donor by Id
// router.get('/:donor_id', donor.findById); // router.get('/:donor_id', donor.findById);
+4 -4
View File
@@ -1,8 +1,8 @@
"use strict"; 'use strict';
const PaymentStatus = require("./PaymentStatus"); const PaymentStatus = require('./PaymentStatus');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Charge = sequelize.define("Charge", { const Charge = sequelize.define('Charge', {
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false }, id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
description: DataTypes.STRING, description: DataTypes.STRING,
amount: DataTypes.INTEGER, amount: DataTypes.INTEGER,
@@ -12,7 +12,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false, allowNull: false,
references: { references: {
model: PaymentStatus(sequelize, DataTypes), model: PaymentStatus(sequelize, DataTypes),
key: "name" key: 'name'
} }
} }
}); });
@@ -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'
}
]);
}
};