2017-06-04 21:40:27 +00:00
|
|
|
// @flow
|
2016-05-20 03:46:34 +00:00
|
|
|
import Router from 'koa-router';
|
|
|
|
import httpErrors from 'http-errors';
|
2016-08-15 19:41:51 +00:00
|
|
|
|
2016-08-27 17:48:56 +00:00
|
|
|
import auth from './middlewares/authentication';
|
2017-06-26 00:21:33 +00:00
|
|
|
import pagination from './middlewares/pagination';
|
2017-10-17 05:36:44 +00:00
|
|
|
import { presentDocument, presentRevision } from '../presenters';
|
|
|
|
import { Document, Collection, Star, View, Revision } from '../models';
|
|
|
|
|
|
|
|
const authDocumentForUser = (ctx, document) => {
|
|
|
|
const user = ctx.state.user;
|
|
|
|
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
|
|
|
|
const router = new Router();
|
2017-10-17 05:36:44 +00:00
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
router.post('documents.list', auth(), pagination(), async ctx => {
|
2017-11-23 02:40:37 +00:00
|
|
|
let { sort = 'updatedAt', direction, collection } = ctx.body;
|
2017-06-26 00:21:33 +00:00
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2017-11-20 05:32:18 +00:00
|
|
|
let where = { teamId: user.teamId };
|
2017-11-23 02:40:37 +00:00
|
|
|
if (collection) where = { ...where, atlasId: collection };
|
2017-11-20 05:32:18 +00:00
|
|
|
|
2017-07-15 23:08:12 +00:00
|
|
|
const userId = user.id;
|
|
|
|
const starredScope = { method: ['withStarred', userId] };
|
|
|
|
const documents = await Document.scope('defaultScope', starredScope).findAll({
|
2017-11-20 05:32:18 +00:00
|
|
|
where,
|
2017-06-26 00:21:33 +00:00
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
|
|
|
documents.map(document => presentDocument(ctx, document))
|
|
|
|
);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('documents.viewed', auth(), pagination(), async ctx => {
|
|
|
|
let { sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const views = await View.findAll({
|
|
|
|
where: { userId: user.id },
|
|
|
|
order: [[sort, direction]],
|
2017-08-03 03:45:09 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Document,
|
2017-09-14 06:04:58 +00:00
|
|
|
required: true,
|
2017-08-03 03:45:09 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Star,
|
|
|
|
as: 'starred',
|
|
|
|
where: { userId: user.id },
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2017-06-26 00:21:33 +00:00
|
|
|
views.map(view => presentDocument(ctx, view.document))
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('documents.starred', auth(), pagination(), async ctx => {
|
|
|
|
let { sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const views = await Star.findAll({
|
|
|
|
where: { userId: user.id },
|
|
|
|
order: [[sort, direction]],
|
2017-07-04 04:35:17 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Document,
|
|
|
|
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
|
|
|
},
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2017-06-26 00:21:33 +00:00
|
|
|
views.map(view => presentDocument(ctx, view.document))
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
});
|
2016-08-15 19:41:51 +00:00
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('documents.info', auth(), async ctx => {
|
2016-08-12 13:36:48 +00:00
|
|
|
const { id } = ctx.body;
|
2016-05-20 03:46:34 +00:00
|
|
|
ctx.assertPresent(id, 'id is required');
|
2017-06-26 00:21:33 +00:00
|
|
|
const document = await Document.findById(id);
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
data: await presentDocument(ctx, document),
|
|
|
|
};
|
|
|
|
});
|
2016-08-03 12:36:50 +00:00
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
router.post('documents.revisions', auth(), pagination(), async ctx => {
|
|
|
|
let { id, sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
const document = await Document.findById(id);
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
|
|
|
|
|
|
|
const revisions = await Revision.findAll({
|
|
|
|
where: { documentId: id },
|
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
|
|
|
revisions.map(revision => presentRevision(ctx, revision))
|
|
|
|
);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
2017-10-17 05:36:44 +00:00
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2017-06-26 00:21:33 +00:00
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
});
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('documents.search', auth(), async ctx => {
|
2016-08-01 07:12:55 +00:00
|
|
|
const { query } = ctx.body;
|
2016-07-13 06:43:41 +00:00
|
|
|
ctx.assertPresent(query, 'query is required');
|
|
|
|
|
|
|
|
const user = await ctx.state.user;
|
|
|
|
|
2016-08-23 06:37:01 +00:00
|
|
|
const documents = await Document.searchForUser(user, query);
|
2016-07-13 06:43:41 +00:00
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
|
|
|
documents.map(async document => await presentDocument(ctx, document))
|
2017-04-27 04:47:03 +00:00
|
|
|
);
|
2016-07-13 06:43:41 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
2016-08-01 07:12:55 +00:00
|
|
|
data,
|
2016-07-13 06:43:41 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
router.post('documents.star', auth(), async ctx => {
|
|
|
|
const { id } = ctx.body;
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
const user = await ctx.state.user;
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
await Star.findOrCreate({
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('documents.unstar', auth(), async ctx => {
|
|
|
|
const { id } = ctx.body;
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
const user = await ctx.state.user;
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
await Star.destroy({
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('documents.create', auth(), async ctx => {
|
2017-06-04 21:40:27 +00:00
|
|
|
const { collection, title, text, parentDocument, index } = ctx.body;
|
2016-08-05 15:09:14 +00:00
|
|
|
ctx.assertPresent(collection, 'collection is required');
|
2017-06-04 21:40:27 +00:00
|
|
|
ctx.assertUuid(collection, 'collection must be an uuid');
|
2016-05-20 03:46:34 +00:00
|
|
|
ctx.assertPresent(title, 'title is required');
|
|
|
|
ctx.assertPresent(text, 'text is required');
|
2017-06-04 21:40:27 +00:00
|
|
|
if (parentDocument)
|
|
|
|
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
|
|
|
|
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2016-05-22 21:31:21 +00:00
|
|
|
const user = ctx.state.user;
|
2017-05-27 18:08:52 +00:00
|
|
|
const ownerCollection = await Collection.findOne({
|
2016-05-20 03:46:34 +00:00
|
|
|
where: {
|
2016-08-05 15:09:14 +00:00
|
|
|
id: collection,
|
2016-06-20 07:18:03 +00:00
|
|
|
teamId: user.teamId,
|
2016-05-20 03:46:34 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-08-12 13:36:48 +00:00
|
|
|
if (!ownerCollection) throw httpErrors.BadRequest();
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2017-06-04 21:40:27 +00:00
|
|
|
let parentDocumentObj = {};
|
|
|
|
if (parentDocument && ownerCollection.type === 'atlas') {
|
|
|
|
parentDocumentObj = await Document.findOne({
|
|
|
|
where: {
|
|
|
|
id: parentDocument,
|
|
|
|
atlasId: ownerCollection.id,
|
|
|
|
},
|
2016-08-21 18:12:24 +00:00
|
|
|
});
|
2017-06-04 21:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const newDocument = await Document.create({
|
|
|
|
parentDocumentId: parentDocumentObj.id,
|
|
|
|
atlasId: ownerCollection.id,
|
|
|
|
teamId: user.teamId,
|
|
|
|
userId: user.id,
|
|
|
|
lastModifiedById: user.id,
|
|
|
|
createdById: user.id,
|
|
|
|
title,
|
|
|
|
text,
|
|
|
|
});
|
|
|
|
|
2017-07-15 05:15:56 +00:00
|
|
|
// reload to get all of the data needed to present (user, collection etc)
|
|
|
|
const document = await Document.findById(newDocument.id);
|
|
|
|
|
2017-06-05 07:36:50 +00:00
|
|
|
if (ownerCollection.type === 'atlas') {
|
2017-07-15 05:15:56 +00:00
|
|
|
await ownerCollection.addDocumentToStructure(document, index);
|
2017-06-04 21:40:27 +00:00
|
|
|
}
|
2016-08-21 22:45:48 +00:00
|
|
|
|
2017-09-12 07:10:02 +00:00
|
|
|
document.collection = ownerCollection;
|
|
|
|
|
2016-08-21 22:45:48 +00:00
|
|
|
ctx.body = {
|
2017-07-15 05:15:56 +00:00
|
|
|
data: await presentDocument(ctx, document),
|
2016-08-21 22:45:48 +00:00
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
});
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('documents.update', auth(), async ctx => {
|
2017-11-25 21:04:54 +00:00
|
|
|
const { id, title, text, lastRevision } = ctx.body;
|
2016-05-26 04:26:06 +00:00
|
|
|
ctx.assertPresent(id, 'id is required');
|
2017-06-05 05:12:36 +00:00
|
|
|
ctx.assertPresent(title || text, 'title or text is required');
|
2016-05-26 04:26:06 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2017-07-10 03:32:38 +00:00
|
|
|
const document = await Document.findById(id);
|
2017-07-10 03:27:06 +00:00
|
|
|
const collection = document.collection;
|
2016-05-26 04:26:06 +00:00
|
|
|
|
2017-11-25 21:04:54 +00:00
|
|
|
if (lastRevision && lastRevision !== document.revisionCount) {
|
|
|
|
throw httpErrors.BadRequest('Document has changed since last revision');
|
|
|
|
}
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
2016-05-26 04:26:06 +00:00
|
|
|
|
2016-07-01 06:47:49 +00:00
|
|
|
// Update document
|
2017-06-05 05:12:36 +00:00
|
|
|
if (title) document.title = title;
|
|
|
|
if (text) document.text = text;
|
2016-06-26 18:23:03 +00:00
|
|
|
document.lastModifiedById = user.id;
|
2016-05-26 04:26:06 +00:00
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
await document.save();
|
|
|
|
if (collection.type === 'atlas') {
|
|
|
|
await collection.updateDocument(document);
|
|
|
|
}
|
2017-07-10 03:27:06 +00:00
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
document.collection = collection;
|
2016-07-01 06:47:49 +00:00
|
|
|
|
2017-06-06 06:50:32 +00:00
|
|
|
ctx.body = {
|
2017-10-09 07:05:15 +00:00
|
|
|
data: await presentDocument(ctx, document),
|
2017-06-06 06:50:32 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('documents.move', auth(), async ctx => {
|
|
|
|
const { id, parentDocument, index } = ctx.body;
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
if (parentDocument)
|
|
|
|
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
|
|
|
|
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
const document = await Document.findById(id);
|
2017-10-09 07:05:15 +00:00
|
|
|
const collection = await Collection.findById(document.atlasId);
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
if (collection.type !== 'atlas')
|
|
|
|
throw httpErrors.BadRequest("This document can't be moved");
|
2017-06-06 06:50:32 +00:00
|
|
|
|
|
|
|
// Set parent document
|
|
|
|
if (parentDocument) {
|
2017-06-26 00:21:33 +00:00
|
|
|
const parent = await Document.findById(parentDocument);
|
2017-10-02 06:42:17 +00:00
|
|
|
if (!parent || parent.atlasId !== document.atlasId)
|
2017-06-06 06:50:32 +00:00
|
|
|
throw httpErrors.BadRequest(
|
|
|
|
'Invalid parentDocument (must be same collection)'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentDocument === id)
|
|
|
|
throw httpErrors.BadRequest('Infinite loop detected and prevented!');
|
|
|
|
|
|
|
|
// If no parent document is provided, set it as null (move to root level)
|
|
|
|
document.parentDocumentId = parentDocument;
|
|
|
|
await document.save();
|
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
await collection.moveDocument(document, index);
|
2017-09-04 21:48:56 +00:00
|
|
|
// Update collection
|
|
|
|
document.collection = collection;
|
2017-06-06 06:50:32 +00:00
|
|
|
|
2016-05-26 04:26:06 +00:00
|
|
|
ctx.body = {
|
2017-07-07 05:02:55 +00:00
|
|
|
data: await presentDocument(ctx, document),
|
2016-05-26 04:26:06 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('documents.delete', auth(), async ctx => {
|
|
|
|
const { id } = ctx.body;
|
2016-05-30 18:15:35 +00:00
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
const document = await Document.findById(id);
|
2017-05-27 18:08:52 +00:00
|
|
|
const collection = await Collection.findById(document.atlasId);
|
2016-05-30 18:15:35 +00:00
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
authDocumentForUser(ctx, document);
|
2016-05-30 18:15:35 +00:00
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
if (collection.type === 'atlas') {
|
2016-07-11 00:02:45 +00:00
|
|
|
// Don't allow deletion of root docs
|
2017-06-05 07:36:50 +00:00
|
|
|
if (collection.documentStructure.length === 1) {
|
|
|
|
throw httpErrors.BadRequest(
|
|
|
|
"Unable to delete collection's only document"
|
|
|
|
);
|
2016-07-11 00:02:45 +00:00
|
|
|
}
|
2016-06-23 07:19:45 +00:00
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
// Delete document and all of its children
|
2016-07-11 00:02:45 +00:00
|
|
|
try {
|
2017-10-09 07:05:15 +00:00
|
|
|
await collection.removeDocument(document);
|
2016-07-11 00:02:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw httpErrors.BadRequest('Error while deleting');
|
2016-08-01 07:11:28 +00:00
|
|
|
}
|
2016-08-14 08:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the actual document
|
|
|
|
try {
|
|
|
|
await document.destroy();
|
|
|
|
} catch (e) {
|
|
|
|
throw httpErrors.BadRequest('Error while deleting document');
|
2016-07-07 04:36:50 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 18:15:35 +00:00
|
|
|
ctx.body = {
|
2016-08-27 05:04:28 +00:00
|
|
|
success: true,
|
2016-05-30 18:15:35 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-07-01 06:47:49 +00:00
|
|
|
export default router;
|