apps/api:add UpdateAccountDto

This commit is contained in:
vchikalkin 2024-01-17 17:45:57 +03:00
parent 01f4378e11
commit f8c78bfa40
3 changed files with 16 additions and 3 deletions

View File

@ -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) {

View File

@ -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<Account> {
public async update({ username, ...props }: UpdateAccountDto): Promise<Account> {
Object.keys(props).forEach((field) => {
if (['_id', '__v', 'password'].includes(field))
throw new BadRequestException(`Prop ${field} is not allowed`);

View File

@ -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;
}