diff --git a/template/nest-next/src/permission/commands/remove-permission.command.ts b/template/nest-next/src/permission/commands/remove-permission.command.ts index d347e1fe..a9521a00 100644 --- a/template/nest-next/src/permission/commands/remove-permission.command.ts +++ b/template/nest-next/src/permission/commands/remove-permission.command.ts @@ -1,9 +1,15 @@ -import { Command, CommandHandler, ICommandHandler } from '@nestjs/cqrs'; +import { + Command, + CommandHandler, + EventBus, + ICommandHandler, +} from '@nestjs/cqrs'; import { RemovePermissionResponse } from '../dto/remove-permission.dto'; import { EntityRepository } from '@mikro-orm/mysql'; import { InjectRepository } from '@mikro-orm/nestjs'; import { Permission, PermissionId } from '../permission.entry'; import { PermissionNotFound } from '../errors'; +import { PermissionRemoved } from '../events'; export class RemovePermissionCommand extends Command { constructor(public readonly id: PermissionId) { @@ -16,6 +22,7 @@ export class RemovePermissionCommandHandler implements ICommandHandler, + private readonly eventBus: EventBus, ) {} async execute( command: RemovePermissionCommand, @@ -27,6 +34,7 @@ export class RemovePermissionCommandHandler implements ICommandHandler { + constructor(public permissionId: PermissionId) { + super(); + } +} + +@CommandHandler(UnbindPermission) +export class UnbindPermissionHandler implements ICommandHandler { + constructor( + @InjectRepository(RolePermission) + private readonly rolePermissionRepo: EntityRepository, + ) {} + async execute(command: UnbindPermission): Promise { + const { permissionId } = command; + await this.rolePermissionRepo.nativeDelete({ + permissionId, + }); + } +} diff --git a/template/nest-next/src/role/event-handler/index.ts b/template/nest-next/src/role/event-handler/index.ts new file mode 100644 index 00000000..2934874e --- /dev/null +++ b/template/nest-next/src/role/event-handler/index.ts @@ -0,0 +1 @@ +export * from './on-permission-removed'; diff --git a/template/nest-next/src/role/event-handler/on-permission-removed.ts b/template/nest-next/src/role/event-handler/on-permission-removed.ts new file mode 100644 index 00000000..5941497c --- /dev/null +++ b/template/nest-next/src/role/event-handler/on-permission-removed.ts @@ -0,0 +1,11 @@ +import { CommandBus, EventsHandler, IEventHandler } from '@nestjs/cqrs'; +import { PermissionRemoved } from 'src/permission'; +import { UnbindPermission } from '../commands'; + +@EventsHandler(PermissionRemoved) +export class OnPermissionRemoved implements IEventHandler { + constructor(private readonly cb: CommandBus) {} + async handle(event: PermissionRemoved) { + await this.cb.execute(new UnbindPermission(event.id)); + } +} diff --git a/template/nest-next/src/role/role.module.ts b/template/nest-next/src/role/role.module.ts index bc19974c..86cdf5cf 100644 --- a/template/nest-next/src/role/role.module.ts +++ b/template/nest-next/src/role/role.module.ts @@ -13,8 +13,10 @@ import { import { CreateRoleHandler, RemoveRoleHandler, + UnbindPermissionHandler, UpdateRoleHandler, } from './commands'; +import { OnPermissionRemoved } from './event-handler'; @Module({ imports: [MikroOrmModule.forFeature([Role, RoleMenu, RolePermission])], @@ -29,6 +31,8 @@ import { CreateRoleHandler, UpdateRoleHandler, RemoveRoleHandler, + UnbindPermissionHandler, + OnPermissionRemoved, ], }) export class RoleModule {}