@@ -136,22 +136,62 @@ 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" } ) ;
146+ return res . status ( 200 ) . json ( shareData ) ;
147+ } catch ( error ) {
148+ return res . status ( 500 ) . send ( { error } ) ;
149+ }
150+ } ) ,
151+
152+ /**
153+ * Get all restrictions from a shared timetable id
154+ * @route GET /api/shared/restrictions/:calendar_id
155+ */
156+ getSharedRestrictions : asyncHandler ( async ( req : Request , res : Response ) => {
157+ try {
158+ const { user_id, calendar_id } = req . query ;
159+
160+ if ( ! user_id || ! calendar_id ) {
161+ return res . status ( 400 ) . json ( {
162+ error : "User ID and Calendar ID are required" ,
163+ } ) ;
152164 }
153165
154- return res . status ( 200 ) . json ( shareData ) ;
166+ //Retrieve users allowed to access the timetable
167+ const { data : timetableData , error : timetableError } = await supabase
168+ . schema ( "timetable" )
169+ . from ( "timetables" )
170+ . select ( "*" )
171+ . eq ( "id" , calendar_id )
172+ . eq ( "user_id" , user_id )
173+ . maybeSingle ( ) ;
174+
175+ if ( timetableError )
176+ return res . status ( 400 ) . json ( { error : timetableError . message } ) ;
177+
178+ //Validate timetable validity:
179+ if ( ! timetableData || timetableData . length === 0 ) {
180+ return res . status ( 404 ) . json ( { error : "Calendar id not found" } ) ;
181+ }
182+
183+ const { data : restrictionData , error : restrictionError } = await supabase
184+ . schema ( "timetable" )
185+ . from ( "restriction" )
186+ . select ( )
187+ . eq ( "user_id" , user_id )
188+ . eq ( "calendar_id" , calendar_id ) ;
189+
190+ if ( restrictionError ) {
191+ return res . status ( 400 ) . json ( { error : restrictionError . message } ) ;
192+ }
193+
194+ return res . status ( 200 ) . json ( restrictionData ) ;
155195 } catch ( error ) {
156196 return res . status ( 500 ) . send ( { error } ) ;
157197 }
@@ -387,15 +427,20 @@ export default {
387427 deleteShare : asyncHandler ( async ( req : Request , res : Response ) => {
388428 try {
389429 const shared_id = ( req as any ) . user . id ;
390- const { id } = req . params ;
391- const { calendar_id } = req . body ;
430+ const { calendar_id, owner_id } = req . body ;
431+
432+ if ( ! calendar_id || ! owner_id ) {
433+ return res . status ( 400 ) . json ( {
434+ error : "Calendar ID and Owner ID are required" ,
435+ } ) ;
436+ }
392437
393438 const { data : existingTimetable , error : existingTimetableError } =
394439 await supabase
395440 . schema ( "timetable" )
396441 . from ( "shared" )
397442 . select ( "*" )
398- . eq ( "id " , id )
443+ . eq ( "owner_id " , owner_id )
399444 . eq ( "calendar_id" , calendar_id )
400445 . eq ( "shared_id" , shared_id ) ;
401446
@@ -412,15 +457,15 @@ export default {
412457 . schema ( "timetable" )
413458 . from ( "shared" )
414459 . delete ( )
415- . eq ( "id " , id )
460+ . eq ( "owner_id " , owner_id )
416461 . eq ( "calendar_id" , calendar_id )
417462 . eq ( "shared_id" , shared_id ) ;
418463 if ( deleteError ) {
419464 return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
420465 }
421466
422467 return res . status ( 200 ) . json ( {
423- message : `Sharing record: ${ id } of calendar: ${ calendar_id } deleted successfully` ,
468+ message : `Sharing record with owner_id of ${ owner_id } and calendar_id of ${ calendar_id } deleted successfully` ,
424469 } ) ;
425470 } catch ( error ) {
426471 return res . status ( 500 ) . send ( { error } ) ;
0 commit comments