From 1739a24ff17e8b9352e16543d454f7d81547ceeb Mon Sep 17 00:00:00 2001 From: FaelSantoss Date: Thu, 28 Nov 2024 11:44:52 -0300 Subject: [PATCH] =?UTF-8?q?[back]=20=E2=99=BB=EF=B8=8F=20refactor:=20refac?= =?UTF-8?q?tor=20register=20location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../locations/register-location-controller.ts | 4 +++- .../locations/delete-locations-use-case.ts | 2 +- .../locations/register-location-use-case.ts | 14 ++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/server/src/api/controllers/locations/register-location-controller.ts b/apps/server/src/api/controllers/locations/register-location-controller.ts index 4c9a4235..e50eed12 100644 --- a/apps/server/src/api/controllers/locations/register-location-controller.ts +++ b/apps/server/src/api/controllers/locations/register-location-controller.ts @@ -8,7 +8,9 @@ export class RegisterLocationController { async handle(http: IHttp) { const locationDto = http.getBody() const useCase = new RegisterLocationUseCase(locationsRepository) - await useCase.execute({ locationDto }) + const {companyId} = await http.getUser() + + await useCase.execute({ locationDto, companyId }) return http.send(null, HTTP_STATUS_CODE.created) } diff --git a/packages/core/src/use-cases/locations/delete-locations-use-case.ts b/packages/core/src/use-cases/locations/delete-locations-use-case.ts index cde30f84..64a73a6b 100644 --- a/packages/core/src/use-cases/locations/delete-locations-use-case.ts +++ b/packages/core/src/use-cases/locations/delete-locations-use-case.ts @@ -16,7 +16,7 @@ export class DeleteLocationsUseCase { for (const locationId of locationsId) { const existingLocation = await this.locationsRepository.findById(locationId); if (!existingLocation) { - throw new NotFoundError(`Local com ID ${locationId} não encontrado`); + throw new NotFoundError("Local não encontrado"); } } await this.locationsRepository.deleteMany(locationsId); diff --git a/packages/core/src/use-cases/locations/register-location-use-case.ts b/packages/core/src/use-cases/locations/register-location-use-case.ts index 563bfd18..57847773 100644 --- a/packages/core/src/use-cases/locations/register-location-use-case.ts +++ b/packages/core/src/use-cases/locations/register-location-use-case.ts @@ -6,6 +6,7 @@ import { ConflictError } from "../../errors"; type Request = { locationDto: LocationDto + companyId: string } export class RegisterLocationUseCase { @@ -15,12 +16,13 @@ export class RegisterLocationUseCase { this.locationsRepository = locationsRepository } - async execute({ locationDto }: Request) { - const existingLocation = await this.locationsRepository.findByName(locationDto.name) - if (existingLocation !== null) { - throw new ConflictError('Nome já em uso') - } - + async execute({ locationDto, companyId }: Request) { + if (locationDto.name) { + const locationName = await this.locationsRepository.findByName(locationDto.name) + if (locationName !== null && locationName.dto.companyId === companyId) { + throw new ConflictError('Nome já em uso por outra localização no sistema') + } + } const location = Location.create(locationDto) await this.locationsRepository.add(location) }