This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/documents.js

166 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-05-20 03:46:34 +00:00
import Router from 'koa-router';
import httpErrors from 'http-errors';
import auth from './authentication';
import pagination from './middlewares/pagination';
import { presentDocument } from '../presenters';
import { Document, Atlas } from '../models';
const router = new Router();
2016-05-30 19:37:07 +00:00
// FIXME: This really needs specs :/
router.post('documents.info', auth({ require: false }), async (ctx) => {
2016-05-20 03:46:34 +00:00
let { id } = ctx.request.body;
ctx.assertPresent(id, 'id is required');
const document = await Document.findOne({
where: {
id: id,
},
});
// Don't expose private documents outside the team
if (document.private) {
if (!ctx.state.user) throw httpErrors.NotFound();
2016-05-20 03:46:34 +00:00
2016-06-20 07:18:03 +00:00
const user = await ctx.state.user;
if (document.teamId !== user.teamId) {
2016-05-31 05:56:49 +00:00
throw httpErrors.NotFound();
}
ctx.body = {
data: await presentDocument(document, true),
};
} else {
ctx.body = {
data: await presentDocument(document),
};
}
if (!document) throw httpErrors.NotFound();
2016-05-20 03:46:34 +00:00
});
router.post('documents.create', auth(), async (ctx) => {
let {
atlas,
title,
text,
2016-06-26 05:27:44 +00:00
parentDocument,
2016-05-20 03:46:34 +00:00
} = ctx.request.body;
ctx.assertPresent(atlas, 'atlas is required');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
const user = ctx.state.user;
2016-05-20 03:46:34 +00:00
const ownerAtlas = await Atlas.findOne({
where: {
id: atlas,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-20 03:46:34 +00:00
},
});
if (!ownerAtlas) throw httpErrors.BadRequest();
2016-06-26 18:23:03 +00:00
let parentDocumentObj = {};
2016-06-26 05:27:44 +00:00
if (parentDocument && ownerAtlas.type === 'atlas') {
parentDocumentObj = await Document.findOne({
where: {
id: parentDocument,
atlasId: ownerAtlas.id,
},
});
}
2016-05-20 03:46:34 +00:00
const document = await Document.create({
2016-06-26 05:27:44 +00:00
parentDocumentId: parentDocumentObj.id,
2016-05-20 03:46:34 +00:00
atlasId: ownerAtlas.id,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
userId: user.id,
2016-06-26 18:23:03 +00:00
lastModifiedById: user.id,
2016-05-20 03:46:34 +00:00
title: title,
text: text,
});
2016-06-26 18:23:03 +00:00
await document.createRevision();
2016-05-20 03:46:34 +00:00
2016-06-26 05:27:44 +00:00
// TODO: Move to afterSave hook if possible with imports
2016-06-26 18:23:03 +00:00
if (parentDocument && ownerAtlas.type === 'atlas') {
ownerAtlas.addNodeToNavigationTree(document);
await ownerAtlas.save();
}
2016-06-26 05:27:44 +00:00
2016-05-20 03:46:34 +00:00
ctx.body = {
data: await presentDocument(document, true),
};
});
2016-05-26 04:26:06 +00:00
router.post('documents.update', auth(), async (ctx) => {
let {
id,
title,
text,
} = ctx.request.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
const user = ctx.state.user;
let document = await Document.findOne({
where: {
id: id,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-26 04:26:06 +00:00
},
});
if (!document) throw httpErrors.BadRequest();
2016-07-01 06:47:49 +00:00
// Update document
2016-05-26 04:26:06 +00:00
document.title = title;
document.text = text;
2016-06-26 18:23:03 +00:00
document.lastModifiedById = user.id;
2016-05-26 04:26:06 +00:00
await document.save();
2016-06-26 18:23:03 +00:00
await document.createRevision();
2016-05-26 04:26:06 +00:00
2016-07-01 06:47:49 +00:00
// Update
const atlas = await Atlas.findById(document.atlasId);
if (atlas.type === 'atlas') {
await atlas.updateNavigationTree();
}
2016-05-26 04:26:06 +00:00
ctx.body = {
data: await presentDocument(document, true),
};
});
2016-05-30 18:15:35 +00:00
router.post('documents.delete', auth(), async (ctx) => {
let {
id,
} = ctx.request.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
let document = await Document.findOne({
where: {
id: id,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-30 18:15:35 +00:00
},
});
if (!document) throw httpErrors.BadRequest();
2016-06-23 07:19:45 +00:00
// TODO: Don't allow to destroy root docs
// TODO: handle sub documents
2016-05-30 18:15:35 +00:00
try {
await document.destroy();
} catch (e) {
throw httpErrors.BadRequest('Error while deleting');
};
ctx.body = {
ok: true,
};
});
2016-07-01 06:47:49 +00:00
export default router;