2016-05-26 06:17:57 +00:00
|
|
|
import _orderBy from 'lodash.orderby';
|
2016-05-20 06:53:07 +00:00
|
|
|
import Document from './models/Document';
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2016-04-29 05:25:37 +00:00
|
|
|
export function presentUser(user) {
|
2016-05-20 06:53:07 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
resolve({
|
|
|
|
id: user.id,
|
|
|
|
name: user.name,
|
|
|
|
username: user.username,
|
|
|
|
email: user.email,
|
2016-05-27 06:36:49 +00:00
|
|
|
avatarUrl: user.slackData.image_192,
|
2016-05-20 06:53:07 +00:00
|
|
|
});
|
|
|
|
});
|
2016-04-29 05:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function presentTeam(team) {
|
2016-05-20 06:53:07 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
resolve({
|
|
|
|
id: team.id,
|
|
|
|
name: team.name,
|
|
|
|
});
|
|
|
|
});
|
2016-04-29 05:25:37 +00:00
|
|
|
}
|
2016-05-07 18:52:08 +00:00
|
|
|
|
2016-05-20 06:53:07 +00:00
|
|
|
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,
|
2016-05-20 07:33:52 +00:00
|
|
|
order: [
|
|
|
|
['updatedAt', 'DESC'],
|
|
|
|
],
|
2016-05-20 06:53:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let recentDocuments = [];
|
|
|
|
await Promise.all(documents.map(async (document) => {
|
2016-05-22 21:31:21 +00:00
|
|
|
recentDocuments.push(await presentDocument(document, true));
|
2016-05-20 06:53:07 +00:00
|
|
|
}))
|
2016-05-26 04:32:59 +00:00
|
|
|
data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
2016-05-20 06:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
});
|
2016-05-20 03:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function presentDocument(document, includeAtlas=false) {
|
|
|
|
const data = {
|
|
|
|
id: document.id,
|
2016-05-26 05:38:45 +00:00
|
|
|
url: document.buildUrl(),
|
2016-05-26 05:47:31 +00:00
|
|
|
private: document.private,
|
2016-05-20 03:46:34 +00:00
|
|
|
title: document.title,
|
|
|
|
text: document.text,
|
2016-05-23 05:08:09 +00:00
|
|
|
html: document.html,
|
|
|
|
preview: document.preview,
|
2016-05-20 03:46:34 +00:00
|
|
|
createdAt: document.createdAt,
|
|
|
|
updatedAt: document.updatedAt,
|
|
|
|
atlas: document.atlaId,
|
|
|
|
team: document.teamId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeAtlas) {
|
|
|
|
const atlas = await document.getAtlas();
|
2016-05-20 06:53:07 +00:00
|
|
|
data.atlas = await presentAtlas(atlas, false);
|
2016-05-22 14:25:13 +00:00
|
|
|
|
|
|
|
const user = await document.getUser();
|
|
|
|
data.user = await presentUser(user, false);
|
2016-05-20 03:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|