Skip to content

Commit

Permalink
Merge pull request #17 from Instituto-Maua-de-Tecnologia/Get-Institut…
Browse files Browse the repository at this point in the history
…ion-Function

Get institution routes
  • Loading branch information
FelipeCarillo authored May 10, 2024
2 parents 95df376 + 83bb41c commit 9fa1a69
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/structure/mocks/InstitutionMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
description: "Example University is a great university",
country: "CountryName",
Expand All @@ -24,6 +25,7 @@ export class InstitutionMock {
new Institution({
id: "2b968d1e-df34-49db-86d7-4c69a75e07c4",
name: "Another University",
description: "This is another university",
email: "another@example.com",
description: "Another University is a great university in another country",
country: "AnotherCountry",
Expand Down
60 changes: 60 additions & 0 deletions src/modules/get_institution/app/get_institution_controler.ts
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 src/modules/get_institution/app/get_institution_presenter.ts
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 src/modules/get_institution/app/get_institution_usecase.ts
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();
}
}
42 changes: 42 additions & 0 deletions test/modules/get_institution/app/get_instition.test.ts
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");
});
});

0 comments on commit 9fa1a69

Please sign in to comment.