@@ -18,20 +18,56 @@ const formatData = (animalData) => {
1818 } ;
1919} ;
2020
21- const getAllAnimals = async ( filters ) => {
21+ const buildFilters = ( query ) => {
22+ const { code, livestockType, animalType, breed, sex, animalId, userId } =
23+ query ;
24+
25+ return {
26+ userId,
27+ ...( animalId && { id : animalId } ) ,
28+ ...( code && { code } ) ,
29+ ...( livestockType && { livestockType } ) ,
30+ ...( animalType && { animalType } ) ,
31+ ...( breed && { breed } ) ,
32+ ...( sex && { sex } ) ,
33+ } ;
34+ } ;
35+
36+ const getAllAnimals = async ( query ) => {
37+ if ( ! query || typeof query !== 'object' ) {
38+ throw Boom . badRequest ( 'query was not provided' ) ;
39+ }
40+
41+ const filters = buildFilters ( query ) ;
42+
2243 const animals = await animalRepository . findAllAnimals ( filters ) ;
23- if ( ! animals ?. length ) return [ ] ;
44+ if ( ! animals ?. length === 0 ) return [ ] ;
2445
2546 return animals . map ( ( animal ) => formatData ( animal ) ) ;
2647} ;
2748
2849const getAnimal = async ( userId , animalId ) => {
50+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
51+ if ( ! animalId ) throw Boom . badRequest ( 'animalId was not provided' ) ;
52+
2953 const animal = await animalRepository . findOne ( userId , animalId ) ;
3054 if ( ! animal ?. id ) throw Boom . notFound ( 'Animal does not exist' ) ;
55+
3156 return formatData ( animal ) ;
3257} ;
3358
3459const createAnimal = async ( animalData ) => {
60+ if (
61+ ! animalData ?. livestockType ||
62+ ! animalData ?. animalType ||
63+ ! animalData ?. code ||
64+ ! animalData ?. breed ||
65+ ! animalData ?. sex ||
66+ ! animalData ?. userId
67+ ) {
68+ throw Boom . badRequest ( 'Some values were not provided' ) ;
69+ }
70+
3571 const animal = {
3672 id : uuidv4 ( ) ,
3773 livestockType : animalData . livestockType ,
@@ -56,6 +92,10 @@ const updateAnimal = async (userId, animalId, animalData) => {
5692 const animal = await animalRepository . findOne ( userId , animalId ) ;
5793 if ( ! animal ?. id ) throw Boom . conflict ( 'Animal does not exists' ) ;
5894
95+ if ( Object . entries ( animalData ) . length === 0 ) {
96+ throw Boom . badRequest ( 'No data was provided to update the animal' ) ;
97+ }
98+
5999 const formattedAnimalData = {
60100 ...( animalData . livestockType && {
61101 livestockType : animalData . livestockType ,
@@ -67,6 +107,11 @@ const updateAnimal = async (userId, animalId, animalData) => {
67107 ...( animalData . motherId && { motherId : animalData . motherId } ) ,
68108 ...( animalData . fatherId && { fatherId : animalData . fatherId } ) ,
69109 } ;
110+
111+ if ( Object . keys ( formattedAnimalData ) . length === 0 ) {
112+ throw Boom . badRequest ( 'There is no data to update' ) ;
113+ }
114+
70115 const [ updatedRows , [ updatedAnimal ] ] = await animalRepository . update (
71116 animalId ,
72117 formattedAnimalData ,
@@ -78,7 +123,18 @@ const updateAnimal = async (userId, animalId, animalData) => {
78123} ;
79124
80125const deleteAnimal = async ( userId , animalId ) => {
81- return animalRepository . destroy ( userId , animalId ) ;
126+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
127+ if ( ! animalId ) throw Boom . badRequest ( 'animalId was not provided' ) ;
128+
129+ const deletedRows = await animalRepository . destroy ( userId , animalId ) ;
130+
131+ if ( deletedRows === 0 ) {
132+ throw Boom . badRequest (
133+ 'Something went wrong deleting the animal or the animal does not exist' ,
134+ ) ;
135+ }
136+
137+ return deletedRows ;
82138} ;
83139
84140module . exports = {
0 commit comments