Merge branch 'entire-grant-flow-fixes'

This commit is contained in:
Arjun Patel
2019-02-04 23:02:22 -08:00
5 changed files with 101 additions and 56 deletions
+12 -1
View File
@@ -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;
+7 -1
View File
@@ -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
+7 -1
View File
@@ -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
@@ -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);
}
};
+14
View File
@@ -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(' ');
}
};