feat: Allow deletion of imports (#5907)
This commit is contained in:
36
server/commands/collectionDestroyer.ts
Normal file
36
server/commands/collectionDestroyer.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { Collection, Event, User } from "@server/models";
|
||||
|
||||
type Props = {
|
||||
/** The collection to delete */
|
||||
collection: Collection;
|
||||
/** The actor who is deleting the collection */
|
||||
user: User;
|
||||
/** The database transaction to use */
|
||||
transaction: Transaction;
|
||||
/** The IP address of the current request */
|
||||
ip: string;
|
||||
};
|
||||
|
||||
export default async function collectionDestroyer({
|
||||
collection,
|
||||
transaction,
|
||||
user,
|
||||
ip,
|
||||
}: Props) {
|
||||
await collection.destroy({ transaction });
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "collections.delete",
|
||||
collectionId: collection.id,
|
||||
teamId: collection.teamId,
|
||||
actorId: user.id,
|
||||
data: {
|
||||
name: collection.name,
|
||||
},
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildAdmin, buildFileOperation } from "@server/test/factories";
|
||||
import fileOperationDeleter from "./fileOperationDeleter";
|
||||
|
||||
describe("fileOperationDeleter", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should destroy file operation", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const fileOp = await buildFileOperation({
|
||||
userId: admin.id,
|
||||
teamId: admin.teamId,
|
||||
});
|
||||
await fileOperationDeleter(fileOp, admin, ip);
|
||||
expect(
|
||||
await FileOperation.count({
|
||||
where: {
|
||||
teamId: admin.teamId,
|
||||
},
|
||||
})
|
||||
).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -1,32 +1,30 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { FileOperation, Event, User } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
|
||||
export default async function fileOperationDeleter(
|
||||
fileOperation: FileOperation,
|
||||
user: User,
|
||||
ip: string
|
||||
) {
|
||||
const transaction = await sequelize.transaction();
|
||||
type Props = {
|
||||
fileOperation: FileOperation;
|
||||
user: User;
|
||||
ip: string;
|
||||
transaction: Transaction;
|
||||
};
|
||||
|
||||
try {
|
||||
await fileOperation.destroy({
|
||||
export default async function fileOperationDeleter({
|
||||
fileOperation,
|
||||
user,
|
||||
ip,
|
||||
transaction,
|
||||
}: Props) {
|
||||
await fileOperation.destroy({ transaction });
|
||||
await Event.create(
|
||||
{
|
||||
name: "fileOperations.delete",
|
||||
teamId: user.teamId,
|
||||
actorId: user.id,
|
||||
modelId: fileOperation.id,
|
||||
ip,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
});
|
||||
await Event.create(
|
||||
{
|
||||
name: "fileOperations.delete",
|
||||
teamId: user.teamId,
|
||||
actorId: user.id,
|
||||
modelId: fileOperation.id,
|
||||
ip,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
await transaction.commit();
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user