0

I have this UpdateAccountDto

export class UpdateAccountDto {
  @ApiPropertyOptional()
  @IsOptional()
  roles: string[];

  @ApiPropertyOptional()
  @IsOptional()
  managers: string[];

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  status: boolean;

  @ApiPropertyOptional()
  @IsString()
  @IsOptional()
  locale: string;
}

I want to use this DTO to make an update request as admin and user. If admin makes request, accept all above fields to be updated If user makes request, only accept locale to be updated (not roles, managers, or status)

Is there any good way to do this?

Currently I have to make 2 DTOs and 2 services, one for admin and one for user. So I'm trying to find a way to reuse from 1 DTO & 1 service

Thank you in advance!

Catopus
  • 81
  • 1
  • 8
  • https://www.npmjs.com/package/class-transformer#using-groups-to-control-excluded-properties – cojack Jul 21 '22 at 07:17
  • You might find a solution using the solution in this link https://stackoverflow.com/a/67718530/14332805 . Just do not use IsOptional use IsNotEmpty instead. – Siddhesh Khadapkar Jul 21 '22 at 19:42

1 Answers1

0

If you want one service to update all the fields, your first approach is correct;

But my approach is with a lot of roles (Admin, sysAdmin, Operator, User):

  • Create a service to update roles with a new Dto just for Admin, sysAdmin, Operator,...

  • Create a service to update managers with a new Dto just for Admin,...

  • Create a service to update status with a new Dto just for Admin,...

  • Create a service to update locale with a new Dto just for User,...

In my opinion, your business should not depend on your roles:

for example, if you create a new Role you should not change all the code

Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39