Skip to content

Commit

Permalink
fix(projects): remove findAll projects and replace for findAllByRole
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarnica1 committed May 11, 2024
1 parent 369f7c9 commit c22209b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 109 deletions.
15 changes: 0 additions & 15 deletions src/api/controllers/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,6 @@ async function getProjectsClient(req: Request, res: Response) {
}
}

/**
* A function that calls the service to get all projects in the database.
* @param req HTTP Request
* @param res Server response
*/
async function getAllProjects(req: Request, res: Response) {
try {
const data = await ProjectService.getAllProjects();
res.status(200).json({ data: data });
} catch (error: any) {
res.status(500).json({ message: error.message });
}
}

/**
* Retrieves all projects from a certain department
* @param req An HTTP Request
Expand Down Expand Up @@ -173,7 +159,6 @@ async function updateProjectStatus(req: Request, res: Response) {
export const ProjectController = {
getReportData,
createProject,
getAllProjects,
getProjectsClient,
getProjectById,
updateProject,
Expand Down
35 changes: 1 addition & 34 deletions src/core/app/services/__tests__/company.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { randomUUID } from 'crypto';
import sinon from 'sinon';
import { SupportedDepartments } from '../../../../utils/enums';
import { CompanyRepository } from '../../../infra/repositories/company.repository';
import { ProjectRepository } from '../../../infra/repositories/project.repository';
import { CompanyService } from '../company.service';

chai.use(chaiAsPromised);

describe('CompanyService', () => {
let findAllCompaniesStub: sinon.SinonStub;
let findAllProjectsStub: sinon.SinonStub;
let updateCompanyStub: sinon.SinonStub;
let findCompanyByIdStub: sinon.SinonStub;
let archiveClientdStub: sinon.SinonStub;
let getArchivedStatusStub: sinon.SinonStub;

beforeEach(() => {
findAllProjectsStub = sinon.stub(ProjectRepository, 'findAll');
findAllCompaniesStub = sinon.stub(CompanyRepository, 'findAll');
updateCompanyStub = sinon.stub(CompanyRepository, 'update');
findCompanyByIdStub = sinon.stub(CompanyRepository, 'findById');
Expand All @@ -33,7 +29,6 @@ describe('CompanyService', () => {

it('should return an array of all companies', async () => {
const mockData = prepareMockData();
findAllProjectsStub.resolves(mockData.existingProjects);
findAllCompaniesStub.resolves(mockData.existingCompanies);

const res = await CompanyService.findAll();
Expand All @@ -44,7 +39,6 @@ describe('CompanyService', () => {

it('should match the name of the companies', async () => {
const mockData = prepareMockData();
findAllProjectsStub.resolves(mockData.existingProjects);
findAllCompaniesStub.resolves(mockData.existingCompanies);

const res = await CompanyService.findAll();
Expand Down Expand Up @@ -150,34 +144,7 @@ function prepareMockData() {
},
];

const existingProjects = [
{
id: randomUUID(),
name: 'Zeitgeist P1',
description: 'Desc',
status: 'Not started',
startDate: new Date(),
totalHours: 10,
isChargeable: true,
area: SupportedDepartments.LEGAL,
createdAt: new Date(),
idCompany: idCompany1,
},
{
id: randomUUID(),
name: 'Zeitgeist P2',
description: 'Desc',
status: 'Not started',
startDate: new Date(),
totalHours: 5,
isChargeable: true,
area: SupportedDepartments.ACCOUNTING,
createdAt: new Date(),
idCompany: idCompany1,
},
];

return { existingCompanies, existingProjects };
return { existingCompanies };
}

function prepareSingleFakeCompany() {
Expand Down
34 changes: 32 additions & 2 deletions src/core/app/services/__tests__/home.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { randomUUID } from 'crypto';
import sinon from 'sinon';
import { SupportedRoles } from '../../../../utils/enums';
import { CompanyRepository } from '../../../infra/repositories/company.repository';
import { EmployeeTaskRepository } from '../../../infra/repositories/employee-task.repository';
import { EmployeeRepository } from '../../../infra/repositories/employee.repository';
import { ProjectRepository } from '../../../infra/repositories/project.repository';
import { RoleRepository } from '../../../infra/repositories/role.repository';
import { TaskRepository } from '../../../infra/repositories/tasks.repository';
import { HomeService } from '../home.service';

Expand All @@ -16,22 +19,45 @@ describe('HomeService', () => {
let findTaskByEmployeeIdStub: sinon.SinonStub;
let findAllTasksStub: sinon.SinonStub;
let findAllCompaniesStub: sinon.SinonStub;
let findEmployeeByEmail: sinon.SinonStub;
let findEmployeeById: sinon.SinonStub;
let findRoleById: sinon.SinonStub;

beforeEach(() => {
findAllProjectsStub = sinon.stub(ProjectRepository, 'findAll');
findAllProjectsStub = sinon.stub(ProjectRepository, 'findAllByRole');
findTaskByEmployeeIdStub = sinon.stub(EmployeeTaskRepository, 'findByEmployeeId');
findAllTasksStub = sinon.stub(TaskRepository, 'findAll');
findAllCompaniesStub = sinon.stub(CompanyRepository, 'findAll');
findEmployeeByEmail = sinon.stub(EmployeeRepository, 'findByEmail');
findEmployeeById = sinon.stub(EmployeeRepository, 'findById');
findRoleById = sinon.stub(RoleRepository, 'findById');
});

afterEach(() => {
sinon.restore();
});

describe('getHomeInfo', () => {
it('should return the projects an employee has assigned and teh companies of those projects', async () => {
it('should return the projects an employee has assigned and the companies of those projects', async () => {
const employeeId = randomUUID();

const accountingRole = randomUUID();

const role = {
title: SupportedRoles.ACCOUNTING,
createdAr: new Date(),
};

const employee = {
id: employeeId,
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
imageUrl: 'http://example.com/john.jpg',
createdAt: new Date(),
idRole: accountingRole,
};

const companyId = randomUUID();
const companyId2 = randomUUID();
const existingCompanies = [
Expand Down Expand Up @@ -122,6 +148,10 @@ describe('HomeService', () => {
},
];

findEmployeeByEmail.resolves(employee);
findEmployeeById.resolves(employee);
findRoleById.resolves(role);

findAllProjectsStub.resolves(existingProjects);
findAllCompaniesStub.resolves(existingCompanies);
findAllTasksStub.resolves(existingTasks);
Expand Down
45 changes: 38 additions & 7 deletions src/core/app/services/__tests__/project.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@ import { faker } from '@faker-js/faker';
import { expect } from 'chai';
import { randomUUID } from 'crypto';
import { default as Sinon, default as sinon } from 'sinon';
import { ProjectCategory, ProjectPeriodicity, ProjectStatus, SupportedDepartments } from '../../../../utils/enums';
import {
ProjectCategory,
ProjectPeriodicity,
ProjectStatus,
SupportedDepartments,
SupportedRoles,
} from '../../../../utils/enums';
import { ProjectEntity } from '../../../domain/entities/project.entity';
import { CompanyRepository } from '../../../infra/repositories/company.repository';
import { EmployeeRepository } from '../../../infra/repositories/employee.repository';
import { ProjectRepository } from '../../../infra/repositories/project.repository';
import { RoleRepository } from '../../../infra/repositories/role.repository';
import { CompanyService } from '../company.service';
import { ProjectService } from '../project.service';

describe('ProjectService', () => {
let findProjectByIdStub: Sinon.SinonStub;
let createProject: sinon.SinonStub;
let findAllStub: sinon.SinonStub;
let findAllByRoleStub: sinon.SinonStub;
let findCompanyByIdStub: Sinon.SinonStub;
let findProjectsByClientId: Sinon.SinonStub;
let updateProjectStub: sinon.SinonStub;
let updateProjectStatusStub: sinon.SinonStub;
let findEmployeeByEmail: sinon.SinonStub;
let findRoleById: sinon.SinonStub;

beforeEach(() => {
createProject = sinon.stub(ProjectRepository, 'createProject');
findAllStub = sinon.stub(ProjectRepository, 'findAll');
findAllByRoleStub = sinon.stub(ProjectRepository, 'findAllByRole');
findProjectByIdStub = sinon.stub(ProjectRepository, 'findById');
findProjectsByClientId = sinon.stub(ProjectRepository, 'findProjetsByClientId');
findCompanyByIdStub = sinon.stub(CompanyRepository, 'findById');
findEmployeeByEmail = sinon.stub(EmployeeRepository, 'findByEmail');
findRoleById = sinon.stub(RoleRepository, 'findById');
});

afterEach(() => {
Expand Down Expand Up @@ -97,6 +109,23 @@ describe('ProjectService', () => {

describe('getAllProjects', () => {
it('should return all projects', async () => {
const accountingRole = randomUUID();

const role = {
title: SupportedRoles.ACCOUNTING,
createdAr: new Date(),
};

const employee = {
id: randomUUID(),
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
imageUrl: 'http://example.com/john.jpg',
createdAt: new Date(),
idRole: accountingRole,
};

const projects = [
{
id: randomUUID(),
Expand All @@ -110,7 +139,7 @@ describe('ProjectService', () => {
endDate: new Date('2023-12-01T00:00:00.000Z'),
periodicity: '1 week',
isChargeable: true,
area: 'Client',
area: SupportedDepartments.ACCOUNTING,
createdAt: new Date('2024-04-19T01:23:49.555Z'),
idCompany: randomUUID(),
},
Expand All @@ -126,15 +155,17 @@ describe('ProjectService', () => {
endDate: new Date('2023-12-01T00:00:00.000Z'),
periodicity: '1 week',
isChargeable: true,
area: 'Client',
area: SupportedDepartments.ACCOUNTING,
createdAt: new Date('2024-04-19T01:23:49.555Z'),
idCompany: randomUUID(),
},
];

findAllStub.resolves(projects);
findEmployeeByEmail.resolves(employee);
findRoleById.resolves(role);
findAllByRoleStub.resolves(projects);

const getProjects = await ProjectService.getAllProjects();
const getProjects = await ProjectService.getDepartmentProjects(employee.email);

expect(getProjects).eql(projects);
});
Expand Down
8 changes: 7 additions & 1 deletion src/core/app/services/home.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CompanyRepository } from '../../infra/repositories/company.repository';
import { EmployeeTaskRepository } from '../../infra/repositories/employee-task.repository';
import { EmployeeRepository } from '../../infra/repositories/employee.repository';
import { ProjectRepository } from '../../infra/repositories/project.repository';
import { RoleRepository } from '../../infra/repositories/role.repository';
import { TaskRepository } from '../../infra/repositories/tasks.repository';
import { Home } from '../interfaces/home.interface';

Expand All @@ -14,7 +16,10 @@ import { Home } from '../interfaces/home.interface';
*/
async function getMyInfo(idEmployee: string): Promise<Home> {
try {
const projects = await ProjectRepository.findAll();
const employee = await EmployeeRepository.findById(idEmployee);
const role = await RoleRepository.findById(employee.idRole);

const projects = await ProjectRepository.findAllByRole(role.title);
const employeeTask = await EmployeeTaskRepository.findByEmployeeId(idEmployee);
const tasks = await TaskRepository.findAll();
const companies = await CompanyRepository.findAll();
Expand Down Expand Up @@ -48,6 +53,7 @@ async function getMyInfo(idEmployee: string): Promise<Home> {

return homeInfo;
} catch (error: unknown) {
console.log(error);
throw new Error('An unexpected error occurred');
}
}
Expand Down
20 changes: 7 additions & 13 deletions src/core/app/services/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,19 @@ async function createProject(data: CreateProjectData): Promise<ProjectEntity> {
return newProject;
}

/**
* Gets all projects from the database
*
* @returns {Promise<ProjectEntity[]>} - An array of project entities
*/
async function getAllProjects(): Promise<ProjectEntity[]> {
return await ProjectRepository.findAll();
}

/**
* Retrieves all project from a certain role given an email
* @param email the email from the requester
* @returns the projects only from a specific role
*/
async function getDepartmentProjects(email: string): Promise<ProjectEntity[]> {
const role = await EmployeeService.findRoleByEmail(email);

return await ProjectRepository.findAllByRole(role);
try {
const role = await EmployeeService.findRoleByEmail(email);
return await ProjectRepository.findAllByRole(role);
} catch (error) {
console.log(error);
throw new Error('An unexpected error occured');
}
}

/**
Expand Down Expand Up @@ -148,7 +143,6 @@ async function updateProjectStatus(projectId: string, newStatus: ProjectStatus):

export const ProjectService = {
createProject,
getAllProjects,
findProjectsClient,
getProjectById,
updateProject,
Expand Down
1 change: 1 addition & 0 deletions src/core/infra/repositories/employee.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function findById(id: string): Promise<EmployeeEntity> {

return mapEmployeeEntityFromDbModel(data);
} catch (error: unknown) {
console.log(error);
throw new Error('Employee repository error');
}
}
Expand Down
Loading

0 comments on commit c22209b

Please sign in to comment.