@@ -19,34 +19,33 @@ export default {
1919 . json ( { error : "Shared user email and calendar ID are required" } ) ;
2020 }
2121
22- // Owner cannot share a timetable to themselve
22+ // Owner cannot share a timetable to themselves
2323 if ( shared_email === owner_email ) {
2424 return res
2525 . status ( 400 )
26- . json ( { error : "Users cannot share timetable to themselves " } ) ;
26+ . json ( { error : "Users cannot share a timetable with themselves" } ) ;
2727 }
2828
2929 // Query users for shared_id using email
3030 const { data : sharedUser , error : sharedError } = await supabase . rpc (
3131 "get_user_id_by_email" ,
32- {
33- email : shared_email ,
34- } ,
32+ { email : shared_email }
3533 ) ;
3634
3735 if ( sharedError ) {
3836 return res . status ( 500 ) . json ( { error : sharedError . message } ) ;
3937 }
4038
41- if ( ! sharedUser ) {
39+ // Ensure sharedUser exists and is not an empty array
40+ if ( ! sharedUser || sharedUser . length === 0 ) {
4241 return res
4342 . status ( 404 )
4443 . json ( { error : "User with provided email not found" } ) ;
4544 }
4645
4746 const shared_id = sharedUser [ 0 ] . id ;
4847
49- // Check for the calendar exists and belongs to the owner
48+ // Check if the calendar exists and belongs to the owner
5049 const { data : timeTable , error : timeTableError } = await supabase
5150 . schema ( "timetable" )
5251 . from ( "timetables" )
@@ -61,7 +60,7 @@ export default {
6160 } ) ;
6261 }
6362
64- // Check if the sharing has already existed
63+ // Check if the sharing already exists
6564 const { data : existingShare , error : existingShareError } = await supabase
6665 . schema ( "timetable" )
6766 . from ( "shared" )
@@ -77,7 +76,7 @@ export default {
7776 } ) ;
7877 }
7978
80- // Inser the shared timetable entry
79+ // Insert the shared timetable entry
8180 const { data : shareInsert , error : shareError } = await supabase
8281 . schema ( "timetable" )
8382 . from ( "shared" )
@@ -96,7 +95,39 @@ export default {
9695 } ) ,
9796
9897 /**
99- * Get all share timetables for the current user
98+ * Get all timetables that the owner has shared
99+ */
100+ getOwnerShare : asyncHandler ( async ( req : Request , res : Response ) => {
101+ try {
102+ const user_id = ( req as any ) . user . id ;
103+
104+ //Fetch all shared calendar IDs where user is the owner who share
105+ const { data : shareData , error : sharedError } = await supabase
106+ . schema ( "timetable" )
107+ . from ( "shared" )
108+ . select (
109+ "calendar_id, owner_id, timetables!inner(id, user_id, timetable_title, semester, favorite)"
110+ )
111+ . eq ( "owner_id" , user_id ) ;
112+
113+ if ( sharedError ) {
114+ return res . status ( 400 ) . json ( { error : sharedError . message } ) ;
115+ }
116+
117+ if ( ! shareData || shareData . length === 0 ) {
118+ return res
119+ . status ( 404 )
120+ . json ( { error : "This user has not shared any timetables" } ) ;
121+ }
122+
123+ return res . status ( 200 ) . json ( shareData ) ;
124+ } catch ( error ) {
125+ return res . status ( 500 ) . send ( { error } ) ;
126+ }
127+ } ) ,
128+
129+ /**
130+ * Get all timetables shared with the current user
100131 * @route GET /api/shared
101132 */
102133
@@ -109,7 +140,7 @@ export default {
109140 . schema ( "timetable" )
110141 . from ( "shared" )
111142 . select (
112- "calendar_id, timetables!inner(id, user_id, timetable_title, semester, favorite)" ,
143+ "calendar_id, owner_id, timetables!inner(id, user_id, timetable_title, semester, favorite)"
113144 )
114145 . eq ( "shared_id" , user_id ) ;
115146
@@ -133,6 +164,7 @@ export default {
133164 * Delete all shared record for a timetable as the timetable's owner
134165 * @route DELETE /api/shared/owner/:calendar_id
135166 */
167+
136168 deleteOwnerShare : asyncHandler ( async ( req : Request , res : Response ) => {
137169 try {
138170 const owner_id = ( req as any ) . user . id ;
0 commit comments