From c64c044961da7aba51ed59673aa85d142f02a0c7 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sat, 2 Feb 2019 22:14:55 -0800 Subject: [PATCH] got delete and create grants all in two routes --- app/controllers/donor/grant.controller.js | 4 ++-- app/controllers/donor/stripe.controller.js | 17 ++++++++--------- app/routes/donor.route.js | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/controllers/donor/grant.controller.js b/app/controllers/donor/grant.controller.js index 84e8518..e850b09 100644 --- a/app/controllers/donor/grant.controller.js +++ b/app/controllers/donor/grant.controller.js @@ -35,8 +35,8 @@ exports.create = (req, res, next) => { // message: 'Grant Created' // }); - req.grant = grants.dataValues; - next(); + next(grants.dataValues); + return null; }) .catch(function(error) { // transaction rollback diff --git a/app/controllers/donor/stripe.controller.js b/app/controllers/donor/stripe.controller.js index c329eff..6214993 100644 --- a/app/controllers/donor/stripe.controller.js +++ b/app/controllers/donor/stripe.controller.js @@ -6,9 +6,9 @@ const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj'); const { Donor, Charge, PaymentPlan } = db; // Subscribe user to plan or one time charge -exports.grantCharge = async (req, res, next) => { +exports.grantCharge = async (grant, req, res, next) => { const { stripeToken, monthly } = req.body; - const { grant_id } = req.grant; + const grant_id = await grant.id; const amount = req.body.amount * 100; //stripe standards @@ -54,7 +54,6 @@ exports.grantCharge = async (req, res, next) => { result = charge; } else { const plan = await stripe.plans.create({ - id: grant_id, nickname: `Plan for grant: ${grant_id}`, product: product_id, currency: 'usd', @@ -109,19 +108,19 @@ exports.deleteGrant = async (req, res, next) => { // check if there are plans for that grant if (!paymentPlans.length) { - res.status(400).json({ + return res.status(400).json({ message: 'Not a valid grant' }); } else { const plan = paymentPlans[0]; + //delete the plan/grant under the subscription for the user await stripe.subscriptionItems.del(plan.sub_item_id); - - res.status(201).json({ - message: 'Removed monthly subscription plan', - plan - }); + //delete the plan from the main product 'Grants' + await stripe.plans.del(plan.plan_id); } + + next(); } catch (error) { next(error); } diff --git a/app/routes/donor.route.js b/app/routes/donor.route.js index c024179..be6a408 100644 --- a/app/routes/donor.route.js +++ b/app/routes/donor.route.js @@ -27,7 +27,7 @@ router.post( controllers.stripe.grantCharge ); -// DELECT a grant of a donor +// DELETE a grant of a donor router.delete( '/grants/', checkAuth(roles.DONOR),