Present with related objects
This commit is contained in:
@@ -23,7 +23,7 @@ router.post('atlases.info', auth(), async (ctx) => {
|
|||||||
if (!atlas) throw httpErrors.NotFound();
|
if (!atlas) throw httpErrors.NotFound();
|
||||||
|
|
||||||
ctx.body = {
|
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,
|
limit: ctx.state.pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Atlases
|
||||||
|
let data = [];
|
||||||
|
await Promise.all(atlases.map(async (atlas) => {
|
||||||
|
data.push(await presentAtlas(atlas));
|
||||||
|
}))
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
pagination: ctx.state.pagination,
|
pagination: ctx.state.pagination,
|
||||||
data: atlases.map((atlas) => {
|
data: data,
|
||||||
return presentAtlas(atlas);
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -11,12 +11,6 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
name: DataTypes.STRING,
|
name: DataTypes.STRING,
|
||||||
description: DataTypes.STRING,
|
description: DataTypes.STRING,
|
||||||
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
|
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
|
||||||
}, {
|
|
||||||
instanceMethods: {
|
|
||||||
getRecentDocuments() {
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Atlas.belongsTo(Team);
|
Atlas.belongsTo(Team);
|
||||||
|
|||||||
@@ -1,30 +1,53 @@
|
|||||||
var marked = require('marked');
|
var marked = require('marked');
|
||||||
|
import Document from './models/Document';
|
||||||
|
|
||||||
export function presentUser(user) {
|
export function presentUser(user) {
|
||||||
return {
|
return new Promise(async (resolve, reject) => {
|
||||||
id: user.id,
|
resolve({
|
||||||
name: user.name,
|
id: user.id,
|
||||||
username: user.username,
|
name: user.name,
|
||||||
email: user.email,
|
username: user.username,
|
||||||
avatarUrl: user.slackData.profile.image_192,
|
email: user.email,
|
||||||
};
|
avatarUrl: user.slackData.profile.image_192,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function presentTeam(team) {
|
export function presentTeam(team) {
|
||||||
return {
|
return new Promise(async (resolve, reject) => {
|
||||||
id: team.id,
|
resolve({
|
||||||
name: team.name,
|
id: team.id,
|
||||||
};
|
name: team.name,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function presentAtlas(atlas, includeRecentDocuments=true) {
|
export function presentAtlas(atlas, includeRecentDocuments=false) {
|
||||||
return {
|
return new Promise(async (resolve, reject) => {
|
||||||
id: atlas.id,
|
const data = {
|
||||||
name: atlas.name,
|
id: atlas.id,
|
||||||
description: atlas.description,
|
name: atlas.name,
|
||||||
type: atlas.type,
|
description: atlas.description,
|
||||||
recentDocuments: atlas.getRecentDocuments(),
|
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) {
|
export async function presentDocument(document, includeAtlas=false) {
|
||||||
@@ -41,7 +64,7 @@ export async function presentDocument(document, includeAtlas=false) {
|
|||||||
|
|
||||||
if (includeAtlas) {
|
if (includeAtlas) {
|
||||||
const atlas = await document.getAtlas();
|
const atlas = await document.getAtlas();
|
||||||
data.atlas = presentAtlas(atlas, false);
|
data.atlas = await presentAtlas(atlas, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
Reference in New Issue
Block a user