From e0e773454d81ff05b78bee21f3f108f2d5e906fb Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 10 Jan 2025 18:38:17 +0100 Subject: [PATCH] feat: implement dynamic create DTO for Permission --- api/src/user/controllers/permission.controller.ts | 8 ++++---- api/src/user/dto/permission.dto.ts | 7 ++++++- api/src/user/repositories/permission.repository.ts | 4 +++- api/src/user/schemas/permission.schema.ts | 3 ++- api/src/user/seeds/permission.seed.ts | 4 +++- api/src/user/services/permission.service.ts | 4 +++- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/api/src/user/controllers/permission.controller.ts b/api/src/user/controllers/permission.controller.ts index 6f3acb03..dd0ff79f 100644 --- a/api/src/user/controllers/permission.controller.ts +++ b/api/src/user/controllers/permission.controller.ts @@ -47,10 +47,10 @@ export class PermissionController extends BaseController< PermissionFull > { constructor( - private readonly permissionService: PermissionService, - private readonly logger: LoggerService, - private readonly roleService: RoleService, - private readonly modelService: ModelService, + protected readonly permissionService: PermissionService, + protected readonly logger: LoggerService, + protected readonly roleService: RoleService, + protected readonly modelService: ModelService, ) { super(permissionService); } diff --git a/api/src/user/dto/permission.dto.ts b/api/src/user/dto/permission.dto.ts index af4e7ce9..93b33ded 100644 --- a/api/src/user/dto/permission.dto.ts +++ b/api/src/user/dto/permission.dto.ts @@ -7,8 +7,9 @@ */ import { ApiProperty } from '@nestjs/swagger'; -import { IsEnum, IsNotEmpty, IsString, IsOptional } from 'class-validator'; +import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { DtoConfig } from '@/utils/types/dto.types'; import { IsObjectId } from '@/utils/validation-rules/is-object-id'; import { Action } from '../types/action.type'; @@ -40,3 +41,7 @@ export class PermissionCreateDto { @IsOptional() relation?: TRelation; } + +export type PermissionDTOMapActions = DtoConfig<{ + create: PermissionCreateDto; +}>; diff --git a/api/src/user/repositories/permission.repository.ts b/api/src/user/repositories/permission.repository.ts index 692aa7fd..b7e60f17 100644 --- a/api/src/user/repositories/permission.repository.ts +++ b/api/src/user/repositories/permission.repository.ts @@ -13,6 +13,7 @@ import { Model } from 'mongoose'; import { BaseRepository } from '@/utils/generics/base-repository'; +import { PermissionDTOMapActions } from '../dto/permission.dto'; import { Permission, PERMISSION_POPULATE, @@ -24,7 +25,8 @@ import { export class PermissionRepository extends BaseRepository< Permission, PermissionPopulate, - PermissionFull + PermissionFull, + PermissionDTOMapActions > { constructor( readonly eventEmitter: EventEmitter2, diff --git a/api/src/user/schemas/permission.schema.ts b/api/src/user/schemas/permission.schema.ts index 9c87fe38..0af884aa 100644 --- a/api/src/user/schemas/permission.schema.ts +++ b/api/src/user/schemas/permission.schema.ts @@ -18,6 +18,7 @@ import { } from '@/utils/types/filter.types'; import { Action } from '../types/action.type'; +import { TRelation } from '../types/index.type'; import { Model } from './model.schema'; import { Role } from './role.schema'; @@ -41,7 +42,7 @@ export class PermissionStub extends BaseSchema { type: String, default: 'role', }) - relation?: string; + relation: TRelation; } @Schema({ timestamps: true }) diff --git a/api/src/user/seeds/permission.seed.ts b/api/src/user/seeds/permission.seed.ts index 7492d2e8..98f179f8 100644 --- a/api/src/user/seeds/permission.seed.ts +++ b/api/src/user/seeds/permission.seed.ts @@ -10,6 +10,7 @@ import { Injectable } from '@nestjs/common'; import { BaseSeeder } from '@/utils/generics/base-seeder'; +import { PermissionDTOMapActions } from '../dto/permission.dto'; import { PermissionRepository } from '../repositories/permission.repository'; import { Permission, @@ -21,7 +22,8 @@ import { export class PermissionSeeder extends BaseSeeder< Permission, PermissionPopulate, - PermissionFull + PermissionFull, + PermissionDTOMapActions > { constructor(private readonly permissionRepository: PermissionRepository) { super(permissionRepository); diff --git a/api/src/user/services/permission.service.ts b/api/src/user/services/permission.service.ts index 244c203f..e867b01a 100644 --- a/api/src/user/services/permission.service.ts +++ b/api/src/user/services/permission.service.ts @@ -15,6 +15,7 @@ import { PERMISSION_CACHE_KEY } from '@/utils/constants/cache'; import { Cacheable } from '@/utils/decorators/cacheable.decorator'; import { BaseService } from '@/utils/generics/base-service'; +import { PermissionDTOMapActions } from '../dto/permission.dto'; import { PermissionRepository } from '../repositories/permission.repository'; import { Permission, @@ -27,7 +28,8 @@ import { PermissionsTree } from '../types/permission.type'; export class PermissionService extends BaseService< Permission, PermissionPopulate, - PermissionFull + PermissionFull, + PermissionDTOMapActions > { constructor( readonly repository: PermissionRepository,