feat(profile-repository): Refactor ProfileRepositoryImpl to utilize DataConnectService and simplify authentication handling

This commit is contained in:
Achintha Isuru
2026-02-16 16:19:27 -05:00
parent a10617f17d
commit 0fc317e1da
2 changed files with 10 additions and 30 deletions

View File

@@ -1,4 +1,3 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:krow_data_connect/krow_data_connect.dart';
import 'package:krow_domain/krow_domain.dart';
@@ -15,38 +14,23 @@ import '../../domain/repositories/profile_repository.dart';
/// Currently uses [ProfileRepositoryMock] from data_connect.
/// When Firebase Data Connect is ready, this will be swapped with a real implementation.
class ProfileRepositoryImpl
with DataErrorHandler
implements ProfileRepositoryInterface {
/// Creates a [ProfileRepositoryImpl].
///
/// Requires a [ExampleConnector] from the data_connect package and [FirebaseAuth].
const ProfileRepositoryImpl({
required this.connector,
required this.firebaseAuth,
});
ProfileRepositoryImpl() : _service = DataConnectService.instance;
/// The Data Connect connector used for data operations.
final ExampleConnector connector;
/// The Firebase Auth instance.
final FirebaseAuth firebaseAuth;
final DataConnectService _service;
@override
Future<Staff> getStaffProfile() async {
return executeProtected(() async {
final user = firebaseAuth.currentUser;
if (user == null) {
throw NotAuthenticatedException(
technicalMessage: 'User not authenticated');
}
final response = await connector.getStaffByUserId(userId: user.uid).execute();
return _service.run(() async {
final staffId = await _service.getStaffId();
final response = await _service.connector.getStaffById(id: staffId).execute();
if (response.data.staffs.isEmpty) {
if (response.data.staff == null) {
throw const ServerException(technicalMessage: 'Staff not found');
}
final GetStaffByUserIdStaffs rawStaff = response.data.staffs.first;
final GetStaffByIdStaff rawStaff = response.data.staff!;
// Map the raw data connect object to the Domain Entity
return Staff(
@@ -71,7 +55,8 @@ class ProfileRepositoryImpl
@override
Future<void> signOut() async {
try {
await firebaseAuth.signOut();
await _service.auth.signOut();
_service.clearCache();
} catch (e) {
throw Exception('Error signing out: ${e.toString()}');
}

View File

@@ -1,8 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_core/core.dart';
import 'package:krow_data_connect/krow_data_connect.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'data/repositories/profile_repository_impl.dart';
import 'domain/repositories/profile_repository.dart';
@@ -25,10 +23,7 @@ class StaffProfileModule extends Module {
void binds(Injector i) {
// Repository implementation - delegates to data_connect
i.addLazySingleton<ProfileRepositoryInterface>(
() => ProfileRepositoryImpl(
connector: ExampleConnector.instance,
firebaseAuth: FirebaseAuth.instance,
),
ProfileRepositoryImpl.new,
);
// Use cases - depend on repository interface