Implementation
Future<void> shareDomain(
String domainId,
String targetUserId, {
String permission = 'read',
}) async {
final userId = _client.auth.currentUser?.id;
if (userId == null) {
throw StateError('Cannot share domain: user is not authenticated');
}
// Plain INSERT: the partial unique index `uq_curriculum_shares_active`
// (migration 051) enforces one active share per (domain, recipient).
// If the recipient already has an active share, PostgreSQL raises a
// unique violation the caller can surface. Re-sharing after revocation
// succeeds because revoked rows are excluded from the partial index.
await _client.from('curriculum_shares').insert({
'domain_id': domainId,
'shared_with_user_id': targetUserId,
'permission': permission,
'shared_by_user_id': userId,
});
}