fix: Cleanup relationships when user is deleted (#6343)

* fix: Cleanup relationships when user is deleted

* Update tests

* Update User.test.ts
This commit is contained in:
Tom Moor
2024-01-03 06:14:10 -08:00
committed by GitHub
parent 7606a3af41
commit 67a6b3fe43
4 changed files with 94 additions and 36 deletions

View File

@@ -0,0 +1,35 @@
import UserAuthentication from "@server/models/UserAuthentication";
import { buildUser } from "@server/test/factories";
import UserDeletedProcessor from "./UserDeletedProcessor";
const ip = "127.0.0.1";
describe("UserDeletedProcessor", () => {
test("should remove relationships", async () => {
const user = await buildUser();
expect(
await UserAuthentication.count({
where: {
userId: user.id,
},
})
).toBe(1);
const processor = new UserDeletedProcessor();
await processor.perform({
name: "users.delete",
userId: user.id,
actorId: user.id,
teamId: user.teamId,
ip,
});
expect(
await UserAuthentication.count({
where: {
userId: user.id,
},
})
).toBe(0);
});
});

View File

@@ -0,0 +1,56 @@
import {
ApiKey,
GroupUser,
Star,
Subscription,
UserAuthentication,
UserPermission,
} from "@server/models";
import { sequelize } from "@server/storage/database";
import { Event as TEvent, UserEvent } from "@server/types";
import BaseProcessor from "./BaseProcessor";
export default class UsersDeletedProcessor extends BaseProcessor {
static applicableEvents: TEvent["name"][] = ["users.delete"];
async perform(event: UserEvent) {
await sequelize.transaction(async (transaction) => {
await GroupUser.destroy({
where: {
userId: event.userId,
},
transaction,
});
await UserAuthentication.destroy({
where: {
userId: event.userId,
},
transaction,
});
await UserPermission.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Subscription.destroy({
where: {
userId: event.userId,
},
transaction,
});
await ApiKey.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Star.destroy({
where: {
userId: event.userId,
},
transaction,
});
});
}
}