Skip to content

Commit

Permalink
Merge pull request #248 from Black-Dot-2024/develop
Browse files Browse the repository at this point in the history
Staging Release
  • Loading branch information
dembA7 authored Jun 6, 2024
2 parents 0551189 + abb9dec commit a0dc825
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
65 changes: 41 additions & 24 deletions src/api/controllers/company.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ const reportSchema = z.object({
});

/**
* Finds all companies
* Retrieves a unique company by its ID.
*
* @param {Request} req
* @param {Response} res
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @returns {Promise<void>}
* @throws {Error}
*/

/**
* Finds all companies
*
* @param {Request} req
* @param {Response} res
* @returns {Promise<void>}
* @throws {Error}
* @throws {Error} - If an error occurs while retrieving the company.
*/

async function getUnique(req: Request, res: Response) {
Expand All @@ -38,10 +29,14 @@ async function getUnique(req: Request, res: Response) {
}

/**
* Receives a request to update a client and validates de data before sending it to the service
* @param req
* @param res
* Updates a client with the provided data.
*
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @returns {Promise<void>}
* @throws {Error} - If an error occurs while updating the client.
*/

async function updateClient(req: Request, res: Response) {
try {
const id = req.params.id;
Expand All @@ -57,9 +52,12 @@ async function updateClient(req: Request, res: Response) {
}

/**
* Receives a request to update a client and validates de data before sending it to the service
* @param req
* @param res
* Retrieves all companies from the database.
*
* @param _ - The request object.
* @param res - The response object.
* @returns {Promise<void>}
* @throws {Error} - If an error occurs while retrieving the companies.
*/

async function getAll(_: Request, res: Response) {
Expand All @@ -72,12 +70,12 @@ async function getAll(_: Request, res: Response) {
}

/**
* Finds all companies
* Creates a company in the database.
*
* @param {Request} req
* @param {Response} res
* @param req - The request object.
* @param res - The response object.
* @returns {Promise<void>}
* @throws {Error}
* @throws {Error} - If an error occurs while creating the company.
*/

async function create(req: Request, res: Response) {
Expand All @@ -95,4 +93,23 @@ async function create(req: Request, res: Response) {
} else res.status(500).json({ error: error.message });
}
}
export const CompanyController = { getUnique, getAll, create, updateClient };

/**
* Retrieves all companies that are not archived.
*
* @param _ - The request object.
* @param res - The response object.
* @returns {Promise<void>}
* @throws {Error} - If an error occurs while retrieving the companies.
*/

async function getUnarchived(_: Request, res: Response) {
try {
const data = await CompanyService.findUnarchived();
res.status(200).json(data);
} catch (error: any) {
res.status(500).json({ message: error.message });
}
}

export const CompanyController = { getUnique, getAll, create, updateClient, getUnarchived };
1 change: 1 addition & 0 deletions src/api/routes/company.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router = Router();

router.use(checkAuthRole([SupportedRoles.ACCOUNTING, SupportedRoles.LEGAL, SupportedRoles.ADMIN]));
router.get('/', CompanyController.getAll);
router.get('/unarchived', CompanyController.getUnarchived);
router.get('/:id', CompanyController.getUnique);
router.post('/new', CompanyController.create);
router.put('/:id', CompanyController.updateClient);
Expand Down
8 changes: 7 additions & 1 deletion src/api/validators/company.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const companySchema = z.object({
.max(15, { message: 'Landlinephone number must be between 10 and 15 digits long' })
.optional()
.nullable(),
archived: z.boolean().optional().nullable(),
archived: z
.boolean()
.optional()
.nullable()
.refine(value => value === false, {
message: 'Cannot create a project for an archived company.',
}),
constitutionDate: z.string().optional().nullable(),
rfc: zodValidRfc.optional().nullable(),
taxResidence: z
Expand Down
8 changes: 7 additions & 1 deletion src/api/validators/zod.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { z } from 'zod';

export const zodValidUuid = z.string().uuid({ message: 'Provided UUID is not valid' });
export const zodValidEmail = z
.union([z.string().email({ message: 'Provided email is not valid.' }), z.string().length(0)])
.union([
z
.string()
.email({ message: 'Provided email is not valid.' })
.max(70, { message: 'Email must be at most 70 characters long.' }),
z.string().length(0),
])
.optional();
export const zodValidString = z
.string()
Expand Down
17 changes: 16 additions & 1 deletion src/core/app/services/company.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,19 @@ async function archiveClient(id: string): Promise<CompanyEntity> {
}
}

export const CompanyService = { findAll, findById, update, create, archiveClient };
/**
* @brief Retrieves all companies that are not archived.
*
* @returns {Promise<CompanyEntity[]>}
* @throws {Error} - If an error occurs while retrieving the companies.
*/
async function findUnarchived(): Promise<CompanyEntity[]> {
try {
const data = await CompanyRepository.findUnarchived();
return data;
} catch (error: any) {
throw new Error(error.message);
}
}

export const CompanyService = { findAll, findById, update, create, archiveClient, findUnarchived };
49 changes: 48 additions & 1 deletion src/core/infra/repositories/company.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ async function archiveClient(id: string, archived: boolean): Promise<CompanyEnti
}
}

/**
* @brief updates a company in the database
*
* @param company
* @returns {Promise<CompanyEntity>}
* @throws {Error} - If an error occurs while updating the company
* @throws {Error} - If the email is already registered
* @throws {Error} - If the RFC is already registered
*/

async function update(company: CompanyEntity): Promise<CompanyEntity> {
try {
const updatedCompany = await Prisma.company.update({
Expand Down Expand Up @@ -172,4 +182,41 @@ async function update(company: CompanyEntity): Promise<CompanyEntity> {
}
}

export const CompanyRepository = { findAll, findById, update, create, archiveClient, getArchivedStatus };
/**
* @brief retrieves all companies that are not archived
*
* @returns {Promise<CompanyEntity[]>}
* @throws {Error} - If an error occurs while retrieving the companies
*/

async function findUnarchived(): Promise<CompanyEntity[]> {
try {
const data: Array<any> = await Prisma.$queryRaw`
SELECT c.*,
COUNT(DISTINCT p.id) as total_projects,
SUM(CASE WHEN p.is_chargeable THEN t.worked_hours ELSE 0 END) AS chargeable_hours,
SUM(CASE WHEN p.is_chargeable AND p.area='Accounting' THEN t.worked_hours ELSE 0 END) AS accounting_hours,
SUM(CASE WHEN p.is_chargeable AND p.area='Legal' THEN t.worked_hours ELSE 0 END) AS legal_hours
FROM company c
LEFT JOIN project p ON c.id = p.id_company
LEFT JOIN task t ON p.id = t.id_project
WHERE c.archived = false
GROUP BY c.id
ORDER BY c.name ASC;
`;

return data.map(mapCompanyEntityFromDbModel);
} catch (error: any) {
throw new Error(`${RESOURCE_NAME} repository error: ${error.message}`);
}
}

export const CompanyRepository = {
findAll,
findById,
update,
create,
archiveClient,
getArchivedStatus,
findUnarchived,
};

0 comments on commit a0dc825

Please sign in to comment.