67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import Router from "koa-router";
|
|
import { NotFoundError } from "@server/errors";
|
|
import auth from "@server/middlewares/authentication";
|
|
import { Document, Revision } from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
import { presentRevision } from "@server/presenters";
|
|
import { assertPresent, assertSort } from "@server/validation";
|
|
import pagination from "./middlewares/pagination";
|
|
|
|
const router = new Router();
|
|
|
|
router.post("revisions.info", auth(), async (ctx) => {
|
|
const { id } = ctx.body;
|
|
assertPresent(id, "id is required");
|
|
const { user } = ctx.state;
|
|
const revision = await Revision.findByPk(id);
|
|
|
|
if (!revision) {
|
|
throw NotFoundError();
|
|
}
|
|
|
|
const document = await Document.findByPk(revision.documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "read", document);
|
|
|
|
ctx.body = {
|
|
pagination: ctx.state.pagination,
|
|
data: await presentRevision(revision),
|
|
};
|
|
});
|
|
|
|
router.post("revisions.list", auth(), pagination(), async (ctx) => {
|
|
let { direction } = ctx.body;
|
|
const { documentId, sort = "updatedAt" } = ctx.body;
|
|
if (direction !== "ASC") {
|
|
direction = "DESC";
|
|
}
|
|
assertSort(sort, Revision);
|
|
assertPresent(documentId, "documentId is required");
|
|
|
|
const { user } = ctx.state;
|
|
const document = await Document.findByPk(documentId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "read", document);
|
|
|
|
const revisions = await Revision.findAll({
|
|
where: {
|
|
documentId: document.id,
|
|
},
|
|
order: [[sort, direction]],
|
|
offset: ctx.state.pagination.offset,
|
|
limit: ctx.state.pagination.limit,
|
|
});
|
|
const data = await Promise.all(
|
|
revisions.map((revision) => presentRevision(revision))
|
|
);
|
|
|
|
ctx.body = {
|
|
pagination: ctx.state.pagination,
|
|
data,
|
|
};
|
|
});
|
|
|
|
export default router;
|