adding payment status to a charge and getting all charges for a donor

This commit is contained in:
Arjun Patel
2019-03-27 09:46:10 -07:00
parent 2caed4fc4c
commit 02c1b2c603
5 changed files with 102 additions and 37 deletions
+33
View File
@@ -0,0 +1,33 @@
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;
try {
const charges = await db.sequelize.query(
`
SELECT
g.name,
g.monthly,
g.num_causes,
g.num_regions,
c.amount,
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 } }
);
return res.status(200).json({
charges
});
} catch (error) {
next(error);
}
};
+10 -9
View File
@@ -1,11 +1,12 @@
module.exports = {
donor: require('./donor.controller'),
grant: require('./grant.controller'),
organization: require('./organization.controller'),
stripe: require('./stripe.controller'),
sendgrid: require('./sendgrid.controller'),
region: require('./region.controller'),
cause: require('./cause.controller'),
user: require('./user.controller'),
auth: require('./auth.controller')
donor: require("./donor.controller"),
grant: require("./grant.controller"),
organization: require("./organization.controller"),
stripe: require("./stripe.controller"),
sendgrid: require("./sendgrid.controller"),
region: require("./region.controller"),
cause: require("./cause.controller"),
user: require("./user.controller"),
auth: require("./auth.controller"),
charge: require("./charge.controller")
};
+28 -20
View File
@@ -1,50 +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 } = require('../controllers');
const {
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.findGrantsByDonorId);
/** 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);
// // Retrieve a single Donor by Id
// router.get('/:donor_id', donor.findById);
+19 -8
View File
@@ -1,10 +1,21 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Charge = sequelize.define('Charge', {
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
description: DataTypes.STRING,
amount: DataTypes.INTEGER
});
"use strict";
const PaymentStatus = require("./PaymentStatus");
return Charge;
module.exports = (sequelize, DataTypes) => {
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"
}
}
});
return Charge;
};
+12
View File
@@ -0,0 +1,12 @@
"use strict";
module.exports = (sequelize, DataTypes) => {
const PaymentStatus = sequelize.define("PaymentStatus", {
name: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
}
});
return PaymentStatus;
};