Skip to content

Commit

Permalink
Merge pull request #243 from Black-Dot-2024/develop
Browse files Browse the repository at this point in the history
release(staging)
  • Loading branch information
DHurtado714-itesm authored Jun 4, 2024
2 parents f9cfa4e + ed58978 commit 0551189
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/api/controllers/company.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async function updateClient(req: Request, res: Response) {

res.status(200).json(updatedCompany);
} catch (error: any) {
res.status(500).json({ error: error.message });
if (error instanceof z.ZodError) {
res.status(500).json({ error: error.errors[0].message });
} else res.status(500).json({ error: error.message });
}
}

Expand Down
38 changes: 21 additions & 17 deletions src/core/app/services/company.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,29 @@ async function findAll(): Promise<CompanyEntity[]> {
* @returns {Promise<CompanyEntity>} a promise that resolves to the updated company entity
*/
async function update(body: UpdateCompanyBody): Promise<CompanyEntity> {
const company = await CompanyRepository.findById(body.id);
try {
const company = await CompanyRepository.findById(body.id);

if (!company) throw new NotFoundError('Company not found');
if (!company) throw new NotFoundError('Company not found');

return await CompanyRepository.update({
id: company.id,
name: body.name ?? company.name,
email: body.email,
phoneNumber: body.phoneNumber,
landlinePhone: body.landlinePhone,
archived: body.archived,
constitutionDate: body.constitutionDate,
rfc: body.rfc,
taxResidence: body.taxResidence,
idCompanyDirectContact: company.idCompanyDirectContact,
idForm: company.idForm,
createdAt: company.createdAt,
updatedAt: new Date(),
});
return await CompanyRepository.update({
id: company.id,
name: body.name ?? company.name,
email: body.email,
phoneNumber: body.phoneNumber,
landlinePhone: body.landlinePhone,
archived: body.archived,
constitutionDate: body.constitutionDate,
rfc: body.rfc,
taxResidence: body.taxResidence,
idCompanyDirectContact: company.idCompanyDirectContact,
idForm: company.idForm,
createdAt: company.createdAt,
updatedAt: new Date(),
});
} catch (error: any) {
throw new Error(error.message);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/infra/repositories/company.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ async function update(company: CompanyEntity): Promise<CompanyEntity> {
} catch (error: any) {
if (error.code == 'P2002' && (error.meta?.target as string[])[0] === 'email')
throw new Error('Email already registered.');
if (error.meta.target[0] == 'rfc') throw new Error('RFC already registered.');
throw new Error(`${RESOURCE_NAME} repository error: ${error.message}`);
}
}
Expand Down

0 comments on commit 0551189

Please sign in to comment.