From a119f36e41bf84038d845cd5e3b1afc1479a6ee4 Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Tue, 17 Feb 2026 16:31:23 -0500 Subject: [PATCH] feat: Refactor StaffSession to remove user field and update related session handling --- .../lib/src/session/staff_session_store.dart | 3 +- .../auth_repository_impl.dart | 1 - .../profile_setup_repository_impl.dart | 16 ++++----- .../repositories/home_repository_impl.dart | 36 ++++++++++++++++++- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/apps/mobile/packages/data_connect/lib/src/session/staff_session_store.dart b/apps/mobile/packages/data_connect/lib/src/session/staff_session_store.dart index 7c5229c9..02333a0c 100644 --- a/apps/mobile/packages/data_connect/lib/src/session/staff_session_store.dart +++ b/apps/mobile/packages/data_connect/lib/src/session/staff_session_store.dart @@ -1,9 +1,8 @@ import 'package:krow_domain/krow_domain.dart' as domain; class StaffSession { - const StaffSession({required this.user, this.staff, this.ownerId}); + const StaffSession({this.staff, this.ownerId}); - final domain.User user; final domain.Staff? staff; final String? ownerId; } diff --git a/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/auth_repository_impl.dart b/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/auth_repository_impl.dart index e2dab61b..5d204fcf 100644 --- a/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/auth_repository_impl.dart +++ b/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/auth_repository_impl.dart @@ -239,7 +239,6 @@ class AuthRepositoryImpl implements AuthRepositoryInterface { ); StaffSessionStore.instance.setSession( StaffSession( - user: domainUser, staff: domainStaff, ownerId: staffRecord?.ownerId, ), diff --git a/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/profile_setup_repository_impl.dart b/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/profile_setup_repository_impl.dart index fe25eea3..c2f013d6 100644 --- a/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/profile_setup_repository_impl.dart +++ b/apps/mobile/packages/features/staff/authentication/lib/src/data/repositories_impl/profile_setup_repository_impl.dart @@ -22,19 +22,17 @@ class ProfileSetupRepositoryImpl implements ProfileSetupRepository { final auth.User? firebaseUser = _service.auth.currentUser; if (firebaseUser == null) { throw const NotAuthenticatedException( - technicalMessage: 'User not authenticated.'); + technicalMessage: 'User not authenticated.', + ); } final StaffSession? session = StaffSessionStore.instance.session; - final String email = session?.user.email ?? ''; + final String email = session?.staff?.email ?? ''; final String? phone = firebaseUser.phoneNumber; final fdc.OperationResult result = await _service.connector - .createStaff( - userId: firebaseUser.uid, - fullName: fullName, - ) + .createStaff(userId: firebaseUser.uid, fullName: fullName) .bio(bio) .preferredLocations(preferredLocations) .maxDistanceMiles(maxDistanceMiles.toInt()) @@ -45,7 +43,7 @@ class ProfileSetupRepositoryImpl implements ProfileSetupRepository { .execute(); final String staffId = result.data.staff_insert.id; - + final Staff staff = Staff( id: staffId, authProviderId: firebaseUser.uid, @@ -54,10 +52,10 @@ class ProfileSetupRepositoryImpl implements ProfileSetupRepository { phone: phone, status: StaffStatus.completedProfile, ); - + if (session != null) { StaffSessionStore.instance.setSession( - StaffSession(user: session.user, staff: staff, ownerId: session.ownerId), + StaffSession(staff: staff, ownerId: session.ownerId), ); } }); diff --git a/apps/mobile/packages/features/staff/home/lib/src/data/repositories/home_repository_impl.dart b/apps/mobile/packages/features/staff/home/lib/src/data/repositories/home_repository_impl.dart index 8f8bf8a8..61de301e 100644 --- a/apps/mobile/packages/features/staff/home/lib/src/data/repositories/home_repository_impl.dart +++ b/apps/mobile/packages/features/staff/home/lib/src/data/repositories/home_repository_impl.dart @@ -76,7 +76,41 @@ class HomeRepositoryImpl @override Future getStaffName() async { final session = StaffSessionStore.instance.session; - return session?.staff?.name; + + // If session data is available, return staff name immediately + if (session?.staff?.name != null) { + return session!.staff!.name; + } + + // If session is not initialized, attempt to fetch staff data to populate session + return await _service.run(() async { + final staffId = await _service.getStaffId(); + final response = await _service.connector + .getStaffById(id: staffId) + .execute(); + + if (response.data.staff == null) { + throw Exception('Staff data not found for ID: $staffId'); + } + + final staff = response.data.staff!; + final updatedSession = StaffSession( + staff: Staff( + id: staff.id, + authProviderId: staff.userId, + name: staff.fullName, + email: staff.email ?? '', + phone: staff.phone, + status: StaffStatus.completedProfile, + address: staff.addres, + avatar: staff.photoUrl, + ), + ownerId: session?.ownerId, + ); + StaffSessionStore.instance.setSession(updatedSession); + + return staff.fullName; + }); } // Mappers specific to Home's Domain Entity 'Shift'