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"),
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) => {
exports.getAllChargesbyDonor = async (req, res, next) => {
const donor_id = req.user.id;
try {
@@ -13,9 +13,9 @@ exports.getAllCharges = async (req, res, next) => {
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
+15 -11
View File
@@ -1,8 +1,8 @@
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,
@@ -26,7 +26,7 @@ exports.createGrant = async (req, res, next) => {
actual_total = Math.round(amount * 100) / 100;
if (!stripeToken) {
throw errorMaker(400, "No stripe token given");
throw errorMaker(400, 'No stripe token given');
}
let transaction;
@@ -77,8 +77,9 @@ exports.createGrant = async (req, res, next) => {
{
id: charge.id,
amount,
description: "One time payment for grant",
grant_id: grant.id
description: 'One time payment for grant',
grant_id: grant.id,
payment_status: 'paid'
},
{ transaction }
);
@@ -94,17 +95,18 @@ exports.createGrant = async (req, res, next) => {
await transaction.commit();
return res.status(200).json({
message: "Successfully charged or subscribed",
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) => {
exports.getGrantsByDonorId = async (req, res, next) => {
const donor_id = req.user.id;
try {
@@ -118,6 +120,8 @@ exports.findGrantsByDonorId = async (req, res, next) => {
g.num_causes,
g.num_regions,
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,
@@ -161,9 +165,9 @@ exports.deleteGrant = (req, res, next) => {
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
.then(result => {
var message = "Already Deleted";
var message = 'Already Deleted';
if (result) {
message = "Grant Deleted";
message = 'Grant Deleted';
}
res.status(201).json({
+13 -13
View File
@@ -1,7 +1,7 @@
const express = require("express"),
const express = require('express'),
router = express.Router(),
checkAuth = require("../middleware/check-auth"),
roles = require("../helpers/roles");
checkAuth = require('../middleware/check-auth'),
roles = require('../helpers/roles');
const {
donor,
@@ -9,27 +9,27 @@ const {
stripe,
organization,
charge
} = require("../controllers");
} = 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/",
'/grants/',
checkAuth(roles.DONOR),
stripe.deleteGrant,
grant.deleteGrant
@@ -38,21 +38,21 @@ router.delete(
// POST to get suggested organizations to distribute to
// running "the algorithm"
router.post(
"/organizations/",
'/organizations/',
checkAuth(roles.DONOR),
organization.findSuggestedCharities
);
// GET min and optimal amount to choose
router.get(
"/organizations/amounts",
'/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);
+4 -4
View File
@@ -1,8 +1,8 @@
"use strict";
const PaymentStatus = require("./PaymentStatus");
'use strict';
const PaymentStatus = require('./PaymentStatus');
module.exports = (sequelize, DataTypes) => {
const Charge = sequelize.define("Charge", {
const Charge = sequelize.define('Charge', {
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
description: DataTypes.STRING,
amount: DataTypes.INTEGER,
@@ -12,7 +12,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false,
references: {
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'
}
]);
}
};