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
Expand Up @@ -18,6 +18,7 @@ export type I18nTranslations = {
};
"role": {
"NOT_FOUND": string;
"EXISTS": string;
};
};
"validation": {
Expand Down
3 changes: 2 additions & 1 deletion template/nest-next/src/i18n/enUS/exception.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"NOT_FOUND": "Menu Not Found"
},
"role":{
"NOT_FOUND": "Role not found"
"NOT_FOUND": "Role not found",
"EXISTS": "Role {name} Exists"
}
}
9 changes: 7 additions & 2 deletions template/nest-next/src/role/commands/create-role.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EntityManager } from '@mikro-orm/core';
import { RedisService } from '@liaoliaots/nestjs-redis';
import Redis from 'ioredis';
import { roleTotal } from '@app/shared';
import { RoleExistsError } from '../errors';

export class CreateRoleRequest extends CreateRoleDto {}

Expand All @@ -31,8 +32,12 @@ export class CreateRoleHandler implements ICommandHandler<CreateRole> {
) {
this.redis = this.redisService.getOrThrow();
}
execute({ data }: CreateRole): Promise<RoleId> {
const { name, permissionIds, menuIds=[] } = data;
async execute({ data }: CreateRole): Promise<RoleId> {
const { name, permissionIds = [], menuIds = [] } = data;
const exists = await this.role.findOne({ name });
if (exists) {
throw new RoleExistsError(name);
}
const role = this.role.create({ name });
const rolePermission = permissionIds.map((id) => {
return this.permission.create({ roleId: role.id, permissionId: id });
Expand Down
1 change: 1 addition & 0 deletions template/nest-next/src/role/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { RoleNotFound } from './role-not-found';
export { RoleExistsError } from './role-exists';
12 changes: 12 additions & 0 deletions template/nest-next/src/role/errors/role-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DomainError } from '@app/shared';
import { HttpStatus } from '@nestjs/common';

export class RoleExistsError extends DomainError {
constructor(name: string) {
super({
code: HttpStatus.BAD_REQUEST,
message: `exception.role.EXISTS`,
params: { name },
});
}
}
Loading