Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/projects #169

Merged
merged 9 commits into from
May 11, 2024
19 changes: 2 additions & 17 deletions src/api/controllers/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function createProject(req: Request, res: Response) {
startDate: data.startDate,
});
res.status(201).json(newProject);
} catch (error: unknown) {
res.status(400).json({ message: error });
} catch (error: any) {
res.status(400).json({ message: error.message });
}
}

Expand Down Expand Up @@ -98,20 +98,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 @@ -177,7 +163,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
61 changes: 54 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 @@ -74,6 +86,20 @@ describe('ProjectService', () => {
it('should create a project', async () => {
const uuid = randomUUID();
const clientUuid = randomUUID();

const company = {
id: clientUuid,
name: 'Zeitgeist',
email: '[email protected]',
phoneNumber: '1234567890',
landlinePhone: '0987654321',
archived: false,
createdAt: new Date(),
updatedAt: null,
idCompanyDirectContact: null,
idForm: null,
};

const projectData = {
id: uuid,
name: 'Nuevo Proyecto de Desarrollo',
Expand All @@ -91,6 +117,8 @@ describe('ProjectService', () => {
createdAt: new Date('2024-04-19T01:23:49.555Z'),
idCompany: clientUuid,
};

findCompanyByIdStub.resolves(company);
createProject.resolves(projectData);
const newProject = await ProjectService.createProject(projectData);
expect(newProject).to.equal(projectData);
Expand All @@ -99,6 +127,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 @@ -113,7 +158,7 @@ describe('ProjectService', () => {
periodicity: '1 week',
isChargeable: true,
isArchived: false,
area: 'Client',
area: SupportedDepartments.ACCOUNTING,
createdAt: new Date('2024-04-19T01:23:49.555Z'),
idCompany: randomUUID(),
},
Expand All @@ -130,15 +175,17 @@ describe('ProjectService', () => {
periodicity: '1 week',
isChargeable: true,
isArchived: false,
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
Loading
Loading