11const Boom = require ( '@hapi/boom' ) ;
2+ const { Op } = require ( 'sequelize' ) ;
23const { v4 : uuidv4 } = require ( 'uuid' ) ;
34
45const noteRepository = require ( './notes.repository' ) ;
56
67const formatNotes = ( note ) => {
78 return {
89 id : note . id ,
9- vaccine : note . title ,
10+ title : note . title ,
1011 description : note . description || null ,
1112 userId : note . user ? note . user . id : null ,
1213 user : note . user ? note . user . name : null ,
1314 createdAt : note . createdAt . toISOString ( ) . split ( 'T' ) [ 0 ] || note . createdAt ,
1415 } ;
1516} ;
1617
17- const getAllNotes = async ( filters ) => {
18+ const buildFilters = ( query ) => {
19+ const { title, userId } = query ;
20+
21+ return {
22+ ...( title && { title : { [ Op . iLike ] : `%${ title } %` } } ) ,
23+ ...( userId && { userId } ) ,
24+ } ;
25+ } ;
26+
27+ const getAllNotes = async ( query ) => {
28+ if ( ! query || typeof query !== 'object' ) {
29+ throw Boom . badRequest ( 'query was not provided' ) ;
30+ }
31+
32+ if ( ! query ?. userId ) {
33+ throw Boom . badData ( 'userId was not provided' ) ;
34+ }
35+
36+ const filters = buildFilters ( query ) ;
37+
1838 const notes = await noteRepository . findAllNotes ( filters ) ;
1939 if ( ! notes ?. length === 0 ) return [ ] ;
40+
2041 return notes . map ( ( note ) => formatNotes ( note ) ) ;
2142} ;
2243
2344const getNote = async ( userId , noteId ) => {
45+ if ( ! userId ) throw Boom . notFound ( 'userId was not provided' ) ;
46+ if ( ! noteId ) throw Boom . notFound ( 'noteId was not provided' ) ;
47+
2448 const note = await noteRepository . findOne ( userId , noteId ) ;
2549 if ( ! note ?. id ) throw Boom . notFound ( 'Note does not exist' ) ;
50+
2651 return formatNotes ( note ) ;
2752} ;
2853
2954const createNote = async ( noteData ) => {
55+ if ( ! noteData ?. userId ) {
56+ throw Boom . badRequest ( 'userId was not provided' ) ;
57+ }
58+ if ( ! noteData ?. title && ! noteData ?. description ) {
59+ throw Boom . badRequest ( 'noteData was not provided' ) ;
60+ }
61+
3062 const note = {
3163 id : uuidv4 ( ) ,
3264 title : noteData . title ,
33- description : noteData . description || null ,
65+ description : noteData ? .description || null ,
3466 userId : noteData . userId ,
3567 createdAt : noteData . createdAt || new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
3668 } ;
3769
3870 const newnote = await noteRepository . create ( note ) ;
39- if ( ! newnote ?. id )
71+ if ( ! newnote ?. id ) {
4072 throw Boom . badRequest ( 'Something went wrong creating the note' ) ;
73+ }
74+
4175 return newnote ;
4276} ;
4377
4478const updateNote = async ( userId , noteId , noteData ) => {
79+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
80+ if ( ! noteId ) throw Boom . badRequest ( 'noteId was not provided' ) ;
81+ if ( ! noteData ?. title && ! noteData ?. description ) {
82+ throw Boom . badRequest ( 'noteData was not provided' ) ;
83+ }
84+
4585 const note = await getNote ( userId , noteId ) ;
4686 if ( ! note ?. id ) throw Boom . notFound ( 'Note does not exist' ) ;
4787
@@ -54,17 +94,28 @@ const updateNote = async (userId, noteId, noteData) => {
5494 noteId ,
5595 formattedNoteData ,
5696 ) ;
97+
5798 if ( updatedRows === 0 ) {
5899 throw Boom . badRequest ( 'Something went wrong creating the note' ) ;
59100 }
101+
60102 return updatedNote ;
61103} ;
62104
63105const deleteNote = async ( userId , noteId ) => {
106+ if ( ! userId ) throw Boom . badRequest ( 'userId was not provided' ) ;
107+ if ( ! noteId ) throw Boom . badRequest ( 'noteId was not provided' ) ;
108+
64109 const note = await getNote ( userId , noteId ) ;
65110 if ( ! note ?. id ) throw Boom . conflict ( 'note does not exists' ) ;
66111
67- return noteRepository . destroy ( noteId ) ;
112+ const affectedRows = await noteRepository . destroy ( noteId ) ;
113+
114+ if ( affectedRows === 0 ) {
115+ throw Boom . badRequest ( 'Something went wrong deleting the note' ) ;
116+ }
117+
118+ return affectedRows ;
68119} ;
69120
70121module . exports = {
0 commit comments