Skip to content

Commit

Permalink
refactor(editor): Extract getIdEditorJSON
Browse files Browse the repository at this point in the history
There's potential here for more fixing but that would require pretty
much a wholesale refactor of everything about this file.
  • Loading branch information
naiveai committed Dec 29, 2017
1 parent 2535c2d commit df8e369
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/server/routes/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,10 @@ function getEditorTitleJSON(editorJSON, TitleUnlock) {
return editorTitleJSON;
}

router.get('/:id', (req, res, next) => {
const {AchievementUnlock, Editor, TitleUnlock} = req.app.locals.orm;
const userId = parseInt(req.params.id, 10);
function getIdEditorJSONPromise(userId, req) {
const {Editor, TitleUnlock} = req.app.locals.orm;

const editorJSONPromise = new Editor({id: userId})
return new Editor({id: userId})
.fetch({
require: true,
withRelated: ['type', 'gender', 'area']
Expand All @@ -181,8 +180,15 @@ router.get('/:id', (req, res, next) => {
.then(getEditorTitleJSON, TitleUnlock)
.catch(Editor.NotFoundError, () => {
throw new error.NotFoundError('Editor not found', req);
})
.catch(next);
});
}

router.get('/:id', (req, res, next) => {
const {AchievementUnlock, Editor, TitleUnlock} = req.app.locals.orm;
const userId = parseInt(req.params.id, 10);

const editorJSONPromise = getIdEditorJSONPromise(userId, req)
.catch(next);

const achievementJSONPromise = new AchievementUnlock()
.where(_.snakeCase('editorId'), userId)
Expand Down Expand Up @@ -293,29 +299,9 @@ router.get('/:id/achievements', (req, res, next) => {
AchievementType, AchievementUnlock, Editor, TitleUnlock
} = req.app.locals.orm;
const userId = parseInt(req.params.id, 10);
const editorJSONPromise = new Editor({id: userId})
.fetch({
require: true,
withRelated: ['type', 'gender', 'area']
})
.then((editordata) => {
let editorJSON = editordata.toJSON();

if (!req.user || userId !== req.user.id) {
editorJSON = _.omit(editorJSON, ['password', 'email']);
editorJSON.authenticated = false;
}
else {
editorJSON.authenticated = true;
}

return editorJSON;
})
.then(getEditorTitleJSON, TitleUnlock)
.catch(Editor.NotFoundError, () => {
throw new error.NotFoundError('Editor not found', req);
})
.catch(next);
const editorJSONPromise = getIdEditorJSONPromise(userId, req)
.catch(next);

const achievementJSONPromise = new AchievementUnlock()
.where('editor_id', userId)
Expand Down

0 comments on commit df8e369

Please sign in to comment.