Merge branch 'entire-grant-flow-fixes'
This commit is contained in:
@@ -63,9 +63,20 @@ exports.grantCharge = async (grant, req, res, next) => {
|
|||||||
|
|
||||||
// either create new sub with new plan, or append plan to existing sub
|
// either create new sub with new plan, or append plan to existing sub
|
||||||
if (!subscription_id) {
|
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({
|
let subscription = await stripe.subscriptions.create({
|
||||||
customer: stripe_id,
|
customer: stripe_id,
|
||||||
items: [{ plan: default_plan_id }]
|
items: [{ plan: default_plan_id }],
|
||||||
|
billing_cycle_anchor: unixFirstNextMonth,
|
||||||
|
trial_end: unixFirstNextMonth
|
||||||
});
|
});
|
||||||
|
|
||||||
subscription_id = subscription.id;
|
subscription_id = subscription.id;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const db = require('../../config/db.config.js'),
|
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;
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
|
|
||||||
@@ -7,6 +8,11 @@ const { Grant, Cause, Region, Organization } = db;
|
|||||||
exports.findAll = (req, res, next) => {
|
exports.findAll = (req, res, next) => {
|
||||||
Cause.findAll()
|
Cause.findAll()
|
||||||
.then(causes => {
|
.then(causes => {
|
||||||
|
causes = causes.map(cause => {
|
||||||
|
cause.name = textCleaner.titleCase(cause.name);
|
||||||
|
return cause;
|
||||||
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
causes,
|
causes,
|
||||||
number_items: causes.length
|
number_items: causes.length
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const db = require('../../config/db.config.js'),
|
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;
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
|
|
||||||
@@ -7,6 +8,11 @@ const { Grant, Cause, Region, Organization } = db;
|
|||||||
exports.findAll = (req, res, next) => {
|
exports.findAll = (req, res, next) => {
|
||||||
Region.findAll()
|
Region.findAll()
|
||||||
.then(regions => {
|
.then(regions => {
|
||||||
|
regions = regions.map(region => {
|
||||||
|
region.name = textCleaner.titleCase(region.name);
|
||||||
|
return region;
|
||||||
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
regions,
|
regions,
|
||||||
number_items: regions.length
|
number_items: regions.length
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ const { Grant, Cause, Region, Organization } = db;
|
|||||||
|
|
||||||
// POST create an organization
|
// POST create an organization
|
||||||
// Temporarily only use: name, email, password, short_description, primary_cause, primary_region
|
// Temporarily only use: name, email, password, short_description, primary_cause, primary_region
|
||||||
exports.create = (req, res, next) => {
|
exports.create = async (req, res, next) => {
|
||||||
const {
|
let {
|
||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
@@ -16,56 +16,64 @@ exports.create = (req, res, next) => {
|
|||||||
primary_region
|
primary_region
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
// see if organization already in db
|
primary_cause = primary_cause.trim().toLowerCase();
|
||||||
Organization.findAll({
|
primary_region = primary_region.trim().toLowerCase();
|
||||||
where: {
|
|
||||||
email
|
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 => {
|
const CAUSE_QUERY = `SELECT name FROM causes
|
||||||
if (orgs.length >= 1) {
|
WHERE name = :primary_cause`;
|
||||||
return next(errorMaker(409, `Email Exists: ${email}`));
|
const causes = await db.sequelize.query(CAUSE_QUERY, {
|
||||||
} else {
|
replacements: { primary_cause },
|
||||||
const QUERY = `SELECT c.name, r.name \
|
type: db.Sequelize.QueryTypes.SELECT
|
||||||
FROM causes AS c, regions AS r
|
});
|
||||||
WHERE c.name = :primary_cause AND r.name = :primary_region`;
|
|
||||||
return db.sequelize
|
const REGION_QUERY = `SELECT name FROM regions
|
||||||
.query(QUERY, {
|
WHERE name = :primary_region`;
|
||||||
replacements: { primary_cause, primary_region },
|
const regions = await db.sequelize.query(REGION_QUERY, {
|
||||||
type: db.Sequelize.QueryTypes.SELECT
|
replacements: { primary_region },
|
||||||
})
|
type: db.Sequelize.QueryTypes.SELECT
|
||||||
.then(num => {
|
});
|
||||||
if (num.length < 1) {
|
|
||||||
// could not find the cause or region in the db
|
// could not find the cause or region in the db, so add
|
||||||
return next(errorMaker(401, `Not a valid cause or region`));
|
if (!causes.length) {
|
||||||
} else {
|
await Cause.create({ name: primary_cause }, { transaction });
|
||||||
// hash and store
|
}
|
||||||
return bcrypt.hash(password, null, null, function(error, hash) {
|
if (!regions.length) {
|
||||||
// Store hash in your password DB.
|
await Region.create({ name: primary_region }, { transaction });
|
||||||
if (error) {
|
}
|
||||||
return next(error);
|
|
||||||
} else {
|
const saltRounds = await bcrypt.genSaltSync(10);
|
||||||
Organization.create({
|
// hash
|
||||||
name,
|
const hashedPass = await bcrypt.hashSync(password, saltRounds);
|
||||||
email,
|
|
||||||
password: hash, //hashed password
|
// Store hash in DB
|
||||||
short_description,
|
const org = await Organization.create({
|
||||||
primary_cause,
|
name,
|
||||||
primary_region
|
email,
|
||||||
})
|
password: hashedPass, //hashed password
|
||||||
.then(org => {
|
short_description,
|
||||||
// Send created org to client
|
primary_cause,
|
||||||
return res.status(201).json({
|
primary_region
|
||||||
message: 'Organization created',
|
});
|
||||||
org
|
|
||||||
});
|
await transaction.commit();
|
||||||
})
|
|
||||||
.catch(error => next(error));
|
return res.status(201).json({
|
||||||
}
|
message: 'Organization created',
|
||||||
});
|
org
|
||||||
}
|
});
|
||||||
});
|
} catch (error) {
|
||||||
}
|
await transaction.rollback();
|
||||||
})
|
next(error);
|
||||||
.catch(error => next(error));
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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(' ');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user