From 17706cb1a08504cb18c2d405c5d30bb181f8ca97 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sun, 3 Feb 2019 22:15:04 -0800 Subject: [PATCH] adding proper billing cycle and cleaning org signup --- app/controllers/donor/stripe.controller.js | 13 +- app/controllers/general/cause.controller.js | 8 +- app/controllers/general/region.controller.js | 8 +- .../organization/organization.controller.js | 114 ++++++++++-------- app/helpers/text_cleaner.js | 14 +++ 5 files changed, 101 insertions(+), 56 deletions(-) create mode 100644 app/helpers/text_cleaner.js diff --git a/app/controllers/donor/stripe.controller.js b/app/controllers/donor/stripe.controller.js index 384e259..d115dd1 100644 --- a/app/controllers/donor/stripe.controller.js +++ b/app/controllers/donor/stripe.controller.js @@ -63,9 +63,20 @@ exports.grantCharge = async (grant, req, res, next) => { // either create new sub with new plan, or append plan to existing sub if (!subscription_id) { + const currDate = new Date(); + const unixFirstNextMonth = Math.round( + new Date( + currDate.getFullYear(), + currDate.getMonth() + 1, + 1 + ).getTime() / 1000 + ); + let subscription = await stripe.subscriptions.create({ customer: stripe_id, - items: [{ plan: default_plan_id }] + items: [{ plan: default_plan_id }], + billing_cycle_anchor: unixFirstNextMonth, + trial_end: unixFirstNextMonth }); subscription_id = subscription.id; diff --git a/app/controllers/general/cause.controller.js b/app/controllers/general/cause.controller.js index 73b5923..c454901 100644 --- a/app/controllers/general/cause.controller.js +++ b/app/controllers/general/cause.controller.js @@ -1,5 +1,6 @@ const db = require('../../config/db.config.js'), - errorMaker = require('../../helpers/error.maker'); + errorMaker = require('../../helpers/error.maker'), + textCleaner = require('../../helpers/text_cleaner'); const { Grant, Cause, Region, Organization } = db; @@ -7,6 +8,11 @@ const { Grant, Cause, Region, Organization } = db; exports.findAll = (req, res, next) => { Cause.findAll() .then(causes => { + causes = causes.map(cause => { + cause.name = textCleaner.titleCase(cause.name); + return cause; + }); + res.status(200).json({ causes, number_items: causes.length diff --git a/app/controllers/general/region.controller.js b/app/controllers/general/region.controller.js index c4b4db0..3d554e7 100644 --- a/app/controllers/general/region.controller.js +++ b/app/controllers/general/region.controller.js @@ -1,5 +1,6 @@ const db = require('../../config/db.config.js'), - errorMaker = require('../../helpers/error.maker'); + errorMaker = require('../../helpers/error.maker'), + textCleaner = require('../../helpers/text_cleaner'); const { Grant, Cause, Region, Organization } = db; @@ -7,6 +8,11 @@ const { Grant, Cause, Region, Organization } = db; exports.findAll = (req, res, next) => { Region.findAll() .then(regions => { + regions = regions.map(region => { + region.name = textCleaner.titleCase(region.name); + return region; + }); + res.status(200).json({ regions, number_items: regions.length diff --git a/app/controllers/organization/organization.controller.js b/app/controllers/organization/organization.controller.js index 4281d5f..d2cf50a 100644 --- a/app/controllers/organization/organization.controller.js +++ b/app/controllers/organization/organization.controller.js @@ -6,8 +6,8 @@ const { Grant, Cause, Region, Organization } = db; // POST create an organization // Temporarily only use: name, email, password, short_description, primary_cause, primary_region -exports.create = (req, res, next) => { - const { +exports.create = async (req, res, next) => { + let { name, email, password, @@ -16,56 +16,64 @@ exports.create = (req, res, next) => { primary_region } = req.body; - // see if organization already in db - Organization.findAll({ - where: { - email + primary_cause = primary_cause.trim().toLowerCase(); + primary_region = primary_region.trim().toLowerCase(); + + let transaction; + + try { + transaction = await db.sequelize.transaction(); + + const orgs = await Organization.findAll({ where: { email } }); + + if (orgs.length >= 1) { + return next(errorMaker(409, `Email Exists: ${email}`)); } - }) - .then(orgs => { - if (orgs.length >= 1) { - return next(errorMaker(409, `Email Exists: ${email}`)); - } else { - const QUERY = `SELECT c.name, r.name \ - FROM causes AS c, regions AS r - WHERE c.name = :primary_cause AND r.name = :primary_region`; - return db.sequelize - .query(QUERY, { - replacements: { primary_cause, primary_region }, - type: db.Sequelize.QueryTypes.SELECT - }) - .then(num => { - if (num.length < 1) { - // could not find the cause or region in the db - return next(errorMaker(401, `Not a valid cause or region`)); - } else { - // hash and store - return bcrypt.hash(password, null, null, function(error, hash) { - // Store hash in your password DB. - if (error) { - return next(error); - } else { - Organization.create({ - name, - email, - password: hash, //hashed password - short_description, - primary_cause, - primary_region - }) - .then(org => { - // Send created org to client - return res.status(201).json({ - message: 'Organization created', - org - }); - }) - .catch(error => next(error)); - } - }); - } - }); - } - }) - .catch(error => next(error)); + + const CAUSE_QUERY = `SELECT name FROM causes + WHERE name = :primary_cause`; + const causes = await db.sequelize.query(CAUSE_QUERY, { + replacements: { primary_cause }, + type: db.Sequelize.QueryTypes.SELECT + }); + + const REGION_QUERY = `SELECT name FROM regions + WHERE name = :primary_region`; + const regions = await db.sequelize.query(REGION_QUERY, { + replacements: { primary_region }, + type: db.Sequelize.QueryTypes.SELECT + }); + + // could not find the cause or region in the db, so add + if (!causes.length) { + await Cause.create({ name: primary_cause }, { transaction }); + } + if (!regions.length) { + await Region.create({ name: primary_region }, { transaction }); + } + + const saltRounds = await bcrypt.genSaltSync(10); + // hash + const hashedPass = await bcrypt.hashSync(password, saltRounds); + + // Store hash in DB + const org = await Organization.create({ + name, + email, + password: hashedPass, //hashed password + short_description, + primary_cause, + primary_region + }); + + await transaction.commit(); + + return res.status(201).json({ + message: 'Organization created', + org + }); + } catch (error) { + await transaction.rollback(); + next(error); + } }; diff --git a/app/helpers/text_cleaner.js b/app/helpers/text_cleaner.js new file mode 100644 index 0000000..d83b206 --- /dev/null +++ b/app/helpers/text_cleaner.js @@ -0,0 +1,14 @@ +module.exports = { + titleCase: function(str) { + var splitStr = str.toLowerCase().split(' '); + + for (var i = 0; i < splitStr.length; i++) { + // You do not need to check if i is larger than splitStr length, as your for does that for you + // Assign it back to the array + splitStr[i] = + splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); + } + // Directly return the joined string + return splitStr.join(' '); + } +};