Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<RemovePermissionResponse> {
constructor(public readonly id: PermissionId) {
Expand All @@ -16,6 +22,7 @@ export class RemovePermissionCommandHandler implements ICommandHandler<RemovePer
constructor(
@InjectRepository(Permission)
private readonly permissionRepository: EntityRepository<Permission>,
private readonly eventBus: EventBus,
) {}
async execute(
command: RemovePermissionCommand,
Expand All @@ -27,6 +34,7 @@ export class RemovePermissionCommandHandler implements ICommandHandler<RemovePer
throw new PermissionNotFound();
}
await this.permissionRepository.nativeDelete(permission);
this.eventBus.publish(new PermissionRemoved(permission.id));
return new RemovePermissionResponse(
permission.id,
permission.desc,
Expand Down
1 change: 1 addition & 0 deletions template/nest-next/src/permission/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PermissionRemoved } from './permission-removed.event';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PermissionId } from '../permission.entry';

export class PermissionRemoved {
constructor(public readonly id: PermissionId) {}
}
1 change: 1 addition & 0 deletions template/nest-next/src/permission/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './queries';
export * from './errors';
export * from './commands';
export * from './permission.entry';
export * from './events';
1 change: 1 addition & 0 deletions template/nest-next/src/role/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './create-role.command';
export * from './update-role.command';
export * from './remove-role.command';
export * from './unbind-permission.command';
25 changes: 25 additions & 0 deletions template/nest-next/src/role/commands/unbind-permission.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command, CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PermissionId } from '../../permission';
import { EntityRepository } from '@mikro-orm/mysql';
import { RolePermission } from '../role.entity';
import { InjectRepository } from '@mikro-orm/nestjs';

export class UnbindPermission extends Command<void> {
constructor(public permissionId: PermissionId) {
super();
}
}

@CommandHandler(UnbindPermission)
export class UnbindPermissionHandler implements ICommandHandler<UnbindPermission> {
constructor(
@InjectRepository(RolePermission)
private readonly rolePermissionRepo: EntityRepository<RolePermission>,
) {}
async execute(command: UnbindPermission): Promise<void> {
const { permissionId } = command;
await this.rolePermissionRepo.nativeDelete({
permissionId,
});
}
}
1 change: 1 addition & 0 deletions template/nest-next/src/role/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './on-permission-removed';
11 changes: 11 additions & 0 deletions template/nest-next/src/role/event-handler/on-permission-removed.ts
Original file line number Diff line number Diff line change
@@ -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<PermissionRemoved> {
constructor(private readonly cb: CommandBus) {}
async handle(event: PermissionRemoved) {
await this.cb.execute(new UnbindPermission(event.id));
}
}
4 changes: 4 additions & 0 deletions template/nest-next/src/role/role.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
import {
CreateRoleHandler,
RemoveRoleHandler,
UnbindPermissionHandler,
UpdateRoleHandler,
} from './commands';
import { OnPermissionRemoved } from './event-handler';

@Module({
imports: [MikroOrmModule.forFeature([Role, RoleMenu, RolePermission])],
Expand All @@ -29,6 +31,8 @@ import {
CreateRoleHandler,
UpdateRoleHandler,
RemoveRoleHandler,
UnbindPermissionHandler,
OnPermissionRemoved,
],
})
export class RoleModule {}
Loading