Skip to content

Commit

Permalink
😒 merge session pr
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnath committed Dec 13, 2016
2 parents e7144e3 + 37b97b3 commit 4a4f031
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
75 changes: 53 additions & 22 deletions lib/routes/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const multipart = require('connect-multiparty');
const uuid = require('uuid');
const _ = require('lodash');
const path = require('path');
const isUUID = require('validator/lib/isUUID');

const utils = require('../utils');
const database = require('../database');
Expand Down Expand Up @@ -188,19 +189,18 @@ const routes = application => {
* @param {object} next - Express callback
*/
app.get(`/${config.content.base}/:type/:id/:revision/${config.content.actions.edit}`, (req, res, next) => {
const errors = _.get(req.session, `form.content.add[${req.params.type.toLowerCase()}].errors`, {});
const idSess = _.get(req.session, `form.content.add[${req.params.type.toLowerCase()}].id`, {});
const revisionSess = _.get(req.session, `form.content.add[${req.params.type.toLowerCase()}].revision`, {});
const values = _.get(req.session, `form.content.add[${req.params.type.toLowerCase()}].content`, {});
const errors = _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].errors`, {});
const id = _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].id`, _.get(req, 'params.id'));
const revision = _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].revision`, _.get(req, 'params.revision'));
const values = _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].content`, {});
let data = {};

const steps = _.cloneDeep(req.content.workflow).steps.reverse();

// new revision, re-start workflow process
const step = steps[steps.length - 1];

_.unset(req.session, 'form.content.add');
_.set(req.session, 'referrer', req.get('Referrer'));
_.unset(req.session, 'form.content.edit');

// find all file inputs
const filers = req.content.type.attributes.map(attr => {
Expand Down Expand Up @@ -229,10 +229,10 @@ const routes = application => {
// something went wrong on save:
if (Object.keys(values).length > 0) {
// add the previous session data back in
_.set(req.session, 'form.content.add', {
[req.params.type.toLowerCase()]: {
id: idSess,
revision: revisionSess,
_.set(req.session, 'form.content.edit', {
[req.content.type.id]: {
id,
revision,
},
});

Expand Down Expand Up @@ -283,7 +283,7 @@ const routes = application => {
data = rows[0].value;

// add session data for this content
_.set(req.session, 'form.content.add', {
_.set(req.session, 'form.content.edit', {
[req.params.type.toLowerCase()]: {
id: rows[0].id,
revision: rows[0].revision,
Expand Down Expand Up @@ -341,18 +341,38 @@ const routes = application => {
* @param {object} next - Express callback
*/
app.post(`/${config.content.base}/:type/${config.content.actions.save}`, multipartMiddleware, (req, res, next) => {
const referrer = _.get(req.session, 'referrer') || `/${config.content.base}/${req.params.type.toLowerCase()}`;
const referrer = _.get(req.session, 'referrer') || req.get('Referrer');
let audits;
let publishable = false;
let check = 'publish';
let files = [];

_.unset(req.session, 'referrer');
let source = {};

if (req.body.submit === config.content.actions.new) {
check = 'save';
}

// determine data source from referrer
if (_.includes(referrer, '/add')) {
source.type = 'add';
}
else if (_.includes(referrer, '/edit')) {
source = {
type: 'edit',
id: _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].id`, ''),
};
}
else {
// if neither edit or add, something nefarious is afoot
const err = {
message: 'You may only save from an edit or add form. For now...',
safe: `/${config.applications.base}/${req.content.type.id}`,
status: 500,
};

return next(err);
}

// Validation
const validated = types.form.validate(req.body, req.content.type, check);

Expand All @@ -365,11 +385,13 @@ const routes = application => {
publishable = true;
}

// id
let id = uuid.v4();
const sessionId = _.get(req.session, `form.content.add[${req.params.type.toLowerCase()}].id`, '');
if (sessionId) {
id = sessionId;
// determine piece-of-content's id
let id;
if (source.type === 'edit' && isUUID(source.id)) {
id = source.id;
}
else {
id = uuid.v4();
}

// Sunrise/Sunset
Expand Down Expand Up @@ -453,15 +475,24 @@ const routes = application => {
revision: latest.revision,
},
});
res.redirect(referrer);

// redirect to piece-of-content
res.redirect(`/${config.content.base}/${req.content.type.id}/${latest.id}`);
}).catch(e => {
next(e);
const err = {
message: 'Something went wrong during save',
error: e.stack,
safe: `/${config.applications.base}/${req.content.type.id}`,
status: 500,
};

return next(err);
});
}

// eslint mad if no return, then mad at this else if it is there
else { // eslint-disable-line no-else-return
_.set(req.session, 'form.content.add', {
_.set(req.session, `form.content.${source.type}`, {
[req.params.type.toLowerCase()]: {
errors: validated,
content: utils.format(req.body),
Expand Down
23 changes: 14 additions & 9 deletions lib/routes/workflows.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const routes = application => {

_.unset(req.session, 'form.content.approve');

_.set(req.session, 'referrer', req.get('Referrer'));


return utils.fill(req.content.types, req.content.type, references, database).then(ct => {
return database(`content-type--${ct.id}`)
.select('*')
Expand Down Expand Up @@ -168,12 +165,11 @@ const routes = application => {
* @param {object} next - Express callback
*/
app.post(`/${config.content.base}/:type/${config.content.actions.approve}`, (req, res, next) => {
const revision = _.get(req.session, `form.content.approve[${req.params.type.toLowerCase()}].revision`, {});
const referrer = _.get(req.session, 'referrer') || `/${config.content.base}/${req.params.type.toLowerCase()}`;
const revision = _.get(req.session, `form.content.approve[${req.params.type.toLowerCase()}].revision`, '');
const id = _.get(req.session, `form.content.approve[${req.params.type.toLowerCase()}].id`, '');
const referrer = _.get(req.session, 'referrer') || req.get('Referrer');
let working;

_.unset(req.session, 'referrer');

return workflows.model().then(model => {
return types.only('workflow', {}, model, config).then(merged => {
// validate audit entry
Expand Down Expand Up @@ -202,9 +198,18 @@ const routes = application => {
return latest;
}).then(() => {
_.unset(req.session, 'form.content.approve');
res.redirect(referrer);

// redirect to piece-of-content
res.redirect(`/${config.content.base}/${req.content.type.id}/${id}`);
}).catch(e => {
next(e);
const err = {
message: 'Something went wrong during save',
error: e.stack,
safe: `/${config.applications.base}/${req.content.type.id}/${id}`,
status: 500,
};

return next(err);
});
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"debug": "^2.2.0",
"express": "^4.14.0",
"express-session": "^1.13.0",
"ibm-design-colors": "^1.6.0",
"ibm-design-colors": "^2.0.1",
"input-plugin-checkbox": "^0.3.1",
"input-plugin-email": "^0.2.0",
"input-plugin-password": "^0.1.4",
Expand All @@ -81,7 +81,7 @@
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"pg": "^6.0.1",
"punchcard-content-types": "^6.1.6",
"punchcard-content-types": "^6.1.9",
"request": "^2.75.0",
"sass-toolkit": "^2.10.0",
"serve-favicon": "^2.3.0",
Expand Down

0 comments on commit 4a4f031

Please sign in to comment.