app/api: check if account already exists

This commit is contained in:
vchikalkin 2023-11-02 10:03:58 +03:00
parent 3bbdf1b8a7
commit 8637ed2565

View File

@ -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<Account> {
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 });