feat(data-connect): Add run method for centralized error handling and authentication checks

This commit is contained in:
Achintha Isuru
2026-02-16 15:39:40 -05:00
parent c3abb819c9
commit 3245c957f6

View File

@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
import 'package:firebase_data_connect/firebase_data_connect.dart' as fdc;
import 'package:krow_core/core.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../krow_data_connect.dart' as dc;
import '../mixins/data_error_handler.dart';
@@ -20,6 +21,7 @@ class DataConnectService with DataErrorHandler {
final dc.ExampleConnector connector = dc.ExampleConnector.instance;
/// The Firebase Auth instance.
firebase_auth.FirebaseAuth get auth => _auth;
final firebase_auth.FirebaseAuth _auth = firebase_auth.FirebaseAuth.instance;
/// Cache for the current staff ID to avoid redundant lookups.
@@ -97,6 +99,20 @@ class DataConnectService with DataErrorHandler {
return fdc.Timestamp(nanoseconds, seconds);
}
// --- 3. Unified Execution ---
// Repositories call this to benefit from centralized error handling/logging
Future<T> run<T>(
Future<T> Function() action, {
bool requiresAuthentication = true,
}) {
if (requiresAuthentication && auth.currentUser == null) {
throw const NotAuthenticatedException(
technicalMessage: 'User must be authenticated to perform this action',
);
}
return executeProtected(action);
}
/// Clears the internal cache (e.g., on logout).
void clearCache() {
_cachedStaffId = null;