From 8883231f0171d28a62a476d161ca191a30cd7ec3 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 27 May 2017 10:54:19 -0700 Subject: [PATCH] Cleanup presenters --- server/presenters.js | 84 ++++++++++++++++++--------------------- server/presenters/user.js | 22 +++++----- 2 files changed, 49 insertions(+), 57 deletions(-) diff --git a/server/presenters.js b/server/presenters.js index 50a1cf10..f1ff9d11 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -5,15 +5,13 @@ import presentUser from './presenters/user'; export { presentUser }; -export function presentTeam(ctx, team) { +export async function presentTeam(ctx, team) { ctx.cache.set(team.id, team); - return new Promise(async (resolve, _reject) => { - resolve({ - id: team.id, - name: team.name, - }); - }); + return { + id: team.id, + name: team.name, + }; } export async function presentDocument(ctx, document, options) { @@ -77,55 +75,49 @@ export async function presentDocument(ctx, document, options) { return data; } -export function presentCollection( +export async function presentCollection( ctx, collection, includeRecentDocuments = false ) { ctx.cache.set(collection.id, collection); - return new Promise(async (resolve, _reject) => { - const data = { - id: collection.id, - url: collection.getUrl(), - name: collection.name, - description: collection.description, - type: collection.type, - createdAt: collection.createdAt, - updatedAt: collection.updatedAt, - }; + const data = { + id: collection.id, + url: collection.getUrl(), + name: collection.name, + description: collection.description, + type: collection.type, + createdAt: collection.createdAt, + updatedAt: collection.updatedAt, + }; - if (collection.type === 'atlas') - data.navigationTree = collection.navigationTree; + if (collection.type === 'atlas') + data.navigationTree = collection.navigationTree; - if (includeRecentDocuments) { - const documents = await Document.findAll({ - where: { - atlasId: collection.id, - }, - limit: 10, - order: [['updatedAt', 'DESC']], - }); + if (includeRecentDocuments) { + const documents = await Document.findAll({ + where: { + atlasId: collection.id, + }, + limit: 10, + order: [['updatedAt', 'DESC']], + }); - const recentDocuments = []; - await Promise.all( - documents.map(async document => { - recentDocuments.push( - await presentDocument(ctx, document, { - includeCollaborators: true, - }) - ); - }) - ); - data.recentDocuments = _.orderBy( - recentDocuments, - ['updatedAt'], - ['desc'] - ); - } + const recentDocuments = []; + await Promise.all( + documents.map(async document => { + recentDocuments.push( + await presentDocument(ctx, document, { + includeCollaborators: true, + }) + ); + }) + ); + data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']); + } - resolve(data); - }); + return data; } export function presentApiKey(ctx, key) { diff --git a/server/presenters/user.js b/server/presenters/user.js index b8d997d9..7288421f 100644 --- a/server/presenters/user.js +++ b/server/presenters/user.js @@ -1,15 +1,15 @@ -const presentUser = (ctx, user) => { +// @flow +import User from '../models/User'; + +async function presentUser(ctx: Object, user: User) { ctx.cache.set(user.id, user); - return new Promise(async (resolve, _reject) => { - const data = { - id: user.id, - username: user.username, - name: user.name, - avatarUrl: user.slackData ? user.slackData.image_192 : null, - }; - resolve(data); - }); -}; + return { + id: user.id, + username: user.username, + name: user.name, + avatarUrl: user.slackData ? user.slackData.image_192 : null, + }; +} export default presentUser;