From 8637ed2565eb55348bebb55ecd68cad0bada1ed1 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 2 Nov 2023 10:03:58 +0300 Subject: [PATCH] app/api: check if account already exists --- apps/api/src/account/account.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/api/src/account/account.service.ts b/apps/api/src/account/account.service.ts index d1b123b..235ad32 100644 --- a/apps/api/src/account/account.service.ts +++ b/apps/api/src/account/account.service.ts @@ -1,5 +1,5 @@ import type { CreateAccountDto } from './dto/create-account.dto'; -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { InjectModel } from '@nestjs/mongoose'; import * as bcrypt from 'bcrypt'; @@ -18,6 +18,10 @@ export class AccountService { ) {} public async create(createAccountDto: CreateAccountDto): Promise { + if (this.accountModel.exists({ username: createAccountDto.username })) { + throw new BadRequestException('Account already exists'); + } + const password = createAccountDto.password || generatePassword(); const createdAccount = new this.accountModel({ ...createAccountDto, password });