From f8c78bfa4052aa1f599321e0afe03c8145a8697c Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 17 Jan 2024 17:45:57 +0300 Subject: [PATCH] apps/api:add UpdateAccountDto --- apps/api/src/account/account.controller.ts | 5 +++-- apps/api/src/account/account.service.ts | 3 ++- apps/api/src/account/dto/update-account.dto.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 apps/api/src/account/dto/update-account.dto.ts diff --git a/apps/api/src/account/account.controller.ts b/apps/api/src/account/account.controller.ts index 548678a..36af0a4 100644 --- a/apps/api/src/account/account.controller.ts +++ b/apps/api/src/account/account.controller.ts @@ -4,6 +4,7 @@ import { AccountService } from './account.service'; import { CreateAccountDto } from './dto/create-account.dto'; import { ResetPasswordDto } from './dto/reset-password.dto'; +import { UpdateAccountDto } from './dto/update-account.dto'; import { Body, Controller, @@ -75,9 +76,9 @@ export class AccountController { status: HttpStatus.OK, type: Account, }) - async update(@Body() createAccountDto: CreateAccountDto, @Res() reply: FastifyReply) { + async update(@Body() updateAccountDto: UpdateAccountDto, @Res() reply: FastifyReply) { try { - const updatedAccount = await this.accountService.update(createAccountDto); + const updatedAccount = await this.accountService.update(updateAccountDto); return reply.status(HttpStatus.OK).send(updatedAccount); } catch (error) { diff --git a/apps/api/src/account/account.service.ts b/apps/api/src/account/account.service.ts index 15988ce..5f9b8f8 100644 --- a/apps/api/src/account/account.service.ts +++ b/apps/api/src/account/account.service.ts @@ -1,5 +1,6 @@ import type { CreateAccountDto } from './dto/create-account.dto'; import type { ResetPasswordDto } from './dto/reset-password.dto'; +import type { UpdateAccountDto } from './dto/update-account.dto'; import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { InjectModel } from '@nestjs/mongoose'; @@ -48,7 +49,7 @@ export class AccountService { return this.accountModel.findOneAndDelete({ username }).exec(); } - public async update({ username, ...props }: CreateAccountDto): Promise { + public async update({ username, ...props }: UpdateAccountDto): Promise { Object.keys(props).forEach((field) => { if (['_id', '__v', 'password'].includes(field)) throw new BadRequestException(`Prop ${field} is not allowed`); diff --git a/apps/api/src/account/dto/update-account.dto.ts b/apps/api/src/account/dto/update-account.dto.ts new file mode 100644 index 0000000..703521b --- /dev/null +++ b/apps/api/src/account/dto/update-account.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class UpdateAccountDto { + @ApiProperty() + @IsString() + @IsNotEmpty() + public readonly username: string; + + readonly [key: string]: unknown; +}