Added collaborators to document API

This commit is contained in:
Jori Lallo
2016-08-15 14:52:07 +02:00
parent f93d979a0f
commit 092c61f1de
2 changed files with 51 additions and 16 deletions

View File

@ -34,11 +34,16 @@ router.post('documents.info', auth({ require: false }), async (ctx) => {
} }
ctx.body = { ctx.body = {
data: await presentDocument(ctx, document, true), data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}),
}; };
} else { } else {
ctx.body = { ctx.body = {
data: await presentDocument(ctx, document), data: await presentDocument(ctx, document, {
includeCollaborators: true,
}),
}; };
} }
}); });
@ -71,7 +76,10 @@ router.post('documents.search', auth(), async (ctx) => {
const data = []; const data = [];
await Promise.all(documents.map(async (document) => { await Promise.all(documents.map(async (document) => {
data.push(await presentDocument(document)); data.push(await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}));
})); }));
ctx.body = { ctx.body = {
@ -130,7 +138,10 @@ router.post('documents.create', auth(), async (ctx) => {
} }
ctx.body = { ctx.body = {
data: await presentDocument(document, true), data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}),
}; };
}); });
@ -167,7 +178,10 @@ router.post('documents.update', auth(), async (ctx) => {
} }
ctx.body = { ctx.body = {
data: await presentDocument(ctx, document, true), data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}),
}; };
}); });

View File

@ -27,7 +27,12 @@ export function presentTeam(ctx, team) {
}); });
} }
export async function presentDocument(ctx, document, includeCollection = false) { export async function presentDocument(ctx, document, options) {
options = {
includeCollection: false,
includeCollaborators: true,
...options,
};
ctx.cache.set(document.id, document); ctx.cache.set(document.id, document);
const data = { const data = {
@ -46,16 +51,30 @@ export async function presentDocument(ctx, document, includeCollection = false)
collaborators: [], collaborators: [],
}; };
if (includeCollection) { if (options.includeCollection) {
const collection = await Atlas.findOne({ where: {
id: document.atlasId,
} });
data.collection = await ctx.cache.get( data.collection = await ctx.cache.get(
collection.id, document.atlasId,
async () => await presentCollection(ctx, collection, false) async () => {
const collection = await Atlas.findOne({ where: {
id: document.atlasId,
} });
return await presentCollection(ctx, collection);
}
); );
} }
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));
}
const createdBy = await ctx.cache.get( const createdBy = await ctx.cache.get(
document.createdById, document.createdById,
async () => await User.findById(document.createdById) async () => await User.findById(document.createdById)
@ -63,10 +82,10 @@ export async function presentDocument(ctx, document, includeCollection = false)
data.createdBy = await presentUser(ctx, createdBy); data.createdBy = await presentUser(ctx, createdBy);
const updatedBy = await ctx.cache.get( const updatedBy = await ctx.cache.get(
document.createdById, document.lastModifiedById,
async () => await User.findById(document.updatedById) async () => await User.findById(document.lastModifiedById)
); );
data.createdBy = await presentUser(ctx, updatedBy); data.updatedBy = await presentUser(ctx, updatedBy);
return data; return data;
} }
@ -97,7 +116,9 @@ export function presentCollection(ctx, collection, includeRecentDocuments=false)
const recentDocuments = []; const recentDocuments = [];
await Promise.all(documents.map(async (document) => { await Promise.all(documents.map(async (document) => {
recentDocuments.push(await presentDocument(ctx, document, true)); recentDocuments.push(await presentDocument(ctx, document, {
includeCollaborators: true,
}));
})); }));
data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']); data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']);
} }