@@ -136,27 +136,69 @@ export default {
136136 const { data : shareData , error : sharedError } = await supabase
137137 . schema ( "timetable" )
138138 . from ( "shared" )
139- . select (
140- "id, calendar_id, owner_id, shared_id, timetables!inner(id, user_id, timetable_title, semester, favorite)" ,
141- )
139+ . select ( "id, calendar_id, owner_id, shared_id, timetables!inner(*)" )
142140 . eq ( "shared_id" , user_id ) ;
143141
144142 if ( sharedError ) {
145143 return res . status ( 400 ) . json ( { error : sharedError . message } ) ;
146144 }
147145
148- if ( ! shareData || shareData . length === 0 ) {
149- return res
150- . status ( 404 )
151- . json ( { error : "No shared timetables found for this user" } ) ;
152- }
153-
154146 return res . status ( 200 ) . json ( shareData ) ;
155147 } catch ( error ) {
156148 return res . status ( 500 ) . send ( { error } ) ;
157149 }
158150 } ) ,
159151
152+ /**
153+ * Get all restrictions from a shared timetable id
154+ * @route GET /api/shared/restrictions/:calendar_id
155+ */
156+ getSharedRestrictions : asyncHandler (
157+ async ( req : Request , res : Response ) => {
158+ try {
159+ const { user_id, calendar_id } = req . query ;
160+
161+ if ( ! user_id || ! calendar_id ) {
162+ return res . status ( 400 ) . json ( {
163+ error : "User ID and Calendar ID are required" ,
164+ } ) ;
165+ }
166+
167+ //Retrieve users allowed to access the timetable
168+ const { data : timetableData , error : timetableError } = await supabase
169+ . schema ( "timetable" )
170+ . from ( "timetables" )
171+ . select ( "*" )
172+ . eq ( "id" , calendar_id )
173+ . eq ( "user_id" , user_id )
174+ . maybeSingle ( ) ;
175+
176+ if ( timetableError )
177+ return res . status ( 400 ) . json ( { error : timetableError . message } ) ;
178+
179+ //Validate timetable validity:
180+ if ( ! timetableData || timetableData . length === 0 ) {
181+ return res . status ( 404 ) . json ( { error : "Calendar id not found" } ) ;
182+ }
183+
184+ const { data : restrictionData , error : restrictionError } = await supabase
185+ . schema ( "timetable" )
186+ . from ( "restriction" )
187+ . select ( )
188+ . eq ( "user_id" , user_id )
189+ . eq ( "calendar_id" , calendar_id ) ;
190+
191+ if ( restrictionError ) {
192+ return res . status ( 400 ) . json ( { error : restrictionError . message } ) ;
193+ }
194+
195+ return res . status ( 200 ) . json ( restrictionData ) ;
196+ } catch ( error ) {
197+ return res . status ( 500 ) . send ( { error } ) ;
198+ }
199+ }
200+ ) ,
201+
160202 /**
161203 * Delete all shared record for a timetable as the timetable's owner
162204 * @route DELETE /api/shared/owner/:id?
@@ -387,15 +429,20 @@ export default {
387429 deleteShare : asyncHandler ( async ( req : Request , res : Response ) => {
388430 try {
389431 const shared_id = ( req as any ) . user . id ;
390- const { id } = req . params ;
391- const { calendar_id } = req . body ;
432+ const { calendar_id, owner_id } = req . body ;
433+
434+ if ( ! calendar_id || ! owner_id ) {
435+ return res . status ( 400 ) . json ( {
436+ error : "Calendar ID and Owner ID are required" ,
437+ } ) ;
438+ }
392439
393440 const { data : existingTimetable , error : existingTimetableError } =
394441 await supabase
395442 . schema ( "timetable" )
396443 . from ( "shared" )
397444 . select ( "*" )
398- . eq ( "id " , id )
445+ . eq ( "owner_id " , owner_id )
399446 . eq ( "calendar_id" , calendar_id )
400447 . eq ( "shared_id" , shared_id ) ;
401448
@@ -412,15 +459,15 @@ export default {
412459 . schema ( "timetable" )
413460 . from ( "shared" )
414461 . delete ( )
415- . eq ( "id " , id )
462+ . eq ( "owner_id " , owner_id )
416463 . eq ( "calendar_id" , calendar_id )
417464 . eq ( "shared_id" , shared_id ) ;
418465 if ( deleteError ) {
419466 return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
420467 }
421468
422469 return res . status ( 200 ) . json ( {
423- message : `Sharing record: ${ id } of calendar: ${ calendar_id } deleted successfully` ,
470+ message : `Sharing record with owner_id of ${ owner_id } and calendar_id of ${ calendar_id } deleted successfully` ,
424471 } ) ;
425472 } catch ( error ) {
426473 return res . status ( 500 ) . send ( { error } ) ;
0 commit comments