diff --git a/server/api/atlases.js b/server/api/atlases.js index 747f074c9..4c1005291 100644 --- a/server/api/atlases.js +++ b/server/api/atlases.js @@ -23,7 +23,7 @@ router.post('atlases.info', auth(), async (ctx) => { if (!atlas) throw httpErrors.NotFound(); ctx.body = { - data: presentAtlas(atlas), + data: await presentAtlas(atlas, true), }; }); @@ -41,11 +41,15 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => { limit: ctx.state.pagination.limit, }); + // Atlases + let data = []; + await Promise.all(atlases.map(async (atlas) => { + data.push(await presentAtlas(atlas)); + })) + ctx.body = { pagination: ctx.state.pagination, - data: atlases.map((atlas) => { - return presentAtlas(atlas); - }) + data: data, }; }); diff --git a/server/models/Atlas.js b/server/models/Atlas.js index a96ba6ca9..175bd1ba1 100644 --- a/server/models/Atlas.js +++ b/server/models/Atlas.js @@ -11,12 +11,6 @@ const Atlas = sequelize.define('atlas', { name: DataTypes.STRING, description: DataTypes.STRING, type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }}, -}, { - instanceMethods: { - getRecentDocuments() { - return []; - }, - }, }); Atlas.belongsTo(Team); diff --git a/server/presenters.js b/server/presenters.js index e77a0312f..afdf6207f 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -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;