From ac53e6b64d27194f80ab7d98ddfaa03af33a30ac Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Sun, 31 Mar 2024 00:27:45 +0100 Subject: [PATCH 01/11] Get institution routes The get institution controler/presenter/usecase have been completed. --- .../app/get_institution_controler.ts | 45 +++++++++++++++++++ .../app/get_institution_presenter.ts | 16 +++++++ .../app/get_institution_usecase.ts | 21 +++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/modules/get_institution/app/get_institution_controler.ts create mode 100644 src/modules/get_institution/app/get_institution_presenter.ts create mode 100644 src/modules/get_institution/app/get_institution_usecase.ts diff --git a/src/modules/get_institution/app/get_institution_controler.ts b/src/modules/get_institution/app/get_institution_controler.ts new file mode 100644 index 0000000..b272988 --- /dev/null +++ b/src/modules/get_institution/app/get_institution_controler.ts @@ -0,0 +1,45 @@ +import { GetInstitutionUsecase } from "./get_institution_usecase"; + +import { EntityError } from '../../../core/helpers/errors/EntityError'; +import { Conflict, OK, Unauthorized } from '../../../core/helpers/http/http_codes'; +import { ConflictError, InvalidParameter, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; +import { ParameterError, InternalServerError } from '../../../core/helpers/http/http_codes'; + + +export class GetInstitutionController { + public usecase: GetInstitutionUsecase; + + constructor(usecase: GetInstitutionUsecase) { + this.usecase = usecase; + } + + public async execute( id: string): Promise<{ [key: string]: any }> { + try { + + + let response = await this.usecase.execute(id); + return response; + + } catch (error) { + if (error instanceof InvalidRequest) { + return new ParameterError(error.message); + } + if (error instanceof UserNotAuthenticated) { + return new Unauthorized(error.message); + } + if (error instanceof ConflictError) { + return new Conflict(error.message); + } + if (error instanceof EntityError) { + return new ParameterError(error.message); + } + if (error instanceof InvalidParameter) { + return new ParameterError(error.message); + } + if (error instanceof MissingParameter) { + return new ParameterError(error.message); + } + return new InternalServerError(error.message); + } + } +} \ No newline at end of file diff --git a/src/modules/get_institution/app/get_institution_presenter.ts b/src/modules/get_institution/app/get_institution_presenter.ts new file mode 100644 index 0000000..11c4686 --- /dev/null +++ b/src/modules/get_institution/app/get_institution_presenter.ts @@ -0,0 +1,16 @@ +import { GetInstitutionController } from "./get_institution_controler"; +import { GetInstitutionUsecase } from "./get_institution_usecase"; + +import { Repository } from "../../../core/repositories/Repository"; +import { HttpRequest } from "../../../core/helpers/http/http_codes"; + +const repository = new Repository({institution_repo: true}); + +const usecase = new GetInstitutionUsecase(repository.InstitutionRepo); +const controller = new GetInstitutionController(usecase); + +export const handler = async (event: any, context: any) => { + let id = event.pathParameters.id; + let response = await controller.execute(id); + return response; +} \ No newline at end of file diff --git a/src/modules/get_institution/app/get_institution_usecase.ts b/src/modules/get_institution/app/get_institution_usecase.ts new file mode 100644 index 0000000..dc8ab40 --- /dev/null +++ b/src/modules/get_institution/app/get_institution_usecase.ts @@ -0,0 +1,21 @@ +import { Institution } from "../../../core/repositories/database/models/Models"; +import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; +import { ConflictError, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; +import { EntityError } from '../../../core/helpers/errors/EntityError'; + +export class GetInstitutionUsecase { + public database_repo: IInstitutionRepo; + + constructor(database_repo: IInstitutionRepo) { + this.database_repo = database_repo; + } + + public async execute(id: string) { + const institution = await this.database_repo.get_institution(id); + if (!institution) { + throw new EntityError("Institution not found"); + } + + return institution; + } +} \ No newline at end of file From 1504460b5b9d446e13e19fccbf96a6a4cd1e402a Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Fri, 19 Apr 2024 21:30:00 +0200 Subject: [PATCH 02/11] Updated accorting to Felipe Requirements --- .../app/get_institution_controler.ts | 21 +++++++--- .../app/get_institution_presenter.ts | 6 +-- .../app/get_institution_usecase.ts | 42 ++++++++++++++----- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/modules/get_institution/app/get_institution_controler.ts b/src/modules/get_institution/app/get_institution_controler.ts index b272988..fb75358 100644 --- a/src/modules/get_institution/app/get_institution_controler.ts +++ b/src/modules/get_institution/app/get_institution_controler.ts @@ -1,7 +1,7 @@ import { GetInstitutionUsecase } from "./get_institution_usecase"; import { EntityError } from '../../../core/helpers/errors/EntityError'; -import { Conflict, OK, Unauthorized } from '../../../core/helpers/http/http_codes'; +import { Conflict, OK, Unauthorized, BadRequest, HttpRequest, HttpResponse} from '../../../core/helpers/http/http_codes'; import { ConflictError, InvalidParameter, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; import { ParameterError, InternalServerError } from '../../../core/helpers/http/http_codes'; @@ -13,12 +13,21 @@ export class GetInstitutionController { this.usecase = usecase; } - public async execute( id: string): Promise<{ [key: string]: any }> { + public async executeexecute(request: HttpRequest): Promise { try { - - - let response = await this.usecase.execute(id); - return response; + if(!request) + { + throw new InvalidRequest(); + + } + if(!request.headers) + { + throw new InvalidRequest("Headers"); + + } + + let response = await this.usecase.execute(request.headers); + return new OK(response, "Institution found"); } catch (error) { if (error instanceof InvalidRequest) { diff --git a/src/modules/get_institution/app/get_institution_presenter.ts b/src/modules/get_institution/app/get_institution_presenter.ts index 11c4686..abe34b6 100644 --- a/src/modules/get_institution/app/get_institution_presenter.ts +++ b/src/modules/get_institution/app/get_institution_presenter.ts @@ -10,7 +10,7 @@ const usecase = new GetInstitutionUsecase(repository.InstitutionRepo); const controller = new GetInstitutionController(usecase); export const handler = async (event: any, context: any) => { - let id = event.pathParameters.id; - let response = await controller.execute(id); - return response; + let request = new HttpRequest(event); + let response = await controller.executeexecute(request); + return response.to_json(); } \ No newline at end of file diff --git a/src/modules/get_institution/app/get_institution_usecase.ts b/src/modules/get_institution/app/get_institution_usecase.ts index dc8ab40..884fbc4 100644 --- a/src/modules/get_institution/app/get_institution_usecase.ts +++ b/src/modules/get_institution/app/get_institution_usecase.ts @@ -2,20 +2,42 @@ import { Institution } from "../../../core/repositories/database/models/Models"; import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; import { ConflictError, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; import { EntityError } from '../../../core/helpers/errors/EntityError'; +import { TokenAuth } from "../../../core/helpers/functions/token_auth"; export class GetInstitutionUsecase { - public database_repo: IInstitutionRepo; - - constructor(database_repo: IInstitutionRepo) { - this.database_repo = database_repo; + public token_auth: TokenAuth; + public institution_repo: IInstitutionRepo; + + constructor(institution_repo: IInstitutionRepo) { + this.institution_repo = institution_repo; + this.token_auth = new TokenAuth(); } - public async execute(id: string) { - const institution = await this.database_repo.get_institution(id); - if (!institution) { - throw new EntityError("Institution not found"); - } + async execute(headers: { [key: string]: any}) { + if(!headers) + { + throw new InvalidRequest("Headers"); + } + if(!headers.authorization) + { + throw new MissingParameter("Authorization"); + } - return institution; + const user_id = await this.token_auth + .decode_token(headers.authorization) + .then((response) => { + return response; + }) + .catch((error) => { + throw new UserNotAuthenticated(); + }); + + + const institution = await this.institution_repo.get_institution(headers.institution_id) + if(!user_id){ + throw new EntityError("User not found"); + } + + return institution?.to_json(); } } \ No newline at end of file From ee4427884e6bbe552c2e943e94d18dc096534454 Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Mon, 29 Apr 2024 19:48:43 +0200 Subject: [PATCH 03/11] Updated to the requests of Felipe! --- .../app/get_institution_controler.ts | 7 +++- .../app/get_institution_presenter.ts | 8 ++-- .../app/get_institution_usecase.ts | 38 +++++++++++++------ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/modules/get_institution/app/get_institution_controler.ts b/src/modules/get_institution/app/get_institution_controler.ts index fb75358..fb9fad1 100644 --- a/src/modules/get_institution/app/get_institution_controler.ts +++ b/src/modules/get_institution/app/get_institution_controler.ts @@ -13,7 +13,7 @@ export class GetInstitutionController { this.usecase = usecase; } - public async executeexecute(request: HttpRequest): Promise { + public async execute(request: HttpRequest): Promise { try { if(!request) { @@ -25,8 +25,11 @@ export class GetInstitutionController { throw new InvalidRequest("Headers"); } + if(!request.body){ + throw new InvalidRequest("Body"); + } - let response = await this.usecase.execute(request.headers); + let response = await this.usecase.execute(request.body.body ,request.headers); return new OK(response, "Institution found"); } catch (error) { diff --git a/src/modules/get_institution/app/get_institution_presenter.ts b/src/modules/get_institution/app/get_institution_presenter.ts index abe34b6..11e66a3 100644 --- a/src/modules/get_institution/app/get_institution_presenter.ts +++ b/src/modules/get_institution/app/get_institution_presenter.ts @@ -4,13 +4,15 @@ import { GetInstitutionUsecase } from "./get_institution_usecase"; import { Repository } from "../../../core/repositories/Repository"; import { HttpRequest } from "../../../core/helpers/http/http_codes"; -const repository = new Repository({institution_repo: true}); +const InstituteRepository = new Repository({institution_repo: true}); +const UserRepository = new Repository({user_repo: true}); -const usecase = new GetInstitutionUsecase(repository.InstitutionRepo); + +const usecase = new GetInstitutionUsecase(InstituteRepository.InstitutionRepo, UserRepository.UserRepo); const controller = new GetInstitutionController(usecase); export const handler = async (event: any, context: any) => { let request = new HttpRequest(event); - let response = await controller.executeexecute(request); + let response = await controller.execute(request); return response.to_json(); } \ No newline at end of file diff --git a/src/modules/get_institution/app/get_institution_usecase.ts b/src/modules/get_institution/app/get_institution_usecase.ts index 884fbc4..d8e2ba9 100644 --- a/src/modules/get_institution/app/get_institution_usecase.ts +++ b/src/modules/get_institution/app/get_institution_usecase.ts @@ -1,19 +1,24 @@ import { Institution } from "../../../core/repositories/database/models/Models"; import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; -import { ConflictError, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; +import { ConflictError, InvalidRequest, MissingParameter, UserNotAuthenticated, UserNotAllowed } from '../../../core/helpers/errors/ModuleError'; import { EntityError } from '../../../core/helpers/errors/EntityError'; import { TokenAuth } from "../../../core/helpers/functions/token_auth"; +import { IUserRepo } from "../../../core/repositories/interfaces/IUserRepo"; +import { UserTypeEnum } from '../../../core/helpers/enums/UserTypeEnum'; + export class GetInstitutionUsecase { public token_auth: TokenAuth; public institution_repo: IInstitutionRepo; + public user_repo: IUserRepo; - constructor(institution_repo: IInstitutionRepo) { + constructor(institution_repo: IInstitutionRepo, user_repo: IUserRepo) { this.institution_repo = institution_repo; this.token_auth = new TokenAuth(); + this.user_repo = user_repo; } - async execute(headers: { [key: string]: any}) { + async execute(institutionData: any, headers: any) { if(!headers) { throw new InvalidRequest("Headers"); @@ -24,18 +29,27 @@ export class GetInstitutionUsecase { } const user_id = await this.token_auth - .decode_token(headers.authorization) - .then((response) => { - return response; - }) - .catch((error) => { + .decode_token(headers.Authorization) + .then((response) => { + return response; + }) + .catch((error) => { + throw new UserNotAuthenticated("Invalid or expired token"); + }); + + const user = await this.user_repo.get_user(user_id); + if (!user) { throw new UserNotAuthenticated(); - }); + } + + if (user.user_type === UserTypeEnum.STUDENT) { + throw new UserNotAllowed(); + } - const institution = await this.institution_repo.get_institution(headers.institution_id) - if(!user_id){ - throw new EntityError("User not found"); + const institution = await this.institution_repo.get_institution(institutionData.institution_id) + if(!institution){ + throw new EntityError("Institution not found"); } return institution?.to_json(); From afbc9a1b78fa885db26281cb3bc220f97671a466 Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Mon, 29 Apr 2024 21:30:24 +0200 Subject: [PATCH 04/11] Resolved the pull request changes --- .../get_institution/app/get_institution_controler.ts | 2 +- .../get_institution/app/get_institution_presenter.ts | 6 ++---- src/modules/get_institution/app/get_institution_usecase.ts | 7 ++++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/modules/get_institution/app/get_institution_controler.ts b/src/modules/get_institution/app/get_institution_controler.ts index fb9fad1..dc54507 100644 --- a/src/modules/get_institution/app/get_institution_controler.ts +++ b/src/modules/get_institution/app/get_institution_controler.ts @@ -29,7 +29,7 @@ export class GetInstitutionController { throw new InvalidRequest("Body"); } - let response = await this.usecase.execute(request.body.body ,request.headers); + let response = await this.usecase.execute(request.body.queryStringParameters, request.headers); return new OK(response, "Institution found"); } catch (error) { diff --git a/src/modules/get_institution/app/get_institution_presenter.ts b/src/modules/get_institution/app/get_institution_presenter.ts index 11e66a3..0128cc2 100644 --- a/src/modules/get_institution/app/get_institution_presenter.ts +++ b/src/modules/get_institution/app/get_institution_presenter.ts @@ -4,11 +4,9 @@ import { GetInstitutionUsecase } from "./get_institution_usecase"; import { Repository } from "../../../core/repositories/Repository"; import { HttpRequest } from "../../../core/helpers/http/http_codes"; -const InstituteRepository = new Repository({institution_repo: true}); -const UserRepository = new Repository({user_repo: true}); +const repository = new Repository({ user_repo: true, institution_repo: true }) - -const usecase = new GetInstitutionUsecase(InstituteRepository.InstitutionRepo, UserRepository.UserRepo); +const usecase = new GetInstitutionUsecase(repository.InstitutionRepo, repository.UserRepo); const controller = new GetInstitutionController(usecase); export const handler = async (event: any, context: any) => { diff --git a/src/modules/get_institution/app/get_institution_usecase.ts b/src/modules/get_institution/app/get_institution_usecase.ts index d8e2ba9..151cd33 100644 --- a/src/modules/get_institution/app/get_institution_usecase.ts +++ b/src/modules/get_institution/app/get_institution_usecase.ts @@ -28,6 +28,11 @@ export class GetInstitutionUsecase { throw new MissingParameter("Authorization"); } + if (!institutionData.institution_id) { + throw new MissingParameter("Institution ID"); + } + + const user_id = await this.token_auth .decode_token(headers.Authorization) .then((response) => { @@ -42,7 +47,7 @@ export class GetInstitutionUsecase { throw new UserNotAuthenticated(); } - if (user.user_type === UserTypeEnum.STUDENT) { + if (!([UserTypeEnum.MODERATOR, UserTypeEnum.ADMIN].includes(user.user_type))) { throw new UserNotAllowed(); } From 9556c4db5c13837f5854181161e669e9b0299638 Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Mon, 29 Apr 2024 22:13:04 +0200 Subject: [PATCH 05/11] testing phase --- .../get_institution/app/get_instition.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/modules/get_institution/app/get_instition.test.ts diff --git a/test/modules/get_institution/app/get_instition.test.ts b/test/modules/get_institution/app/get_instition.test.ts new file mode 100644 index 0000000..7b07b3b --- /dev/null +++ b/test/modules/get_institution/app/get_instition.test.ts @@ -0,0 +1,49 @@ +import { it, describe, expect } from 'vitest'; +import { GetInstitutionController } from '../../../../src/modules/get_institution/app/get_institution_controler'; +import { GetInstitutionUsecase } from '../../../../src/modules/get_institution/app/get_institution_usecase'; +import { Repository } from '../../../../src/core/repositories/Repository'; +import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; +import { InstitutionRepoMock } from '../../../../src/core/repositories/mocks/InstitutionRepoMock'; +import { UserRepoMock } from '../../../../src/core/repositories/mocks/UserRepoMock'; +import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; + +describe("Testing Get Institution Presenter", () => { + it("should return institution data", async () => { + const mockUserRepo = new UserRepoMock(); + const mockInstitutionRepo = new InstitutionRepoMock(); + + + const usecase = new GetInstitutionUsecase( + mockInstitutionRepo, mockUserRepo + ); + const controller = new GetInstitutionController(usecase); + + const mockEvent = { + body: { + property1: "value1", + property2: "value2", + }, + headers: { + Authorization: "Bearer token", + "Content-Type": "application/json", + }, + queryStringParameters: { + param1: "value1", + param2: "value2", + }, + }; + + const request = new HttpRequest(mockEvent); + + // Execute the controller + const response = await controller.execute(request); + + // Assert the response + expect(response.statusCode).toBe(200); + expect(response.body).toEqual({ + id: 1, + name: "Sample Institution", + // Add other expected properties + }); + }); +}); \ No newline at end of file From 14bd9451f0af33702e9b7d8b4154c02d43a383ad Mon Sep 17 00:00:00 2001 From: Yunuz Yunuz <112627091+yunkaa19@users.noreply.github.com> Date: Tue, 30 Apr 2024 03:15:34 +0200 Subject: [PATCH 06/11] Update get_instition.test.ts --- .../get_institution/app/get_instition.test.ts | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/test/modules/get_institution/app/get_instition.test.ts b/test/modules/get_institution/app/get_instition.test.ts index 7b07b3b..35febbc 100644 --- a/test/modules/get_institution/app/get_instition.test.ts +++ b/test/modules/get_institution/app/get_instition.test.ts @@ -5,31 +5,43 @@ import { Repository } from '../../../../src/core/repositories/Repository'; import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; import { InstitutionRepoMock } from '../../../../src/core/repositories/mocks/InstitutionRepoMock'; import { UserRepoMock } from '../../../../src/core/repositories/mocks/UserRepoMock'; -import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; + +import { UserMock } from '../../../../src/core/structure/mocks/UserMock'; + +import {TokenAuth} from '../../../../src/core/helpers/functions/token_auth'; describe("Testing Get Institution Presenter", () => { - it("should return institution data", async () => { - const mockUserRepo = new UserRepoMock(); - const mockInstitutionRepo = new InstitutionRepoMock(); + const mockUserRepo = new UserRepoMock(); + const mockInstitutionRepo = new InstitutionRepoMock(); - const usecase = new GetInstitutionUsecase( - mockInstitutionRepo, mockUserRepo - ); - const controller = new GetInstitutionController(usecase); + const usecase = new GetInstitutionUsecase( + mockInstitutionRepo, mockUserRepo + ); + const controller = new GetInstitutionController(usecase); - const mockEvent = { - body: { - property1: "value1", - property2: "value2", - }, + const mockAdmin = new UserMock().users[0]; + + + it("should return institution data", async () => { + var token = (await new TokenAuth().generate_token(mockAdmin.id)).toString(); + const mockEvent = { headers: { - Authorization: "Bearer token", - "Content-Type": "application/json", + Authorization: + token, }, + body: JSON.stringify({ + id: mockAdmin.id, + name: mockAdmin.name, + email: mockAdmin.email, + user_type: mockAdmin.user_type, + course: mockAdmin.course, + semester_course: mockAdmin.semester_course, + created_at: mockAdmin.created_at, + updated_at: mockAdmin.updated_at + }), queryStringParameters: { - param1: "value1", - param2: "value2", + id: 1, }, }; From 575e8513aef84690c7bd5e0261d0a05b9cd9fa20 Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:06 -0300 Subject: [PATCH 07/11] refactor: Add new error classes for user authentication and not found errors --- src/core/helpers/errors/ModuleError.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/core/helpers/errors/ModuleError.ts b/src/core/helpers/errors/ModuleError.ts index 47726fc..e4d7db9 100644 --- a/src/core/helpers/errors/ModuleError.ts +++ b/src/core/helpers/errors/ModuleError.ts @@ -33,6 +33,22 @@ export class UserNotAuthenticated extends MainError { } } +export class UserNotAllowed extends MainError { + constructor(message?: string) { + if (message) { + super(message); + } else { + super("User not allowed"); + } + } +} + +export class NotfoundError extends MainError { + constructor(message: string) { + super(message); + } +} + export class ConflictError extends MainError { constructor(message: string) { super(message); From e3b68fb29fcf88cda1cf0a980220742b4bf47948 Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:10 -0300 Subject: [PATCH 08/11] feat: Add description field to InstitutionMock The `InstitutionMock` class now includes a `description` field to provide a description for each institution. --- src/core/structure/mocks/InstitutionMock.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/structure/mocks/InstitutionMock.ts b/src/core/structure/mocks/InstitutionMock.ts index ad10f08..321a4da 100644 --- a/src/core/structure/mocks/InstitutionMock.ts +++ b/src/core/structure/mocks/InstitutionMock.ts @@ -9,6 +9,7 @@ export class InstitutionMock { new Institution({ id: "1c92b625-eb2a-4e56-8d9b-99e3c4a93b58", name: "Example University", + description: "This is an example university", email: "example@example.com", country: "CountryName", social_medias: [{ @@ -23,6 +24,7 @@ export class InstitutionMock { new Institution({ id: "2b968d1e-df34-49db-86d7-4c69a75e07c4", name: "Another University", + description: "This is another university", email: "another@example.com", country: "AnotherCountry", social_medias: [{ From 77a0b3e97bd8564b5314119801db6f8e6c5465ac Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:15 -0300 Subject: [PATCH 09/11] chore: Update get_institution_controller.ts Refactor the import statements in `get_institution_controller.ts` to include the new error classes for user authentication and not found errors. --- .../app/get_institution_controler.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/modules/get_institution/app/get_institution_controler.ts b/src/modules/get_institution/app/get_institution_controler.ts index dc54507..d9a3247 100644 --- a/src/modules/get_institution/app/get_institution_controler.ts +++ b/src/modules/get_institution/app/get_institution_controler.ts @@ -1,8 +1,8 @@ import { GetInstitutionUsecase } from "./get_institution_usecase"; import { EntityError } from '../../../core/helpers/errors/EntityError'; -import { Conflict, OK, Unauthorized, BadRequest, HttpRequest, HttpResponse} from '../../../core/helpers/http/http_codes'; -import { ConflictError, InvalidParameter, InvalidRequest, MissingParameter, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; +import { Conflict, OK, Unauthorized, BadRequest, HttpRequest, HttpResponse, Forbidden, NotFound} from '../../../core/helpers/http/http_codes'; +import { ConflictError, InvalidParameter, InvalidRequest, MissingParameter, NotfoundError, UserNotAllowed, UserNotAuthenticated } from '../../../core/helpers/errors/ModuleError'; import { ParameterError, InternalServerError } from '../../../core/helpers/http/http_codes'; @@ -29,7 +29,7 @@ export class GetInstitutionController { throw new InvalidRequest("Body"); } - let response = await this.usecase.execute(request.body.queryStringParameters, request.headers); + let response = await this.usecase.execute(request.body.body, request.headers); return new OK(response, "Institution found"); } catch (error) { @@ -39,8 +39,11 @@ export class GetInstitutionController { if (error instanceof UserNotAuthenticated) { return new Unauthorized(error.message); } - if (error instanceof ConflictError) { - return new Conflict(error.message); + if (error instanceof UserNotAllowed) { + return new Forbidden(error.message); + } + if (error instanceof NotfoundError) { + return new NotFound(error.message); } if (error instanceof EntityError) { return new ParameterError(error.message); From 821f23f539718489d78f829c6a6257dbfa7a0c10 Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:22 -0300 Subject: [PATCH 10/11] refactor: Update get_institution_usecase.ts Refactor the `get_institution_usecase.ts` file to improve code readability and remove unnecessary imports and conditions. --- .../app/get_institution_usecase.ts | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/modules/get_institution/app/get_institution_usecase.ts b/src/modules/get_institution/app/get_institution_usecase.ts index 151cd33..7654c1a 100644 --- a/src/modules/get_institution/app/get_institution_usecase.ts +++ b/src/modules/get_institution/app/get_institution_usecase.ts @@ -1,10 +1,7 @@ -import { Institution } from "../../../core/repositories/database/models/Models"; -import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; -import { ConflictError, InvalidRequest, MissingParameter, UserNotAuthenticated, UserNotAllowed } from '../../../core/helpers/errors/ModuleError'; -import { EntityError } from '../../../core/helpers/errors/EntityError'; import { TokenAuth } from "../../../core/helpers/functions/token_auth"; import { IUserRepo } from "../../../core/repositories/interfaces/IUserRepo"; -import { UserTypeEnum } from '../../../core/helpers/enums/UserTypeEnum'; +import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; +import { InvalidRequest, MissingParameter, UserNotAuthenticated, NotfoundError } from '../../../core/helpers/errors/ModuleError'; export class GetInstitutionUsecase { @@ -19,19 +16,17 @@ export class GetInstitutionUsecase { } async execute(institutionData: any, headers: any) { - if(!headers) - { - throw new InvalidRequest("Headers"); - } - if(!headers.authorization) - { - throw new MissingParameter("Authorization"); - } + if (!headers) { + throw new InvalidRequest("Headers"); + } + if (!headers.Authorization) { + throw new MissingParameter("Authorization"); + } if (!institutionData.institution_id) { throw new MissingParameter("Institution ID"); } - + const user_id = await this.token_auth .decode_token(headers.Authorization) @@ -47,16 +42,11 @@ export class GetInstitutionUsecase { throw new UserNotAuthenticated(); } - if (!([UserTypeEnum.MODERATOR, UserTypeEnum.ADMIN].includes(user.user_type))) { - throw new UserNotAllowed(); - } - - const institution = await this.institution_repo.get_institution(institutionData.institution_id) - if(!institution){ - throw new EntityError("Institution not found"); + if (!institution) { + throw new NotfoundError("Institution not found") } - + return institution?.to_json(); } } \ No newline at end of file From 83bb41c877351e5767840b4565bd3c7b75ea97be Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:27 -0300 Subject: [PATCH 11/11] feat: Refactor Get Institution Presenter Refactor the Get Institution Presenter to improve code readability and remove unnecessary imports and conditions. --- .../get_institution/app/get_instition.test.ts | 69 +++++++------------ 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/test/modules/get_institution/app/get_instition.test.ts b/test/modules/get_institution/app/get_instition.test.ts index 35febbc..406d419 100644 --- a/test/modules/get_institution/app/get_instition.test.ts +++ b/test/modules/get_institution/app/get_instition.test.ts @@ -1,61 +1,42 @@ import { it, describe, expect } from 'vitest'; -import { GetInstitutionController } from '../../../../src/modules/get_institution/app/get_institution_controler'; -import { GetInstitutionUsecase } from '../../../../src/modules/get_institution/app/get_institution_usecase'; -import { Repository } from '../../../../src/core/repositories/Repository'; -import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; -import { InstitutionRepoMock } from '../../../../src/core/repositories/mocks/InstitutionRepoMock'; -import { UserRepoMock } from '../../../../src/core/repositories/mocks/UserRepoMock'; import { UserMock } from '../../../../src/core/structure/mocks/UserMock'; - import {TokenAuth} from '../../../../src/core/helpers/functions/token_auth'; +import { InstitutionMock } from '../../../../src/core/structure/mocks/InstitutionMock'; +import { handler } from '../../../../src/modules/get_institution/app/get_institution_presenter'; describe("Testing Get Institution Presenter", () => { - const mockUserRepo = new UserRepoMock(); - const mockInstitutionRepo = new InstitutionRepoMock(); - - - const usecase = new GetInstitutionUsecase( - mockInstitutionRepo, mockUserRepo - ); - const controller = new GetInstitutionController(usecase); - - const mockAdmin = new UserMock().users[0]; + const user_admin = new UserMock().users[0]; + const mockInstitution = new InstitutionMock(); it("should return institution data", async () => { - var token = (await new TokenAuth().generate_token(mockAdmin.id)).toString(); - const mockEvent = { + var token = (await new TokenAuth().generate_token(user_admin.id)).toString(); + + var response = await handler({ headers: { - Authorization: - token, + Authorization: token }, body: JSON.stringify({ - id: mockAdmin.id, - name: mockAdmin.name, - email: mockAdmin.email, - user_type: mockAdmin.user_type, - course: mockAdmin.course, - semester_course: mockAdmin.semester_course, - created_at: mockAdmin.created_at, - updated_at: mockAdmin.updated_at - }), - queryStringParameters: { - id: 1, - }, - }; + institution_id: mockInstitution.institutions[0].id + }) + }, null); - const request = new HttpRequest(mockEvent); + expect(response.statusCode).toBe(200); + expect(JSON.parse(response.body).message).toBe("Institution found"); + }); - // Execute the controller - const response = await controller.execute(request); + it("should not return institution with invalid token", async () => { + var response = await handler({ + headers: { + Authorization: "invalid_token" + }, + body: JSON.stringify({ + institution_id: mockInstitution.institutions[0].id + }) + }, null); - // Assert the response - expect(response.statusCode).toBe(200); - expect(response.body).toEqual({ - id: 1, - name: "Sample Institution", - // Add other expected properties - }); + expect(response.statusCode).toBe(401); + expect(JSON.parse(response.body).message).toBe("Invalid or expired token"); }); }); \ No newline at end of file