From 946d977db87406e24a912faa70aabdb6359ffaae Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Fri, 12 Jan 2024 16:08:24 +0300 Subject: [PATCH] apps/api: check if account exists before create --- apps/api/src/account/account.service.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/api/src/account/account.service.ts b/apps/api/src/account/account.service.ts index f4d5181..39ec7b5 100644 --- a/apps/api/src/account/account.service.ts +++ b/apps/api/src/account/account.service.ts @@ -18,6 +18,13 @@ export class AccountService { ) {} public async create(createAccountDto: CreateAccountDto): Promise { + const isExist = await this.accountModel.exists({ username: createAccountDto.username }).exec(); + + if (isExist) + throw new BadRequestException( + `Account with username '${createAccountDto.username}' already exists` + ); + Object.keys(createAccountDto).forEach((field) => { if (['_id', '__v'].includes(field)) throw new BadRequestException(`Prop ${field} is not allowed`);