@@ -5,6 +5,8 @@ import { NOTE_EXCEPTIONS } from "../exceptions/NoteExceptions";
55import { extractJwtPayload } from "../auth/jwt/PayloadExtractor" ;
66import { extractToken } from "../utils/TokenExtractor" ;
77import { AddCollaboratorSchema , CreateNoteSchema , DeleteNoteSchema , GetNoteCollaboratorsSchema , GetNoteSchema , GetNotesSchema , RemoveCollaboratorSchema , UpdateNoteSchema } from "../validation/schemas/NoteSchemas" ;
8+ import { isException } from "../utils/guards/ExceptionGuard" ;
9+ import { USER_EXCEPTIONS } from "../exceptions/UserExceptions" ;
810
911
1012export const handleNoteRoutes = (
@@ -20,14 +22,13 @@ export const handleNoteRoutes = (
2022 Body : Omit < NoteWithoutMetadata , "author" > ,
2123 Reply : {
2224 201 : Note ,
23- 503 : typeof NOTE_EXCEPTIONS . ServiceUnavailable ,
25+ 503 : typeof NOTE_EXCEPTIONS . ServiceUnavailable | typeof USER_EXCEPTIONS . ServiceUnavailable
2426 404 : typeof NOTE_EXCEPTIONS . CollaboratorNotFound
2527 }
2628 } > ( "/notes" , {
2729 schema : CreateNoteSchema ,
2830 preHandler : authenticate
2931 } , async ( request , reply ) => {
30- try {
3132 const payload = extractJwtPayload (
3233 extractToken ( request )
3334 )
@@ -36,14 +37,14 @@ export const handleNoteRoutes = (
3637 ...request . body ,
3738 author : payload . login
3839 }
39- console . log ( 123 )
40- const createdNote = await notesService . createNote ( insertData ) as Note
40+ const createdNote = await notesService . createNote ( insertData )
41+ if ( isException ( createdNote ) ) {
42+ reply . code ( createdNote . statusCode ) . send ( createdNote )
43+ return
44+ }
45+
4146 reply . code ( 201 ) . send ( createdNote )
42- } catch ( exception : any ) {
43- reply . code (
44- exception . statusCode
45- ) . send ( exception )
46- }
47+
4748 } )
4849
4950 server . get < {
@@ -63,7 +64,6 @@ export const handleNoteRoutes = (
6364 preHandler : authenticate
6465 } ,
6566 async ( request , reply ) => {
66- try {
6767 const payload = extractJwtPayload (
6868 extractToken ( request )
6969 )
@@ -73,13 +73,14 @@ export const handleNoteRoutes = (
7373 const sort = request . query . sort
7474 const tags = request . query . tags
7575
76- const notes = await notesService . getMyNotes ( payload . login , tags , limit , skip , sort ) as NotePreview [ ]
76+ const notes = await notesService . getMyNotes ( payload . login , tags , limit , skip , sort )
77+ if ( isException ( notes ) ) {
78+ reply . code ( notes . statusCode ) . send ( notes )
79+ return
80+ }
81+
7782 reply . code ( 200 ) . send ( notes )
78- } catch ( exception : any ) {
79- reply . code (
80- exception . statusCode
81- ) . send ( exception )
82- }
83+
8384 } )
8485
8586 server . get < {
@@ -97,7 +98,6 @@ export const handleNoteRoutes = (
9798 schema : GetNotesSchema ,
9899 preHandler : authenticate
99100 } , async ( request , reply ) => {
100- try {
101101 const payload = extractJwtPayload (
102102 extractToken ( request )
103103 )
@@ -107,13 +107,12 @@ export const handleNoteRoutes = (
107107 const sort = request . query . sort
108108 const tags = request . query . tags
109109
110- const notes = await notesService . getCollaboratedNotes ( payload . login , tags , limit , skip , sort ) as NotePreview [ ]
110+ const notes = await notesService . getCollaboratedNotes ( payload . login , tags , limit , skip , sort )
111+ if ( isException ( notes ) ) {
112+ reply . code ( notes . statusCode ) . send ( notes )
113+ return
114+ }
111115 reply . code ( 200 ) . send ( notes )
112- } catch ( exception : any ) {
113- reply . code (
114- exception . statusCode
115- ) . send ( exception )
116- }
117116 } )
118117
119118 server . get < {
@@ -129,20 +128,19 @@ export const handleNoteRoutes = (
129128 preHandler : authenticate
130129 } ,
131130 async ( request , reply ) => {
132- try {
133131 const payload = extractJwtPayload (
134132 extractToken ( request )
135133 )
136134
137135 const id = request . params . id
138136
139- const foundNote = await notesService . getNote ( id , payload . login ) as Note
137+ const foundNote = await notesService . getNote ( id , payload . login )
138+ if ( isException ( foundNote ) ) {
139+ reply . code ( foundNote . statusCode ) . send ( foundNote )
140+ return
141+ }
140142 reply . code ( 200 ) . send ( foundNote )
141- } catch ( exception : any ) {
142- reply . code (
143- exception . statusCode
144- ) . send ( exception )
145- }
143+
146144 } )
147145
148146 server . delete < {
@@ -158,20 +156,19 @@ export const handleNoteRoutes = (
158156 preHandler : authenticate
159157 } ,
160158 async ( request , reply ) => {
161- try {
162159 const payload = extractJwtPayload (
163160 extractToken ( request )
164161 )
165162
166163 const id = request . params . id
167164
168- const state = await notesService . deleteNote ( id , payload . login ) as { success : true }
165+ const state = await notesService . deleteNote ( id , payload . login )
166+ if ( isException ( state ) ) {
167+ reply . code ( state . statusCode ) . send ( state )
168+ return
169+ }
169170 reply . code ( 200 ) . send ( state )
170- } catch ( exception : any ) {
171- reply . code (
172- exception . statusCode
173- ) . send ( exception )
174- }
171+
175172 } )
176173
177174 server . patch < {
@@ -186,21 +183,21 @@ export const handleNoteRoutes = (
186183 schema : UpdateNoteSchema ,
187184 preHandler : authenticate
188185 } , async ( request , reply ) => {
189- try {
190186 const payload = extractJwtPayload (
191187 extractToken ( request )
192188 )
193189
194190 const id = request . params . id
195191 const updateData = request . body
196192
197- const updatedNote = await notesService . updateNote ( id , payload . login , updateData ) as Note
193+ const updatedNote = await notesService . updateNote ( id , payload . login , updateData )
194+ if ( isException ( updatedNote ) ) {
195+ reply . code ( updatedNote . statusCode ) . send ( updatedNote )
196+ return
197+ }
198+
198199 reply . code ( 200 ) . send ( updatedNote )
199- } catch ( exception : any ) {
200- reply . code (
201- exception . statusCode
202- ) . send ( exception )
203- }
200+
204201 } )
205202
206203 server . get < {
@@ -214,19 +211,17 @@ export const handleNoteRoutes = (
214211 schema : GetNoteCollaboratorsSchema ,
215212 preHandler : authenticate
216213 } , async ( request , reply ) => {
217- try {
218214 const payload = extractJwtPayload (
219215 extractToken ( request )
220216 )
221217 const id = request . params . id
222- const collaborators = await notesService . getCollaborators ( id , payload . login ) as NoteCollaborators
223-
218+ const collaborators = await notesService . getCollaborators ( id , payload . login )
219+ if ( isException ( collaborators ) ) {
220+ reply . code ( collaborators . statusCode ) . send ( collaborators )
221+ return
222+ }
224223 reply . code ( 200 ) . send ( collaborators )
225- } catch ( exception : any ) {
226- reply . code (
227- exception . statusCode
228- ) . send ( exception )
229- }
224+
230225 } )
231226
232227 server . put < {
@@ -245,7 +240,6 @@ export const handleNoteRoutes = (
245240 schema : AddCollaboratorSchema ,
246241 preHandler : authenticate
247242 } , async ( request , reply ) => {
248- try {
249243 const payload = extractJwtPayload (
250244 extractToken ( request )
251245 )
@@ -257,14 +251,13 @@ export const handleNoteRoutes = (
257251 id ,
258252 payload . login ,
259253 collaboratorLogin
260- ) as { success : true }
254+ )
255+ if ( isException ( state ) ) {
256+ reply . code ( state . statusCode ) . send ( state )
257+ return
258+ }
261259
262260 reply . code ( 201 ) . send ( state )
263- } catch ( exception : any ) {
264- reply . code (
265- exception . statusCode
266- ) . send ( exception )
267- }
268261 } )
269262
270263 server . delete < {
@@ -282,7 +275,6 @@ export const handleNoteRoutes = (
282275 schema : RemoveCollaboratorSchema ,
283276 preHandler : authenticate
284277 } , async ( request , reply ) => {
285- try {
286278 const payload = extractJwtPayload (
287279 extractToken ( request )
288280 )
@@ -294,13 +286,13 @@ export const handleNoteRoutes = (
294286 id ,
295287 payload . login ,
296288 collaboratorLogin
297- ) as { success : true }
289+ )
290+ if ( isException ( state ) ) {
291+ reply . code ( state . statusCode ) . send ( state )
292+ return
293+ }
298294
299295 reply . code ( 200 ) . send ( state )
300- } catch ( exception : any ) {
301- reply . code (
302- exception . statusCode
303- ) . send ( exception )
304- }
296+
305297 } )
306298}
0 commit comments