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/views.js
2017-12-26 15:08:10 +02:00

54 lines
1.1 KiB
JavaScript

// @flow
import Router from 'koa-router';
import httpErrors from 'http-errors';
import auth from './middlewares/authentication';
import { presentView } from '../presenters';
import { View, Document } from '../models';
const router = new Router();
router.use(auth());
router.post('views.list', async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const views = await View.findAll({
where: {
documentId: id,
},
order: [['updatedAt', 'DESC']],
});
// Collectiones
let users = [];
let count = 0;
await Promise.all(
views.map(async view => {
count = view.count;
return users.push(await presentView(ctx, view));
})
);
ctx.body = {
data: {
users,
count,
},
};
});
router.post('views.create', async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
await View.increment({ documentId: document.id, userId: user.id });
});
export default router;