2016-05-07 18:52:08 +00:00
|
|
|
import Router from 'koa-router';
|
|
|
|
import httpErrors from 'http-errors';
|
2016-08-22 06:40:57 +00:00
|
|
|
import _ from 'lodash';
|
2016-05-07 18:52:08 +00:00
|
|
|
|
2016-08-27 17:48:56 +00:00
|
|
|
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';
|
2017-05-27 18:08:52 +00:00
|
|
|
import { Collection } from '../models';
|
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 => {
|
|
|
|
const { name, description, type } = ctx.body;
|
2016-07-22 05:06:30 +00:00
|
|
|
ctx.assertPresent(name, 'name is required');
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
2017-05-27 18:08:52 +00:00
|
|
|
const atlas = await Collection.create({
|
2016-07-22 05:06:30 +00:00
|
|
|
name,
|
|
|
|
description,
|
|
|
|
type: type || 'atlas',
|
|
|
|
teamId: user.teamId,
|
2016-07-26 07:05:10 +00:00
|
|
|
creatorId: user.id,
|
2016-07-22 05:06:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.body = {
|
2017-07-07 05:02:55 +00:00
|
|
|
data: await presentCollection(ctx, atlas),
|
2016-07-22 05:06:30 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
router.post('collections.info', auth(), async ctx => {
|
2016-07-26 06:01:14 +00:00
|
|
|
const { id } = ctx.body;
|
2016-05-07 18:52:08 +00:00
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
2016-06-20 07:18:03 +00:00
|
|
|
const user = ctx.state.user;
|
2017-07-07 05:02:55 +00:00
|
|
|
const atlas = await Collection.scope('withRecentDocuments').findOne({
|
2016-05-07 18:52:08 +00:00
|
|
|
where: {
|
2016-07-26 06:01:14 +00:00
|
|
|
id,
|
2016-06-20 07:18:03 +00:00
|
|
|
teamId: user.teamId,
|
2016-05-07 18:52:08 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!atlas) throw httpErrors.NotFound();
|
|
|
|
|
|
|
|
ctx.body = {
|
2017-07-07 05:02:55 +00:00
|
|
|
data: await presentCollection(ctx, atlas),
|
2016-05-07 18:52:08 +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;
|
2017-07-07 05:02:55 +00:00
|
|
|
const collections = await Collection.scope('withRecentDocuments').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,
|
|
|
|
});
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
|
|
|
collections.map(async atlas => await presentCollection(ctx, atlas))
|
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
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-06-22 07:01:57 +00:00
|
|
|
export default router;
|