From ee7198d0ff659a937085acada2ab2f0a53eab195 Mon Sep 17 00:00:00 2001 From: Konstantine Datunishvili <68782786+KostaD02@users.noreply.github.com> Date: Sat, 9 Mar 2024 18:34:56 +0400 Subject: [PATCH] feat: user max document handle (#101) --- src/consts/database.ts | 1 + src/consts/index.ts | 1 + src/enums/exceptions.enum.ts | 1 + src/http-exceptions.filter.ts | 1 - src/modules/user/auth/auth.service.ts | 25 ++++++++++++++++++++++++- 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/consts/database.ts diff --git a/src/consts/database.ts b/src/consts/database.ts new file mode 100644 index 0000000..72f0d5d --- /dev/null +++ b/src/consts/database.ts @@ -0,0 +1 @@ +export const MAX_DATABASE_USER = 1000; diff --git a/src/consts/index.ts b/src/consts/index.ts index ce41b6a..10b50fb 100644 --- a/src/consts/index.ts +++ b/src/consts/index.ts @@ -1 +1,2 @@ export * from './api.config'; +export * from './database'; diff --git a/src/enums/exceptions.enum.ts b/src/enums/exceptions.enum.ts index a5c6a30..6273a4e 100644 --- a/src/enums/exceptions.enum.ts +++ b/src/enums/exceptions.enum.ts @@ -100,6 +100,7 @@ export enum AuthExpectionKeys { UserNotFound = 'errors.user_not_found', ShouldProvideEmail = 'errors.should_provide_email', ShouldProvidePassword = 'errors.should_provide_password', + ClearingDatabase = 'errors.clearing_database', } export enum CartExpectionKeys { diff --git a/src/http-exceptions.filter.ts b/src/http-exceptions.filter.ts index 307a481..113053b 100644 --- a/src/http-exceptions.filter.ts +++ b/src/http-exceptions.filter.ts @@ -34,7 +34,6 @@ export class HttpExceptionsFilter implements ExceptionFilter { let isBadJSON = false; if (!Array.isArray(exceptionResponse.message)) { - console.log(badJSONTexts.includes(exceptionResponse.message)); if ( badJSONTexts.some((e) => e.search(exceptionResponse.message as string)) ) { diff --git a/src/modules/user/auth/auth.service.ts b/src/modules/user/auth/auth.service.ts index 3652a1a..44d58a8 100644 --- a/src/modules/user/auth/auth.service.ts +++ b/src/modules/user/auth/auth.service.ts @@ -22,7 +22,7 @@ import { generateVerifyPageTemplate, generateResetPageTemplate, } from 'src/modules/mail/templates'; -import { API_CONFIG } from 'src/consts'; +import { API_CONFIG, MAX_DATABASE_USER } from 'src/consts'; @Injectable() export class AuthService { @@ -38,6 +38,7 @@ export class AuthService { ) {} async signUp(body: SignUpDto) { + this.handleMaximumAmountUsers(); const userExsists = await this.userModel.findOne({ email: body.email }); if (userExsists) { this.exceptionService.throwError( @@ -473,4 +474,26 @@ export class AuthService { }), }; } + + async handleMaximumAmountUsers() { + const usersAmount = await this.userModel.find({}).countDocuments(); + if (usersAmount <= MAX_DATABASE_USER) { + return; + } + + const deletedCount = this.userModel + .deleteMany({}) + .deleteMany() + .countDocuments(); + + this.exceptionService.throwError( + ExceptionStatusKeys.Conflict, + 'Database reached maximum uesrs, currently deleting all user, send request again', + AuthExpectionKeys.ClearingDatabase, + ); + + return { + deleted: deletedCount, + }; + } }