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:
56
server/queues/processors/UserDeletedProcessor.ts
Normal file
56
server/queues/processors/UserDeletedProcessor.ts
Normal 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,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user