2016-09-09 08:35:39 +00:00
|
|
|
import _ from 'lodash';
|
2016-09-11 20:38:52 +00:00
|
|
|
import { Document, Atlas, User } from './models';
|
2016-08-15 10:51:26 +00:00
|
|
|
|
2016-09-11 20:38:52 +00:00
|
|
|
import presentUser from './presenters/user';
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2016-09-11 20:38:52 +00:00
|
|
|
export {
|
|
|
|
presentUser,
|
|
|
|
};
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2016-08-15 10:51:26 +00:00
|
|
|
export function presentTeam(ctx, team) {
|
|
|
|
ctx.cache.set(team.id, team);
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
return new Promise(async (resolve, _reject) => {
|
2016-05-20 06:53:07 +00:00
|
|
|
resolve({
|
|
|
|
id: team.id,
|
|
|
|
name: team.name,
|
|
|
|
});
|
|
|
|
});
|
2016-04-29 05:25:37 +00:00
|
|
|
}
|
2016-05-07 18:52:08 +00:00
|
|
|
|
2016-08-15 12:52:07 +00:00
|
|
|
export async function presentDocument(ctx, document, options) {
|
|
|
|
options = {
|
2016-08-15 12:59:53 +00:00
|
|
|
includeCollection: true,
|
2016-08-15 12:52:07 +00:00
|
|
|
includeCollaborators: true,
|
|
|
|
...options,
|
|
|
|
};
|
2016-08-15 10:51:26 +00:00
|
|
|
ctx.cache.set(document.id, document);
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
const data = {
|
|
|
|
id: document.id,
|
2016-08-15 19:41:51 +00:00
|
|
|
url: document.getUrl(),
|
2016-08-05 15:09:14 +00:00
|
|
|
private: document.private,
|
|
|
|
title: document.title,
|
|
|
|
text: document.text,
|
|
|
|
html: document.html,
|
|
|
|
preview: document.preview,
|
|
|
|
createdAt: document.createdAt,
|
2016-08-15 10:51:26 +00:00
|
|
|
createdBy: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
updatedAt: document.updatedAt,
|
2016-08-15 10:51:26 +00:00
|
|
|
updatedBy: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
team: document.teamId,
|
2016-08-15 10:51:26 +00:00
|
|
|
collaborators: [],
|
2016-08-05 15:09:14 +00:00
|
|
|
};
|
|
|
|
|
2016-08-15 12:52:07 +00:00
|
|
|
if (options.includeCollection) {
|
2016-08-15 10:51:26 +00:00
|
|
|
data.collection = await ctx.cache.get(
|
2016-08-15 12:52:07 +00:00
|
|
|
document.atlasId,
|
|
|
|
async () => {
|
|
|
|
const collection = await Atlas.findOne({ where: {
|
|
|
|
id: document.atlasId,
|
|
|
|
} });
|
|
|
|
return await presentCollection(ctx, collection);
|
|
|
|
}
|
2016-08-15 10:51:26 +00:00
|
|
|
);
|
2016-08-05 15:09:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:52:07 +00:00
|
|
|
if (options.includeCollaborators) {
|
|
|
|
// This could be further optimized by using ctx.cache
|
|
|
|
data.collaborators = await User.findAll({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: document.collaboratorIds || [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.map(user => presentUser(ctx, user));
|
|
|
|
}
|
|
|
|
|
2016-08-15 10:51:26 +00:00
|
|
|
const createdBy = await ctx.cache.get(
|
|
|
|
document.createdById,
|
|
|
|
async () => await User.findById(document.createdById)
|
|
|
|
);
|
|
|
|
data.createdBy = await presentUser(ctx, createdBy);
|
|
|
|
|
|
|
|
const updatedBy = await ctx.cache.get(
|
2016-08-15 12:52:07 +00:00
|
|
|
document.lastModifiedById,
|
|
|
|
async () => await User.findById(document.lastModifiedById)
|
2016-08-15 10:51:26 +00:00
|
|
|
);
|
2016-08-15 12:52:07 +00:00
|
|
|
data.updatedBy = await presentUser(ctx, updatedBy);
|
2016-08-05 15:09:14 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-08-15 10:51:26 +00:00
|
|
|
export function presentCollection(ctx, collection, includeRecentDocuments=false) {
|
|
|
|
ctx.cache.set(collection.id, collection);
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
return new Promise(async (resolve, _reject) => {
|
2016-05-20 06:53:07 +00:00
|
|
|
const data = {
|
2016-08-05 15:09:14 +00:00
|
|
|
id: collection.id,
|
2016-08-15 19:41:51 +00:00
|
|
|
url: collection.getUrl(),
|
2016-08-05 15:09:14 +00:00
|
|
|
name: collection.name,
|
|
|
|
description: collection.description,
|
|
|
|
type: collection.type,
|
2016-08-22 06:40:57 +00:00
|
|
|
createdAt: collection.createdAt,
|
|
|
|
updatedAt: collection.updatedAt,
|
2016-08-05 15:09:14 +00:00
|
|
|
};
|
2016-05-20 06:53:07 +00:00
|
|
|
|
2016-08-14 08:50:42 +00:00
|
|
|
if (collection.type === 'atlas') data.navigationTree = collection.navigationTree;
|
2016-06-21 06:12:56 +00:00
|
|
|
|
2016-05-20 06:53:07 +00:00
|
|
|
if (includeRecentDocuments) {
|
|
|
|
const documents = await Document.findAll({
|
|
|
|
where: {
|
2016-08-05 15:09:14 +00:00
|
|
|
atlasId: collection.id,
|
2016-05-20 06:53:07 +00:00
|
|
|
},
|
|
|
|
limit: 10,
|
2016-05-20 07:33:52 +00:00
|
|
|
order: [
|
|
|
|
['updatedAt', 'DESC'],
|
|
|
|
],
|
2016-05-20 06:53:07 +00:00
|
|
|
});
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
const recentDocuments = [];
|
2016-05-20 06:53:07 +00:00
|
|
|
await Promise.all(documents.map(async (document) => {
|
2016-08-15 12:52:07 +00:00
|
|
|
recentDocuments.push(await presentDocument(ctx, document, {
|
|
|
|
includeCollaborators: true,
|
|
|
|
}));
|
2016-08-05 15:09:14 +00:00
|
|
|
}));
|
2016-09-09 08:35:39 +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
|
|
|
}
|
2016-08-24 07:37:54 +00:00
|
|
|
|
|
|
|
export function presentApiKey(ctx, key) {
|
|
|
|
return {
|
|
|
|
id: key.id,
|
|
|
|
name: key.name,
|
|
|
|
secret: key.secret,
|
|
|
|
};
|
|
|
|
}
|