feat: Allow deletion of imports (#5907)

This commit is contained in:
Tom Moor
2023-10-01 21:24:50 -04:00
committed by GitHub
parent 16cd82a732
commit e7b7032284
24 changed files with 304 additions and 184 deletions

View File

@@ -7,9 +7,9 @@ import {
FileOperationState,
FileOperationType,
} from "@shared/types";
import collectionDestroyer from "@server/commands/collectionDestroyer";
import collectionExporter from "@server/commands/collectionExporter";
import teamUpdater from "@server/commands/teamUpdater";
import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { rateLimiter } from "@server/middlewares/rateLimiter";
import { transaction } from "@server/middlewares/transaction";
@@ -803,44 +803,15 @@ router.post(
}).findByPk(id, {
transaction,
});
const team = await Team.findByPk(user.teamId);
authorize(user, "delete", collection);
const total = await Collection.count({
where: {
teamId: user.teamId,
},
await collectionDestroyer({
collection,
transaction,
user,
ip: ctx.request.ip,
});
if (total === 1) {
throw ValidationError("Cannot delete last collection");
}
await collection.destroy({ transaction });
if (team && team.defaultCollectionId === collection.id) {
await teamUpdater({
params: { defaultCollectionId: null },
ip: ctx.request.ip,
user,
team,
transaction,
});
}
await Event.create(
{
name: "collections.delete",
collectionId: collection.id,
teamId: collection.teamId,
actorId: user.id,
data: {
name: collection.name,
},
ip: ctx.request.ip,
},
{ transaction }
);
ctx.body = {
success: true,

View File

@@ -2301,7 +2301,7 @@ describe("#documents.restore", () => {
teamId: team.id,
});
await document.destroy();
await collection.destroy();
await collection.destroy({ hooks: false });
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),

View File

@@ -150,7 +150,7 @@ describe("#fileOperations.list", () => {
userId: admin.id,
collectionId: collection.id,
});
await collection.destroy();
await collection.destroy({ hooks: false });
const isCollectionPresent = await Collection.findByPk(collection.id);
expect(isCollectionPresent).toBe(null);
const res = await server.post("/api/fileOperations.list", {

View File

@@ -3,6 +3,7 @@ import { WhereOptions } from "sequelize";
import fileOperationDeleter from "@server/commands/fileOperationDeleter";
import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { FileOperation, Team } from "@server/models";
import { authorize } from "@server/policies";
@@ -105,16 +106,23 @@ router.post(
"fileOperations.delete",
auth({ admin: true }),
validate(T.FileOperationsDeleteSchema),
transaction(),
async (ctx: APIContext<T.FileOperationsDeleteReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const fileOperation = await FileOperation.unscoped().findByPk(id, {
rejectOnEmpty: true,
});
authorize(user, "delete", fileOperation);
await fileOperationDeleter(fileOperation, user, ctx.request.ip);
await fileOperationDeleter({
fileOperation,
user,
ip: ctx.request.ip,
transaction,
});
ctx.body = {
success: true,

View File

@@ -216,48 +216,6 @@ describe("#team.update", () => {
expect(body.data.defaultCollectionId).toEqual(collection.id);
});
it("should default to home if default collection is deleted", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
userId: admin.id,
});
await buildCollection({
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/team.update", {
body: {
token: admin.getJwtToken(),
defaultCollectionId: collection.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.defaultCollectionId).toEqual(collection.id);
const deleteRes = await server.post("/api/collections.delete", {
body: {
token: admin.getJwtToken(),
id: collection.id,
},
});
expect(deleteRes.status).toEqual(200);
const res3 = await server.post("/api/auth.info", {
body: {
token: admin.getJwtToken(),
},
});
const body3 = await res3.json();
expect(res3.status).toEqual(200);
expect(body3.data.team.defaultCollectionId).toEqual(null);
});
it("should update default collection to null when collection is made private", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });