Skip to content

Commit

Permalink
feat: user max document handle (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
KostaD02 authored Mar 9, 2024
1 parent 7a2c449 commit ee7198d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/consts/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MAX_DATABASE_USER = 1000;
1 change: 1 addition & 0 deletions src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './api.config';
export * from './database';
1 change: 1 addition & 0 deletions src/enums/exceptions.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/http-exceptions.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
) {
Expand Down
25 changes: 24 additions & 1 deletion src/modules/user/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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,
};
}
}

0 comments on commit ee7198d

Please sign in to comment.