diff --git a/lib/services/collection_service.dart b/lib/services/collection_service.dart index c237e8a..523c4cb 100644 --- a/lib/services/collection_service.dart +++ b/lib/services/collection_service.dart @@ -374,11 +374,42 @@ class CollectionService { throw Exception('Collection owner cannot be removed.'); } - await supabase + final targetMembership = await supabase + .from('collection_members') + .select('id, role') + .eq('collection_id', collectionId) + .eq('user_id', memberUserId) + .maybeSingle(); + + if (targetMembership == null) { + throw Exception('Member not found in this collection.'); + } + if (targetMembership['role'] == 'owner') { + throw Exception('Collection owner cannot be removed.'); + } + + final deleted = await supabase .from('collection_members') .delete() - .eq('user_id', memberUserId) - .eq('collection_id', collectionId); + .eq('id', targetMembership['id'] as String) + .select('id'); + + if (deleted.isEmpty) { + throw Exception( + 'Member could not be removed (blocked by database policy).', + ); + } + + final stillExists = await supabase + .from('collection_members') + .select('id') + .eq('collection_id', collectionId) + .eq('user_id', memberUserId) + .maybeSingle(); + + if (stillExists != null) { + throw Exception('Member removal did not persist. Please try again.'); + } } /// Leave a collection (for non-owners).