Skip to content

Commit

Permalink
refactor: Update update_users_activity_usecase to handle conflict errors
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeCarillo committed Jun 9, 2024
1 parent ac7f41f commit 936adf0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/core/repositories/database/repositories/ActivityRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,24 @@ export class ActivityRepo implements IActivityRepo {
}

async update_users_activity_status(activity_id: string, users: { user_id: string, status: boolean }[]): Promise<boolean> {
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: {
activity_id: activity_id,
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; }[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UpdateUsersActivityUsecase } from "./update_users_activity_usecase";
import {
ConflictError,
InvalidParameter,
InvalidRequest,
MissingParameter,
Expand All @@ -16,6 +17,7 @@ import {
OK,
ParameterError,
Unauthorized,
Unprocessable_Entity,
} from "../../../core/helpers/http/http_codes";
import { EntityError } from "../../../core/helpers/errors/EntityError";

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 936adf0

Please sign in to comment.