Skip to content
2 changes: 1 addition & 1 deletion src/application/controllers/pets/load-pet-by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class LoadPetByIdController implements Controller {
const { petId } = httpRequest.params
const result = await this.loadPet.loadById({ petId })
if (!result.isSuccess) {
return badRequest(result.error as Error)
return badRequest(result.error)
}
return success(result.data)
} catch (error) {
Expand Down
26 changes: 13 additions & 13 deletions src/application/controllers/pets/pet-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ export class PetRegistryController implements Controller {
image
})
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return create({
id: result.data?.id,
guardian: result.data?.guardian,
specie: result.data?.specie,
specieAlias: result.data?.specieAlias,
petName: result.data?.petName,
gender: result.data?.gender,
breed: result.data?.breed,
breedAlias: result.data?.breedAlias,
size: result.data?.size,
castrated: result.data?.castrated,
dateOfBirth: result.data?.dateOfBirth,
image: result.data?.image
id: result.data.id,
guardian: result.data.guardian,
specie: result.data.specie,
specieAlias: result.data.specieAlias,
petName: result.data.petName,
gender: result.data.gender,
breed: result.data.breed,
breedAlias: result.data.breedAlias,
size: result.data.size,
castrated: result.data.castrated,
dateOfBirth: result.data.dateOfBirth,
image: result.data.image
})
} catch (error) {
return serverError(error as Error)
Expand Down
2 changes: 1 addition & 1 deletion src/application/controllers/pets/update-pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class UpdatePetController implements Controller {
}
const result = await this.updatePet.update(updateData)
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return success({
id: result.data?.id,
Expand Down
2 changes: 1 addition & 1 deletion src/application/controllers/scheduler/create-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class CreateSchedulerController implements Controller {
pets
})
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return create({
id: result.data?.id,
Expand Down
2 changes: 1 addition & 1 deletion src/application/controllers/scheduler/tag/add-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AddTagController implements Controller {
const guardianId = httpRequest.userId as string
const result = await this.addTag.add({ guardianId, name, color })
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return create({
id: result.data?.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class DeleteTagByIdController implements Controller {
const { tagId } = httpRequest.params
const result = await this.deleteTag.deleteById(tagId)
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return success({
message: 'Tag deleted',
Expand Down
2 changes: 1 addition & 1 deletion src/application/controllers/scheduler/tag/update-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class UpdateTagController implements Controller {
const { name } = httpRequest.body
const result = await this.updateTag.update({ id: tagId, name })
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return success(result)
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/application/controllers/settings/update-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class UpdateSettingsController implements Controller {
const updateData = { ...httpRequest.body, guardianId }
const result = await this.updateSettings.update(updateData)
if (!result.isSuccess) {
return notAcceptable(result.error as Error)
return notAcceptable(result.error)
}
return success({
guardianId: result.data?.guardianId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LoadNextTaskByPetIdAndTagIdController implements Controller {
})

if (!result.isSuccess) {
return badRequest(result.error as Error)
return badRequest(result.error)
}

return success({
Expand Down
19 changes: 7 additions & 12 deletions src/data/protocols/service/events-generator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type ResultResponse } from '@/domain/types/result'
import { type AddEventRepository, type LoadEventByDateRepository } from '../db'
import { type AddManyEventsRepository } from '../db/event/add-many-events-repository'

Expand All @@ -15,19 +16,13 @@ export namespace EventsGenerator {
daily: boolean | undefined
}

export type Result = {
isSuccess: boolean
error?: Error
data?: Array<{
schedulerId: string
start: Date
end: Date
}> | {
schedulerId: string
start: Date
end: Date
}
type Data = {
schedulerId: string
start: Date
end: Date
}

export type Result = ResultResponse<Data[] | Data>
export type Dependencies = {
eventRepository: AddEventRepository & LoadEventByDateRepository & AddManyEventsRepository
}
Expand Down
51 changes: 25 additions & 26 deletions src/data/use-cases/pet/db-add-pet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { NotAcceptableError } from '@/application/errors'
import { type FileStorage, type AddPetRepository, type LoadGuardianByIdRepository, type UpdatePetRepository } from '@/data/protocols'
import {
type PetGender,
type Guardian,
type Specie,
type Breed,
type Size
} from '@/domain/models'
import {
type AddPet,
type AppointPet
Expand Down Expand Up @@ -58,44 +51,50 @@ export class DbAddPet implements AddPet {
const { petName, gender, dateOfBirth } = petData
const pet = await this.petRepository.add({
guardianId: guardian.id,
specieId: appointResult.data?.specie.id as string,
specieAlias: appointResult.data?.specieAlias,
specieId: appointResult.data.specie.id,
specieAlias: appointResult.data.specieAlias,
petName,
gender,
breedId: appointResult.data?.breed.id as string,
breedAlias: appointResult.data?.breedAlias as string,
sizeId: appointResult.data?.size.id as string,
castrated: appointResult.data?.castrated as boolean,
breedId: appointResult.data.breed.id,
breedAlias: appointResult.data.breedAlias,
sizeId: appointResult.data.size.id,
castrated: appointResult.data.castrated,
dateOfBirth
})
if (!pet) {
return {
isSuccess: false,
error: new NotAcceptableError('petData')
}
}

let imageUrl: string = ''
if (petData.image) {
imageUrl = await this.fileStorage.save({ file: petData.image, fileName: `images/pet-${pet?.id as string}` })
imageUrl = await this.fileStorage.save({ file: petData.image, fileName: `images/pet-${pet.id}` })
}

if (imageUrl) {
await this.petRepository.update({
guardianId: guardian.id,
petId: pet?.id as string,
petId: pet.id,
image: imageUrl
})
}

return {
isSuccess: true,
data: {
id: pet?.id as string,
guardian: pet?.guardian as Guardian & { id: string },
specie: pet?.specie as Specie & { id: string },
specieAlias: pet?.specieAlias,
petName: pet?.petName as string,
gender: pet?.gender as PetGender,
breed: pet?.breed as Breed & { id: string },
breedAlias: pet?.breedAlias as string,
size: pet?.size as Size & { id: string },
castrated: pet?.castrated as boolean,
dateOfBirth: pet?.dateOfBirth as Date,
id: pet.id,
guardian: pet.guardian,
specie: pet.specie,
specieAlias: pet.specieAlias,
petName: pet.petName,
gender: pet.gender,
breed: pet.breed,
breedAlias: pet.breedAlias,
size: pet.size,
castrated: pet.castrated,
dateOfBirth: pet.dateOfBirth,
image: imageUrl || this.defaultImageUrl
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/data/use-cases/pet/db-appoint-pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class DbAppointPet implements AppointPet {
data: {
specie: specieResult.specie,
specieAlias: specieResult.specieAlias,
breed: breedResult.data?.breed as Breed & { id: string },
breedAlias: breedResult.data?.breedAlias as string,
size: sizeResult.data?.size as Size & { id: string },
breed: breedResult.data.breed,
breedAlias: breedResult.data.breedAlias,
size: sizeResult.data.size,
castrated
}
}
Expand Down Expand Up @@ -146,17 +146,23 @@ type SpecieResult = {
}

type BreedResult = {
error?: Error
data?: {
error: Error
data?: never
} | {
error?: never
data: {
breed: Breed & { id: string }
breedAlias: string | undefined
breedAlias: string
specieId: string
}
}

type SizeResult = {
error?: Error
data?: {
error: Error
data?: never
} | {
error?: never
data: {
size: Size & { id: string }
specieId: string
}
Expand Down
56 changes: 27 additions & 29 deletions src/data/use-cases/pet/db-update-pet.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { NotAcceptableError } from '@/application/errors'
import { type UpdatePetRepository, type LoadGuardianByIdRepository, type LoadPetByIdRepository } from '@/data/protocols'
import {
type PetGender,
type Guardian,
type Specie,
type Breed,
type Size
} from '@/domain/models'
import {
type UpdatePet,
type AppointPet
} from '@/domain/use-cases'
import { type PetGender } from '@/domain/models'
import { type UpdatePet, type AppointPet } from '@/domain/use-cases'

export class DbUpdatePet implements UpdatePet {
private readonly guardianRepository: LoadGuardianByIdRepository
Expand Down Expand Up @@ -57,31 +48,38 @@ export class DbUpdatePet implements UpdatePet {
const petUpdateResult = await this.petRepository.update({
guardianId: guardian.id,
petId: pet.id,
specieId: appointResult.data?.specie.id as string,
specieAlias: appointResult.data?.specieAlias,
specieId: appointResult.data.specie.id,
specieAlias: appointResult.data.specieAlias,
petName: petData.petName ? petData.petName : pet.petName,
gender: petData.gender ? petData.gender : pet.gender as PetGender,
breedId: appointResult.data?.breed.id as string,
breedAlias: appointResult.data?.breedAlias as string,
sizeId: appointResult.data?.size.id as string,
castrated: appointResult.data?.castrated as boolean,
breedId: appointResult.data.breed.id,
breedAlias: appointResult.data.breedAlias,
sizeId: appointResult.data.size.id,
castrated: appointResult.data.castrated,
dateOfBirth: petData.dateOfBirth ? petData.dateOfBirth : pet.dateOfBirth
})
if (!petUpdateResult) {
return {
isSuccess: false,
error: new NotAcceptableError('update error')
}
}

return {
isSuccess: true,
data: {
id: petUpdateResult?.id as string,
guardian: petUpdateResult?.guardian as Guardian & { id: string },
specie: petUpdateResult?.specie as Specie & { id: string },
specieAlias: petUpdateResult?.specieAlias,
petName: petUpdateResult?.petName as string,
gender: petUpdateResult?.gender as PetGender,
breed: petUpdateResult?.breed as Breed & { id: string },
breedAlias: petUpdateResult?.breedAlias as string,
size: petUpdateResult?.size as Size & { id: string },
castrated: petUpdateResult?.castrated as boolean,
dateOfBirth: petUpdateResult?.dateOfBirth as Date,
image: petUpdateResult?.image as string
id: petUpdateResult.id,
guardian: petUpdateResult.guardian,
specie: petUpdateResult.specie,
specieAlias: petUpdateResult.specieAlias,
petName: petUpdateResult.petName,
gender: petUpdateResult.gender,
breed: petUpdateResult.breed,
breedAlias: petUpdateResult.breedAlias,
size: petUpdateResult.size,
castrated: petUpdateResult.castrated,
dateOfBirth: petUpdateResult.dateOfBirth,
image: petUpdateResult.image
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/data/use-cases/scheduler/tag/db-delete-tag-by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class DbDeleteTagById implements DeleteTagById {
}
}
return {
isSuccess: true
isSuccess: true,
data: undefined
}
}
}
11 changes: 11 additions & 0 deletions src/domain/types/page-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type ResultResponse } from './result'

type PageInfo = {
page: number
limit: number
totalPages: number
}

type PageData<T, K extends string> = PageInfo & { [key in K]: T[] }

export type PageResult<T, K extends string> = ResultResponse<PageData<T, K>>
13 changes: 13 additions & 0 deletions src/domain/types/result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Success<T> = {
isSuccess: true
data: T
error?: never

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talvez remover o error do type success seja melhor

}

type Failure = {
isSuccess: false
error: Error
data?: never

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talvez remover o data do type Failure seja melhor.

}

export type ResultResponse<T> = Success<T> | Failure
Loading