From 6d43fcfe2902808dd44f4234715557fb4f4f6c1a Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Thu, 22 Oct 2020 19:51:30 +0530 Subject: [PATCH] :sparkles: Use DTO in PATCH method --- src/modules/user/user.controller.ts | 5 +-- src/modules/user/user.dto.ts | 56 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/modules/user/user.dto.ts diff --git a/src/modules/user/user.controller.ts b/src/modules/user/user.controller.ts index d7405621b..023e06b66 100644 --- a/src/modules/user/user.controller.ts +++ b/src/modules/user/user.controller.ts @@ -8,12 +8,13 @@ import { Patch, Query, } from '@nestjs/common'; -import { users, usersUpdateInput } from '@prisma/client'; +import { users } from '@prisma/client'; import { OmitSecrets } from 'src/modules/prisma/prisma.interface'; import { CursorPipe } from 'src/pipes/cursor.pipe'; import { OptionalIntPipe } from 'src/pipes/optional-int.pipe'; import { OrderByPipe } from 'src/pipes/order-by.pipe'; import { WherePipe } from 'src/pipes/where.pipe'; +import { UpdateUserDto } from './user.dto'; import { UsersService } from './user.service'; @Controller('users') @@ -41,7 +42,7 @@ export class UserController { @Patch(':id') async update( @Param('id', ParseIntPipe) id: number, - @Body() data: usersUpdateInput, + @Body() data: UpdateUserDto, ): Promise> { return this.usersService.updateUser({ where: { id: Number(id) }, data }); } diff --git a/src/modules/user/user.dto.ts b/src/modules/user/user.dto.ts new file mode 100644 index 000000000..2e6deab67 --- /dev/null +++ b/src/modules/user/user.dto.ts @@ -0,0 +1,56 @@ +import { + IsBoolean, + IsIn, + IsLocale, + IsObject, + IsString, + IsUrl, + Length, + MinLength, +} from 'class-validator'; + +export class UpdateUserDto { + @IsBoolean() + checkLocationOnLogin: boolean; + + @IsString() + @Length(2, 2) + countryCode: string; + + @IsString() + @IsIn(['MALE', 'FEMALE', 'NONBINARY', 'UNKNOWN']) + gender: 'MALE' | 'FEMALE' | 'NONBINARY' | 'UNKNOWN'; + + @IsString() + @MinLength(3) + name: string; + + @IsIn(['ACCOUNT', 'UPDATES', 'PROMOTIONS']) + notificationEmails: 'ACCOUNT' | 'UPDATES' | 'PROMOTIONS'; + + @IsString() + password: string | null; + + @IsLocale() + prefersLanguage: string; + + @IsString() + @IsIn(['NO_PREFERENCE', 'LIGHT', 'DARK']) + prefersColorScheme: 'NO_PREFERENCE' | 'LIGHT' | 'DARK'; + + @IsString() + @IsIn(['NO_PREFERENCE', 'REDUCE']) + prefersReducedMotion: 'NO_PREFERENCE' | 'REDUCE'; + + @IsUrl() + profilePictureUrl: string; + + @IsString() + timezone: string; + + @IsBoolean() + twoFactorEnabled: boolean; + + @IsObject() + attributes: Record; +}