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/collections.js

138 lines
3.5 KiB
JavaScript
Raw Normal View History

// @flow
2016-05-07 18:52:08 +00:00
import Router from 'koa-router';
import auth from '../middlewares/authentication';
2016-05-07 18:52:08 +00:00
import pagination from './middlewares/pagination';
2016-08-05 15:09:14 +00:00
import { presentCollection } from '../presenters';
import { Collection, Team } from '../models';
2018-02-20 07:31:18 +00:00
import { ValidationError } from '../errors';
import { exportCollection, exportCollections } from '../logistics';
import policy from '../policies';
2016-05-07 18:52:08 +00:00
const { authorize } = policy;
2016-05-07 18:52:08 +00:00
const router = new Router();
2017-04-27 04:47:03 +00:00
router.post('collections.create', auth(), async ctx => {
2017-10-30 06:22:46 +00:00
const { name, color, description, type } = ctx.body;
2016-07-22 05:06:30 +00:00
ctx.assertPresent(name, 'name is required');
2017-10-30 06:22:46 +00:00
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
2016-07-22 05:06:30 +00:00
const user = ctx.state.user;
authorize(user, 'create', Collection);
2016-07-22 05:06:30 +00:00
const collection = await Collection.create({
2016-07-22 05:06:30 +00:00
name,
description,
2017-10-30 06:22:46 +00:00
color,
2016-07-22 05:06:30 +00:00
type: type || 'atlas',
teamId: user.teamId,
creatorId: user.id,
2016-07-22 05:06:30 +00:00
});
ctx.body = {
data: await presentCollection(ctx, collection),
};
});
router.post('collections.info', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const collection = await Collection.scope('withRecentDocuments').findById(id);
authorize(ctx.state.user, 'read', collection);
ctx.body = {
data: await presentCollection(ctx, collection),
};
});
router.post('collections.export', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const collection = await Collection.findById(id);
authorize(user, 'export', collection);
// async operation to create zip archive and email user
exportCollection(id, user.email);
ctx.body = {
success: true,
};
});
router.post('collections.exportAll', auth(), async ctx => {
const user = ctx.state.user;
const team = await Team.findById(user.teamId);
authorize(user, 'export', team);
// async operation to create zip archive and email user
exportCollections(user.teamId, user.email);
ctx.body = {
success: true,
};
});
router.post('collections.update', auth(), async ctx => {
2017-10-30 06:22:46 +00:00
const { id, name, color } = ctx.body;
ctx.assertPresent(name, 'name is required');
2017-10-30 06:22:46 +00:00
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
const collection = await Collection.findById(id);
authorize(ctx.state.user, 'update', collection);
collection.name = name;
2017-10-30 06:22:46 +00:00
collection.color = color;
await collection.save();
ctx.body = {
data: await presentCollection(ctx, collection),
2016-07-22 05:06:30 +00:00
};
});
2017-04-27 04:47:03 +00:00
router.post('collections.list', auth(), pagination(), async ctx => {
2016-06-20 07:18:03 +00:00
const user = ctx.state.user;
const collections = await Collection.findAll({
2016-05-07 18:52:08 +00:00
where: {
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-07 18:52:08 +00:00
},
2017-04-27 04:47:03 +00:00
order: [['updatedAt', 'DESC']],
2016-05-07 18:52:08 +00:00
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
collections.map(
async collection => await presentCollection(ctx, collection)
)
2017-04-27 04:47:03 +00:00
);
2016-05-20 07:33:52 +00:00
2016-05-07 18:52:08 +00:00
ctx.body = {
pagination: ctx.state.pagination,
2016-07-26 06:01:14 +00:00
data,
2016-05-07 18:52:08 +00:00
};
});
router.post('collections.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const collection = await Collection.findById(id);
authorize(ctx.state.user, 'delete', collection);
const total = await Collection.count();
2018-02-20 07:31:18 +00:00
if (total === 1) throw new ValidationError('Cannot delete last collection');
2018-02-20 07:31:18 +00:00
await collection.destroy();
ctx.body = {
success: true,
};
});
2016-06-22 07:01:57 +00:00
export default router;