Added simple revisions API

This commit is contained in:
Jori Lallo
2017-10-16 22:36:44 -07:00
parent 2498155b79
commit 36d46533b6
4 changed files with 75 additions and 24 deletions

View File

@@ -4,10 +4,16 @@ import httpErrors from 'http-errors';
import auth from './middlewares/authentication';
import pagination from './middlewares/pagination';
import { presentDocument } from '../presenters';
import { Document, Collection, Star, View } from '../models';
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();
};
const router = new Router();
router.post('documents.list', auth(), pagination(), async ctx => {
let { sort = 'updatedAt', direction } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
@@ -101,23 +107,38 @@ router.post('documents.info', auth(), async ctx => {
ctx.assertPresent(id, 'id is required');
const document = await Document.findById(id);
if (!document) throw httpErrors.NotFound();
// Don't expose private documents outside the team
if (document.private) {
if (!ctx.state.user) throw httpErrors.NotFound();
const user = await ctx.state.user;
if (document.teamId !== user.teamId) {
throw httpErrors.NotFound();
}
}
authDocumentForUser(ctx, document);
ctx.body = {
data: await presentDocument(ctx, document),
};
});
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);
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))
);
ctx.body = {
pagination: ctx.state.pagination,
data,
};
});
router.post('documents.search', auth(), async ctx => {
const { query } = ctx.body;
ctx.assertPresent(query, 'query is required');
@@ -142,8 +163,7 @@ router.post('documents.star', auth(), async ctx => {
const user = await ctx.state.user;
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
authDocumentForUser(ctx, document);
await Star.findOrCreate({
where: { documentId: document.id, userId: user.id },
@@ -156,8 +176,7 @@ router.post('documents.unstar', auth(), async ctx => {
const user = await ctx.state.user;
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
authDocumentForUser(ctx, document);
await Star.destroy({
where: { documentId: document.id, userId: user.id },
@@ -228,7 +247,7 @@ router.post('documents.update', auth(), async ctx => {
const document = await Document.findById(id);
const collection = document.collection;
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
authDocumentForUser(ctx, document);
// Update document
if (title) document.title = title;
@@ -254,15 +273,14 @@ router.post('documents.move', auth(), async ctx => {
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
const user = ctx.state.user;
const document = await Document.findById(id);
const collection = await Collection.findById(document.atlasId);
authDocumentForUser(ctx, document);
if (collection.type !== 'atlas')
throw httpErrors.BadRequest("This document can't be moved");
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
// Set parent document
if (parentDocument) {
const parent = await Document.findById(parentDocument);
@@ -292,12 +310,10 @@ router.post('documents.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
const collection = await Collection.findById(document.atlasId);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
authDocumentForUser(ctx, document);
if (collection.type === 'atlas') {
// Don't allow deletion of root docs