@@ -1,23 +1,26 @@
|
||||
import invariant from "invariant";
|
||||
import Router from "koa-router";
|
||||
import { WhereOptions } from "sequelize/types";
|
||||
import fileOperationDeleter from "@server/commands/fileOperationDeleter";
|
||||
import { NotFoundError, ValidationError } from "@server/errors";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { FileOperation, Team } from "@server/models";
|
||||
import policy from "@server/policies";
|
||||
import { authorize } from "@server/policies";
|
||||
import { presentFileOperation } from "@server/presenters";
|
||||
import { getSignedUrl } from "@server/utils/s3";
|
||||
import { assertPresent, assertIn, assertUuid } from "@server/validation";
|
||||
import pagination from "./middlewares/pagination";
|
||||
|
||||
const { authorize } = policy;
|
||||
const router = new Router();
|
||||
|
||||
router.post("fileOperations.info", auth(), async (ctx) => {
|
||||
const { id } = ctx.body;
|
||||
assertUuid(id, "id is required");
|
||||
const user = ctx.state.user;
|
||||
const { user } = ctx.state;
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const fileOperation = await FileOperation.findByPk(id);
|
||||
invariant(fileOperation, "File operation not found");
|
||||
|
||||
authorize(user, fileOperation.type, team);
|
||||
|
||||
if (!fileOperation) {
|
||||
@@ -40,13 +43,14 @@ router.post("fileOperations.list", auth(), pagination(), async (ctx) => {
|
||||
);
|
||||
|
||||
if (direction !== "ASC") direction = "DESC";
|
||||
const user = ctx.state.user;
|
||||
const where = {
|
||||
const { user } = ctx.state;
|
||||
const where: WhereOptions<FileOperation> = {
|
||||
teamId: user.teamId,
|
||||
type,
|
||||
};
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
authorize(user, type, team);
|
||||
|
||||
const [exports, total] = await Promise.all([
|
||||
await FileOperation.findAll({
|
||||
where,
|
||||
@@ -58,6 +62,7 @@ router.post("fileOperations.list", auth(), pagination(), async (ctx) => {
|
||||
where,
|
||||
}),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
pagination: { ...ctx.state.pagination, total },
|
||||
data: exports.map(presentFileOperation),
|
||||
@@ -68,7 +73,7 @@ router.post("fileOperations.redirect", auth(), async (ctx) => {
|
||||
const { id } = ctx.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
const user = ctx.state.user;
|
||||
const { user } = ctx.state;
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const fileOp = await FileOperation.unscoped().findByPk(id);
|
||||
|
||||
@@ -90,7 +95,7 @@ router.post("fileOperations.delete", auth(), async (ctx) => {
|
||||
const { id } = ctx.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
const user = ctx.state.user;
|
||||
const { user } = ctx.state;
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const fileOp = await FileOperation.findByPk(id);
|
||||
|
||||
@@ -100,6 +105,7 @@ router.post("fileOperations.delete", auth(), async (ctx) => {
|
||||
|
||||
authorize(user, fileOp.type, team);
|
||||
await fileOperationDeleter(fileOp, user, ctx.request.ip);
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user