-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Instituto-Maua-de-Tecnologia/Get-Institut…
…ion-Function Get institution routes
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/modules/get_institution/app/get_institution_controler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { GetInstitutionUsecase } from "./get_institution_usecase"; | ||
|
||
import { EntityError } from '../../../core/helpers/errors/EntityError'; | ||
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'; | ||
|
||
|
||
export class GetInstitutionController { | ||
public usecase: GetInstitutionUsecase; | ||
|
||
constructor(usecase: GetInstitutionUsecase) { | ||
this.usecase = usecase; | ||
} | ||
|
||
public async execute(request: HttpRequest): Promise<HttpResponse> { | ||
try { | ||
if(!request) | ||
{ | ||
throw new InvalidRequest(); | ||
|
||
} | ||
if(!request.headers) | ||
{ | ||
throw new InvalidRequest("Headers"); | ||
|
||
} | ||
if(!request.body){ | ||
throw new InvalidRequest("Body"); | ||
} | ||
|
||
let response = await this.usecase.execute(request.body.body, request.headers); | ||
return new OK(response, "Institution found"); | ||
|
||
} catch (error) { | ||
if (error instanceof InvalidRequest) { | ||
return new ParameterError(error.message); | ||
} | ||
if (error instanceof UserNotAuthenticated) { | ||
return new Unauthorized(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); | ||
} | ||
if (error instanceof InvalidParameter) { | ||
return new ParameterError(error.message); | ||
} | ||
if (error instanceof MissingParameter) { | ||
return new ParameterError(error.message); | ||
} | ||
return new InternalServerError(error.message); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/modules/get_institution/app/get_institution_presenter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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({ user_repo: true, institution_repo: true }) | ||
|
||
const usecase = new GetInstitutionUsecase(repository.InstitutionRepo, repository.UserRepo); | ||
const controller = new GetInstitutionController(usecase); | ||
|
||
export const handler = async (event: any, context: any) => { | ||
let request = new HttpRequest(event); | ||
let response = await controller.execute(request); | ||
return response.to_json(); | ||
} |
52 changes: 52 additions & 0 deletions
52
src/modules/get_institution/app/get_institution_usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { TokenAuth } from "../../../core/helpers/functions/token_auth"; | ||
import { IUserRepo } from "../../../core/repositories/interfaces/IUserRepo"; | ||
import { IInstitutionRepo } from "../../../core/repositories/interfaces/IInstitutionRepo"; | ||
import { InvalidRequest, MissingParameter, UserNotAuthenticated, NotfoundError } from '../../../core/helpers/errors/ModuleError'; | ||
|
||
|
||
export class GetInstitutionUsecase { | ||
public token_auth: TokenAuth; | ||
public institution_repo: IInstitutionRepo; | ||
public user_repo: IUserRepo; | ||
|
||
constructor(institution_repo: IInstitutionRepo, user_repo: IUserRepo) { | ||
this.institution_repo = institution_repo; | ||
this.token_auth = new TokenAuth(); | ||
this.user_repo = user_repo; | ||
} | ||
|
||
async execute(institutionData: any, headers: any) { | ||
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) | ||
.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(); | ||
} | ||
|
||
const institution = await this.institution_repo.get_institution(institutionData.institution_id) | ||
if (!institution) { | ||
throw new NotfoundError("Institution not found") | ||
} | ||
|
||
return institution?.to_json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { it, describe, expect } from 'vitest'; | ||
|
||
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 user_admin = new UserMock().users[0]; | ||
const mockInstitution = new InstitutionMock(); | ||
|
||
|
||
it("should return institution data", async () => { | ||
var token = (await new TokenAuth().generate_token(user_admin.id)).toString(); | ||
|
||
var response = await handler({ | ||
headers: { | ||
Authorization: token | ||
}, | ||
body: JSON.stringify({ | ||
institution_id: mockInstitution.institutions[0].id | ||
}) | ||
}, null); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(JSON.parse(response.body).message).toBe("Institution found"); | ||
}); | ||
|
||
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); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(JSON.parse(response.body).message).toBe("Invalid or expired token"); | ||
}); | ||
}); |