11const Boom = require ( '@hapi/boom' ) ;
2+ const { Op } = require ( 'sequelize' ) ;
23const { v4 : uuidv4 } = require ( 'uuid' ) ;
34
45const dewormingRepository = require ( './deworming.repository' ) ;
@@ -9,49 +10,84 @@ const formatDeworming = (dewormer) => {
910 id : dewormer . id ,
1011 dewormer : dewormer . dewormer ,
1112 description : dewormer . description || null ,
12- animalId : dewormer . animal ? dewormer . animal . code : null ,
13+ animal : dewormer . animal ? dewormer . animal . code : null ,
1314 registeredAt :
1415 dewormer . registeredAt . toISOString ( ) . split ( 'T' ) [ 0 ] ||
1516 dewormer . registeredAt ,
1617 } ;
1718} ;
1819
19- const getAllDeworming = async ( userId , filters ) => {
20+ const buildFilters = ( query ) => {
21+ const { dewormer, animalId } = query ;
22+
23+ return {
24+ ...( dewormer && { dewormer : { [ Op . iLike ] : `%${ dewormer } %` } } ) ,
25+ ...( animalId && { animalId } ) ,
26+ } ;
27+ } ;
28+
29+ const getAllDeworming = async ( userId , query ) => {
30+ if ( ! query || typeof query !== 'object' ) {
31+ throw Boom . badRequest ( 'query was not provided' ) ;
32+ }
33+
34+ const filters = buildFilters ( query ) ;
35+
2036 const deworming = await dewormingRepository . findAllDewormings (
2137 userId ,
2238 filters ,
2339 ) ;
2440
41+ if ( deworming ?. length === 0 ) return [ ] ;
42+
2543 return deworming . map ( ( d ) => formatDeworming ( d ) ) ;
2644} ;
2745
2846const getDeworming = async ( userId , dewormingId ) => {
47+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
48+ if ( ! dewormingId ) throw Boom . badRequest ( 'dewormingId was not provided' ) ;
49+
2950 const deworming = await dewormingRepository . findOne ( userId , dewormingId ) ;
3051 if ( ! deworming ?. id ) throw Boom . notFound ( 'Deworming does not exist' ) ;
52+
3153 return formatDeworming ( deworming ) ;
3254} ;
3355
3456const createDeworming = async ( userId , dewormingData ) => {
57+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
58+ if ( ! dewormingData ?. dewormer || ! dewormingData ?. animalId ) {
59+ throw Boom . badRequest ( 'dewormer was not provided' ) ;
60+ }
61+
3562 const animal = await animalRepository . findOne ( userId , dewormingData . animalId ) ;
36- if ( ! animal ?. id )
63+ if ( ! animal ?. id ) {
3764 throw Boom . notFound ( 'Animal does not exist or does not belong to the user' ) ;
65+ }
3866
3967 const deworming = {
4068 id : uuidv4 ( ) ,
4169 dewormer : dewormingData . dewormer ,
42- description : dewormingData . description || null ,
70+ description : dewormingData ? .description || null ,
4371 animalId : dewormingData . animalId ,
4472 registeredAt :
4573 dewormingData . registeredAt || new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
4674 } ;
4775
4876 const newdeworming = await dewormingRepository . create ( deworming ) ;
49- if ( ! newdeworming ?. id )
77+ if ( ! newdeworming ?. id ) {
5078 throw Boom . badRequest ( 'Something went wrong creating the deworming' ) ;
79+ }
80+
5181 return newdeworming ;
5282} ;
5383
5484const updateDeworming = async ( userId , dewormingId , dewormingData ) => {
85+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
86+ if ( ! dewormingId ) throw Boom . badRequest ( 'dewormingId was not provided' ) ;
87+ if ( ! dewormingData ?. dewormer && ! dewormingData . description ) {
88+ throw Boom . badRequest ( 'dewormingData was not provided' ) ;
89+ }
90+
5591 const deworming = await dewormingRepository . findOne ( userId , dewormingId ) ;
5692 if ( ! deworming ?. id ) throw Boom . conflict ( 'deworming does not exists' ) ;
5793
@@ -73,10 +109,19 @@ const updateDeworming = async (userId, dewormingId, dewormingData) => {
73109} ;
74110
75111const deleteDeworming = async ( userId , dewormingId ) => {
112+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
113+ if ( ! dewormingId ) throw Boom . badRequest ( 'dewormingId was not provided' ) ;
114+
76115 const deworming = await dewormingRepository . findOne ( userId , dewormingId ) ;
77116 if ( ! deworming ?. id ) throw Boom . conflict ( 'deworming does not exists' ) ;
78117
79- return dewormingRepository . destroy ( dewormingId ) ;
118+ const affectedRows = await dewormingRepository . destroy ( dewormingId ) ;
119+
120+ if ( affectedRows === 0 ) {
121+ throw Boom . badRequest ( 'Something went wrong deleting the deworming' ) ;
122+ }
123+
124+ return affectedRows ;
80125} ;
81126
82127module . exports = {
0 commit comments