@@ -26,23 +26,20 @@ export default {
2626 . json ( { error : "Users cannot share a timetable with themselves" } ) ;
2727 }
2828
29- // Query users for shared_id using email
3029 const { data : sharedUser , error : sharedError } = await supabase . rpc (
3130 "get_user_id_by_email" ,
32- { email : shared_email } ,
31+ { email : shared_email }
3332 ) ;
3433
3534 if ( sharedError ) {
36- return res . status ( 500 ) . json ( { error : sharedError . message } ) ;
35+ return res . status ( 400 ) . json ( { Error : sharedError . message } ) ;
3736 }
3837
39- // Ensure sharedUser exists and is not an empty array
4038 if ( ! sharedUser || sharedUser . length === 0 ) {
4139 return res
4240 . status ( 400 )
4341 . json ( { error : "User with provided email not found" } ) ;
4442 }
45-
4643 const shared_id = sharedUser [ 0 ] . id ;
4744
4845 // Check if the calendar exists and belongs to the owner
@@ -106,7 +103,7 @@ export default {
106103 . schema ( "timetable" )
107104 . from ( "shared" )
108105 . select (
109- "calendar_id, owner_id, timetables!inner(id, user_id, timetable_title, semester, favorite)" ,
106+ "calendar_id, owner_id, shared_id, timetables!inner(id, user_id, timetable_title, semester, favorite)"
110107 )
111108 . eq ( "owner_id" , user_id ) ;
112109
@@ -140,7 +137,7 @@ export default {
140137 . schema ( "timetable" )
141138 . from ( "shared" )
142139 . select (
143- "calendar_id, owner_id, timetables!inner(id, user_id, timetable_title, semester, favorite)" ,
140+ "calendar_id, owner_id, shared_id, timetables!inner(id, user_id, timetable_title, semester, favorite)"
144141 )
145142 . eq ( "shared_id" , user_id ) ;
146143
@@ -162,64 +159,243 @@ export default {
162159
163160 /**
164161 * Delete all shared record for a timetable as the timetable's owner
165- * @route DELETE /api/shared/owner/:calendar_id
162+ * @route DELETE /api/shared/owner/:id?
166163 */
167164
168165 deleteOwnerShare : asyncHandler ( async ( req : Request , res : Response ) => {
169166 try {
170167 const owner_id = ( req as any ) . user . id ;
171- const { calendar_id } = req . params ;
172-
173- const { data : existingTimetable , error : existingTimetableError } =
174- await supabase
168+ const { id } = req . params ;
169+ const { calendar_id, shared_email } = req . body ;
170+
171+ if ( ! id ) {
172+ if ( calendar_id && ! shared_email ) {
173+ // Check if the provided calendar_id belong to the current user
174+ const { data : existingTimetable , error : existingTimetableError } =
175+ await supabase
176+ . schema ( "timetable" )
177+ . from ( "shared" )
178+ . select ( "*" )
179+ . eq ( "calendar_id" , calendar_id )
180+ . eq ( "owner_id" , owner_id ) ;
181+
182+ if ( existingTimetableError ) {
183+ return res
184+ . status ( 500 )
185+ . json ( { error : existingTimetableError . message } ) ;
186+ }
187+
188+ if ( ! existingTimetable || existingTimetable . length === 0 ) {
189+ return res
190+ . status ( 404 )
191+ . json ( { error : "Provided timetable for delete does not found" } ) ;
192+ }
193+
194+ //Delete all shares belong to the owner for a specific table
195+ const { error : deleteError } = await supabase
196+ . schema ( "timetable" )
197+ . from ( "shared" )
198+ . delete ( )
199+ . eq ( "calendar_id" , calendar_id )
200+ . eq ( "owner_id" , owner_id ) ;
201+
202+ if ( deleteError ) {
203+ return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
204+ }
205+
206+ return res . status ( 200 ) . send ( {
207+ message : `All sharing records for the timetable: ${ calendar_id } of user: ${
208+ ( req as any ) . user . email
209+ } have been deleted successfully`,
210+ } ) ;
211+ }
212+
213+ if ( ! calendar_id && shared_email ) {
214+ // Delete all shares belonging to the owner shared with a specific person
215+
216+ // Get Person id via email
217+ const { data : sharedUser , error : sharedError } = await supabase . rpc (
218+ "get_user_id_by_email" ,
219+ { email : shared_email }
220+ ) ;
221+
222+ if ( sharedError ) {
223+ return res . status ( 400 ) . json ( { error : sharedError . message } ) ;
224+ }
225+
226+ if ( ! sharedUser || sharedUser . length === 0 ) {
227+ return res
228+ . status ( 400 )
229+ . json ( { error : "User with provided email not found" } ) ;
230+ }
231+
232+ const shared_id = sharedUser [ 0 ] . id ;
233+
234+ //Check if the curernt owner has shared with the provided user
235+ const { data : existingTimetable , error : existingTimetableError } =
236+ await supabase
237+ . schema ( "timetable" )
238+ . from ( "shared" )
239+ . select ( "*" )
240+ . eq ( "shared_id" , shared_id )
241+ . eq ( "owner_id" , owner_id ) ;
242+
243+ if ( existingTimetableError ) {
244+ return res
245+ . status ( 500 )
246+ . json ( { error : existingTimetableError . message } ) ;
247+ }
248+
249+ if ( ! existingTimetable || existingTimetable . length === 0 ) {
250+ return res . status ( 404 ) . json ( {
251+ error : "You have not shared any timetable with the provided user" ,
252+ } ) ;
253+ }
254+
255+ const { error : deleteError } = await supabase
256+ . schema ( "timetable" )
257+ . from ( "shared" )
258+ . delete ( )
259+ . eq ( "owner_id" , owner_id )
260+ . eq ( "shared_id" , shared_id ) ;
261+
262+ if ( deleteError ) {
263+ return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
264+ }
265+
266+ return res . status ( 200 ) . json ( {
267+ message : `All sharing records of user: ${
268+ ( req as any ) . user . email
269+ } to user: ${ shared_email } have been deleted successfully`,
270+ } ) ;
271+ }
272+
273+ if ( calendar_id && shared_email ) {
274+ // Get Person id via email
275+ const { data : sharedUser , error : sharedError } = await supabase . rpc (
276+ "get_user_id_by_email" ,
277+ { email : shared_email }
278+ ) ;
279+
280+ if ( sharedError ) {
281+ return res . status ( 400 ) . json ( { error : sharedError . message } ) ;
282+ }
283+
284+ if ( ! sharedUser || sharedUser . length === 0 ) {
285+ return res
286+ . status ( 400 )
287+ . json ( { error : "User with provided email not found" } ) ;
288+ }
289+
290+ const shared_id = sharedUser [ 0 ] . id ;
291+
292+ //Check if the curernt owner has shared with the provided user
293+ const { data : existingTimetable , error : existingTimetableError } =
294+ await supabase
295+ . schema ( "timetable" )
296+ . from ( "shared" )
297+ . select ( "*" )
298+ . eq ( "calendar_id" , calendar_id )
299+ . eq ( "shared_id" , shared_id )
300+ . eq ( "owner_id" , owner_id ) ;
301+
302+ if ( existingTimetableError ) {
303+ return res
304+ . status ( 500 )
305+ . json ( { error : existingTimetableError . message } ) ;
306+ }
307+
308+ if ( ! existingTimetable || existingTimetable . length === 0 ) {
309+ return res . status ( 404 ) . json ( {
310+ error :
311+ "You have not shared the provided timetable with the provided user" ,
312+ } ) ;
313+ }
314+
315+ const { error : deleteError } = await supabase
316+ . schema ( "timetable" )
317+ . from ( "shared" )
318+ . delete ( )
319+ . eq ( "calendar_id" , calendar_id )
320+ . eq ( "owner_id" , owner_id )
321+ . eq ( "shared_id" , shared_id ) ;
322+
323+ if ( deleteError ) {
324+ return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
325+ }
326+
327+ return res . status ( 200 ) . json ( {
328+ message : `All sharing records of table: ${ calendar_id } from user: ${
329+ ( req as any ) . user . email
330+ } to user: ${ shared_email } have been deleted successfully`,
331+ } ) ;
332+ }
333+ return res . status ( 400 ) . json ( {
334+ error : "Calendar_id, shared_email or share id is required" ,
335+ } ) ;
336+ } else {
337+ if ( ! calendar_id ) {
338+ return res . status ( 400 ) . json ( {
339+ error : "Calendar_id is requried to delete a specific share entry" ,
340+ } ) ;
341+ }
342+
343+ const { data : existingShare , error : existingShareError } =
344+ await supabase
345+ . schema ( "timetable" )
346+ . from ( "shared" )
347+ . select ( "*" )
348+ . eq ( "id" , id )
349+ . eq ( "calendar_id" , calendar_id )
350+ . eq ( "owner_id" , owner_id ) ;
351+
352+ if ( existingShareError ) {
353+ return res . status ( 400 ) . json ( { error : existingShareError . message } ) ;
354+ }
355+
356+ if ( ! existingShare || existingShare . length === 0 ) {
357+ return res
358+ . status ( 404 )
359+ . json ( { error : "Cannot find the provided share entry" } ) ;
360+ }
361+
362+ const { error : deleteError } = await supabase
175363 . schema ( "timetable" )
176364 . from ( "shared" )
177- . select ( "*" )
365+ . delete ( )
366+ . eq ( "id" , id )
178367 . eq ( "calendar_id" , calendar_id )
179368 . eq ( "owner_id" , owner_id ) ;
180369
181- if ( existingTimetableError ) {
182- return res . status ( 500 ) . json ( { error : existingTimetableError . message } ) ;
183- }
184-
185- if ( ! existingTimetable || existingTimetable . length === 0 ) {
186- return res
187- . status ( 404 )
188- . json ( { error : "Provided timetable for delete does not found" } ) ;
189- }
190- const { error : deleteError } = await supabase
191- . schema ( "timetable" )
192- . from ( "shared" )
193- . delete ( )
194- . eq ( "calendar_id" , calendar_id )
195- . eq ( "owner_id" , owner_id ) ;
370+ if ( deleteError ) {
371+ return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
372+ }
196373
197- if ( deleteError ) {
198- return res . status ( 400 ) . json ( { error : deleteError . message } ) ;
374+ return res . status ( 200 ) . json ( {
375+ message : `Share number ${ id } of calendar: ${ calendar_id } has been sucessfully deleted` ,
376+ } ) ;
199377 }
200-
201- return res . status ( 200 ) . send ( {
202- message : "All sharing records for the timetable deleted successfully" ,
203- } ) ;
204378 } catch ( error ) {
205379 return res . status ( 500 ) . send ( { error } ) ;
206380 }
207381 } ) ,
208382
209383 /**
210384 * Delete a shared entryas shared userd
211- * @route DELETE /api/shared/:calendar_id
385+ * @route DELETE /api/shared/:id?
212386 */
213387 deleteShare : asyncHandler ( async ( req : Request , res : Response ) => {
214388 try {
215389 const shared_id = ( req as any ) . user . id ;
216- const { calendar_id } = req . params ;
390+ const { id } = req . params ;
391+ const { calendar_id } = req . body ;
217392
218393 const { data : existingTimetable , error : existingTimetableError } =
219394 await supabase
220395 . schema ( "timetable" )
221396 . from ( "shared" )
222397 . select ( "*" )
398+ . eq ( "id" , id )
223399 . eq ( "calendar_id" , calendar_id )
224400 . eq ( "shared_id" , shared_id ) ;
225401
@@ -236,6 +412,7 @@ export default {
236412 . schema ( "timetable" )
237413 . from ( "shared" )
238414 . delete ( )
415+ . eq ( "id" , id )
239416 . eq ( "calendar_id" , calendar_id )
240417 . eq ( "shared_id" , shared_id ) ;
241418 if ( deleteError ) {
@@ -244,7 +421,7 @@ export default {
244421
245422 return res
246423 . status ( 200 )
247- . json ( { message : " Sharing record deleted successfully" } ) ;
424+ . json ( { message : ` Sharing record: ${ id } deleted successfully` } ) ;
248425 } catch ( error ) {
249426 return res . status ( 500 ) . send ( { error } ) ;
250427 }
0 commit comments