feat(profile-repository): Refactor ProfileRepositoryImpl to utilize DataConnectService and simplify authentication handling
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import 'package:firebase_auth/firebase_auth.dart';
|
|
||||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||||
import 'package:krow_domain/krow_domain.dart';
|
import 'package:krow_domain/krow_domain.dart';
|
||||||
|
|
||||||
@@ -15,38 +14,23 @@ import '../../domain/repositories/profile_repository.dart';
|
|||||||
/// Currently uses [ProfileRepositoryMock] from data_connect.
|
/// Currently uses [ProfileRepositoryMock] from data_connect.
|
||||||
/// When Firebase Data Connect is ready, this will be swapped with a real implementation.
|
/// When Firebase Data Connect is ready, this will be swapped with a real implementation.
|
||||||
class ProfileRepositoryImpl
|
class ProfileRepositoryImpl
|
||||||
with DataErrorHandler
|
|
||||||
implements ProfileRepositoryInterface {
|
implements ProfileRepositoryInterface {
|
||||||
/// Creates a [ProfileRepositoryImpl].
|
/// Creates a [ProfileRepositoryImpl].
|
||||||
///
|
ProfileRepositoryImpl() : _service = DataConnectService.instance;
|
||||||
/// Requires a [ExampleConnector] from the data_connect package and [FirebaseAuth].
|
|
||||||
const ProfileRepositoryImpl({
|
|
||||||
required this.connector,
|
|
||||||
required this.firebaseAuth,
|
|
||||||
});
|
|
||||||
|
|
||||||
/// The Data Connect connector used for data operations.
|
final DataConnectService _service;
|
||||||
final ExampleConnector connector;
|
|
||||||
|
|
||||||
/// The Firebase Auth instance.
|
|
||||||
final FirebaseAuth firebaseAuth;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Staff> getStaffProfile() async {
|
Future<Staff> getStaffProfile() async {
|
||||||
return executeProtected(() async {
|
return _service.run(() async {
|
||||||
final user = firebaseAuth.currentUser;
|
final staffId = await _service.getStaffId();
|
||||||
if (user == null) {
|
final response = await _service.connector.getStaffById(id: staffId).execute();
|
||||||
throw NotAuthenticatedException(
|
|
||||||
technicalMessage: 'User not authenticated');
|
|
||||||
}
|
|
||||||
|
|
||||||
final response = await connector.getStaffByUserId(userId: user.uid).execute();
|
|
||||||
|
|
||||||
if (response.data.staffs.isEmpty) {
|
if (response.data.staff == null) {
|
||||||
throw const ServerException(technicalMessage: 'Staff not found');
|
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
|
// Map the raw data connect object to the Domain Entity
|
||||||
return Staff(
|
return Staff(
|
||||||
@@ -71,7 +55,8 @@ class ProfileRepositoryImpl
|
|||||||
@override
|
@override
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
try {
|
try {
|
||||||
await firebaseAuth.signOut();
|
await _service.auth.signOut();
|
||||||
|
_service.clearCache();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception('Error signing out: ${e.toString()}');
|
throw Exception('Error signing out: ${e.toString()}');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_modular/flutter_modular.dart';
|
import 'package:flutter_modular/flutter_modular.dart';
|
||||||
import 'package:krow_core/core.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 'data/repositories/profile_repository_impl.dart';
|
||||||
import 'domain/repositories/profile_repository.dart';
|
import 'domain/repositories/profile_repository.dart';
|
||||||
@@ -25,10 +23,7 @@ class StaffProfileModule extends Module {
|
|||||||
void binds(Injector i) {
|
void binds(Injector i) {
|
||||||
// Repository implementation - delegates to data_connect
|
// Repository implementation - delegates to data_connect
|
||||||
i.addLazySingleton<ProfileRepositoryInterface>(
|
i.addLazySingleton<ProfileRepositoryInterface>(
|
||||||
() => ProfileRepositoryImpl(
|
ProfileRepositoryImpl.new,
|
||||||
connector: ExampleConnector.instance,
|
|
||||||
firebaseAuth: FirebaseAuth.instance,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Use cases - depend on repository interface
|
// Use cases - depend on repository interface
|
||||||
|
|||||||
Reference in New Issue
Block a user