Cleanup presenters

This commit is contained in:
Tom Moor 2017-05-27 10:54:19 -07:00
parent 51d3191ac0
commit 8883231f01
No known key found for this signature in database
GPG Key ID: 495FE29B5F21BD41
2 changed files with 49 additions and 57 deletions

View File

@ -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) {

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);
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;