Present with related objects

This commit is contained in:
Jori Lallo
2016-05-19 23:53:07 -07:00
parent 99a59ceb94
commit 270f556570
3 changed files with 51 additions and 30 deletions

View File

@@ -1,30 +1,53 @@
var marked = require('marked');
import Document from './models/Document';
export function presentUser(user) {
return {
id: user.id,
name: user.name,
username: user.username,
email: user.email,
avatarUrl: user.slackData.profile.image_192,
};
return new Promise(async (resolve, reject) => {
resolve({
id: user.id,
name: user.name,
username: user.username,
email: user.email,
avatarUrl: user.slackData.profile.image_192,
});
});
}
export function presentTeam(team) {
return {
id: team.id,
name: team.name,
};
return new Promise(async (resolve, reject) => {
resolve({
id: team.id,
name: team.name,
});
});
}
export function presentAtlas(atlas, includeRecentDocuments=true) {
return {
id: atlas.id,
name: atlas.name,
description: atlas.description,
type: atlas.type,
recentDocuments: atlas.getRecentDocuments(),
}
export function presentAtlas(atlas, includeRecentDocuments=false) {
return new Promise(async (resolve, reject) => {
const data = {
id: atlas.id,
name: atlas.name,
description: atlas.description,
type: atlas.type,
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: atlas.id,
},
limit: 10,
});
let recentDocuments = [];
await Promise.all(documents.map(async (document) => {
recentDocuments.push(await presentDocument(document));
}))
data.recentDocuments = recentDocuments;
}
resolve(data);
});
}
export async function presentDocument(document, includeAtlas=false) {
@@ -41,7 +64,7 @@ export async function presentDocument(document, includeAtlas=false) {
if (includeAtlas) {
const atlas = await document.getAtlas();
data.atlas = presentAtlas(atlas, false);
data.atlas = await presentAtlas(atlas, false);
}
return data;