From 936adf0a69cc94af86d9395879e99bdc726fa7cf Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Sat, 8 Jun 2024 21:21:43 -0300 Subject: [PATCH] refactor: Update update_users_activity_usecase to handle conflict errors --- .../database/repositories/ActivityRepo.ts | 14 +++++++++----- .../app/update_users_activity_controller.ts | 5 +++++ .../app/update_users_activity_usecase.ts | 7 ++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/core/repositories/database/repositories/ActivityRepo.ts b/src/core/repositories/database/repositories/ActivityRepo.ts index 8531a78..9dec53d 100644 --- a/src/core/repositories/database/repositories/ActivityRepo.ts +++ b/src/core/repositories/database/repositories/ActivityRepo.ts @@ -437,8 +437,10 @@ export class ActivityRepo implements IActivityRepo { } async update_users_activity_status(activity_id: string, users: { user_id: string, status: boolean }[]): Promise { - for (let user of users) { - const response = await ActivityApplication.update({ + let response = true; + + for (const user of users) { + const [affectedCount] = await ActivityApplication.update({ status: user.status }, { where: { @@ -446,11 +448,13 @@ export class ActivityRepo implements IActivityRepo { user_id: user.user_id } }); - if (response[0] === 0) { - return false; + + if (affectedCount === 0) { + response = false; } } - return true; + + return response; } async get_all_activities_catalog(): Promise<{ title: string; logo: string; type_activity: ActivityTypeEnum; }[]> { diff --git a/src/modules/update_users_activity/app/update_users_activity_controller.ts b/src/modules/update_users_activity/app/update_users_activity_controller.ts index 7c7f6ba..eece7d7 100644 --- a/src/modules/update_users_activity/app/update_users_activity_controller.ts +++ b/src/modules/update_users_activity/app/update_users_activity_controller.ts @@ -1,5 +1,6 @@ import { UpdateUsersActivityUsecase } from "./update_users_activity_usecase"; import { + ConflictError, InvalidParameter, InvalidRequest, MissingParameter, @@ -16,6 +17,7 @@ import { OK, ParameterError, Unauthorized, + Unprocessable_Entity, } from "../../../core/helpers/http/http_codes"; import { EntityError } from "../../../core/helpers/errors/EntityError"; @@ -54,6 +56,9 @@ export class UpdateUsersActivityController { if (error instanceof NotfoundError) { return new NotFound(error.message); } + if (error instanceof ConflictError) { + return new Unprocessable_Entity(error.message); + } if (error instanceof EntityError) { return new ParameterError(error.message); } diff --git a/src/modules/update_users_activity/app/update_users_activity_usecase.ts b/src/modules/update_users_activity/app/update_users_activity_usecase.ts index a7f0209..6a82520 100644 --- a/src/modules/update_users_activity/app/update_users_activity_usecase.ts +++ b/src/modules/update_users_activity/app/update_users_activity_usecase.ts @@ -4,7 +4,8 @@ import { NotfoundError, UserNotAllowed, UserNotAuthenticated, - InvalidParameter + InvalidParameter, + ConflictError } from "../../../core/helpers/errors/ModuleError"; import { TokenAuth } from "../../../core/helpers/functions/token_auth"; import { UserTypeEnum } from "../../../core/helpers/enums/UserTypeEnum"; @@ -95,7 +96,7 @@ export class UpdateUsersActivityUsecase { throw new NotfoundError("Applicants not found"); } - applicants_db.forEach((applicant: {user_id: string, status: boolean}) => { + applicants_db.forEach((applicant: { user_id: string, status: boolean }) => { return { user_id: applicant.user_id, status: !applicant.status, @@ -104,7 +105,7 @@ export class UpdateUsersActivityUsecase { const updateStatusResult = await this.activity_repo.update_users_activity_status(body.activity_id, body.applicants); if (!updateStatusResult) { - throw new NotfoundError("Activity not found"); + throw new ConflictError("Error updating activity status"); } return true;