Skip to content

Commit

Permalink
Merge pull request #259 from CtrI-Alt-Del/updateLocation
Browse files Browse the repository at this point in the history
Update location
  • Loading branch information
JohnPetros authored Nov 30, 2024
2 parents c3c372e + 6027063 commit 2aff670
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class RegisterLocationController {
async handle(http: IHttp) {
const locationDto = http.getBody<LocationDto>()
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class DeleteLocationsUseCase {
}

async execute({ locationsId }: Request): Promise<void> {
await this.locationsRepository.deleteById(locationsId)
for (const locationId of locationsId) {
const existingLocation = await this.locationsRepository.findById(locationId);
if (!existingLocation) {
throw new NotFoundError("Local não encontrado");
}
}
await this.locationsRepository.deleteMany(locationsId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ConflictError } from "../../errors";

type Request = {
locationDto: LocationDto
companyId: string
}

export class RegisterLocationUseCase {
Expand All @@ -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)
}
Expand Down

0 comments on commit 2aff670

Please sign in to comment.