Cleanup presenters

This commit is contained in:
Tom Moor
2017-05-27 10:54:19 -07:00
parent 51d3191ac0
commit 8883231f01
2 changed files with 49 additions and 57 deletions

View File

@ -5,15 +5,13 @@ import presentUser from './presenters/user';
export { presentUser }; export { presentUser };
export function presentTeam(ctx, team) { export async function presentTeam(ctx, team) {
ctx.cache.set(team.id, team); ctx.cache.set(team.id, team);
return new Promise(async (resolve, _reject) => { return {
resolve({ id: team.id,
id: team.id, name: team.name,
name: team.name, };
});
});
} }
export async function presentDocument(ctx, document, options) { export async function presentDocument(ctx, document, options) {
@ -77,55 +75,49 @@ export async function presentDocument(ctx, document, options) {
return data; return data;
} }
export function presentCollection( export async function presentCollection(
ctx, ctx,
collection, collection,
includeRecentDocuments = false includeRecentDocuments = false
) { ) {
ctx.cache.set(collection.id, collection); ctx.cache.set(collection.id, collection);
return new Promise(async (resolve, _reject) => { const data = {
const data = { id: collection.id,
id: collection.id, url: collection.getUrl(),
url: collection.getUrl(), name: collection.name,
name: collection.name, description: collection.description,
description: collection.description, type: collection.type,
type: collection.type, createdAt: collection.createdAt,
createdAt: collection.createdAt, updatedAt: collection.updatedAt,
updatedAt: collection.updatedAt, };
};
if (collection.type === 'atlas') if (collection.type === 'atlas')
data.navigationTree = collection.navigationTree; data.navigationTree = collection.navigationTree;
if (includeRecentDocuments) { if (includeRecentDocuments) {
const documents = await Document.findAll({ const documents = await Document.findAll({
where: { where: {
atlasId: collection.id, atlasId: collection.id,
}, },
limit: 10, limit: 10,
order: [['updatedAt', 'DESC']], order: [['updatedAt', 'DESC']],
}); });
const recentDocuments = []; const recentDocuments = [];
await Promise.all( await Promise.all(
documents.map(async document => { documents.map(async document => {
recentDocuments.push( recentDocuments.push(
await presentDocument(ctx, document, { await presentDocument(ctx, document, {
includeCollaborators: true, includeCollaborators: true,
}) })
); );
}) })
); );
data.recentDocuments = _.orderBy( data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
recentDocuments, }
['updatedAt'],
['desc']
);
}
resolve(data); return data;
});
} }
export function presentApiKey(ctx, key) { export function presentApiKey(ctx, key) {

View File

@ -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); ctx.cache.set(user.id, user);
return new Promise(async (resolve, _reject) => { return {
const data = { id: user.id,
id: user.id, username: user.username,
username: user.username, name: user.name,
name: user.name, avatarUrl: user.slackData ? user.slackData.image_192 : null,
avatarUrl: user.slackData ? user.slackData.image_192 : null, };
}; }
resolve(data);
});
};
export default presentUser; export default presentUser;