feat: Refactor session management and improve user session data retrieval
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import 'package:krow_domain/krow_domain.dart' as domain;
|
||||
|
||||
class ClientBusinessSession {
|
||||
final String id;
|
||||
final String businessName;
|
||||
@@ -19,15 +17,9 @@ class ClientBusinessSession {
|
||||
}
|
||||
|
||||
class ClientSession {
|
||||
final domain.User user;
|
||||
final String? userPhotoUrl;
|
||||
final ClientBusinessSession? business;
|
||||
|
||||
const ClientSession({
|
||||
required this.user,
|
||||
required this.userPhotoUrl,
|
||||
required this.business,
|
||||
});
|
||||
const ClientSession({required this.business});
|
||||
}
|
||||
|
||||
class ClientSessionStore {
|
||||
|
||||
@@ -24,9 +24,8 @@ import '../../domain/repositories/auth_repository_interface.dart';
|
||||
/// identity management and Krow's Data Connect SDK for storing user profile data.
|
||||
class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
/// Creates an [AuthRepositoryImpl] with the real dependencies.
|
||||
AuthRepositoryImpl({
|
||||
dc.DataConnectService? service,
|
||||
}) : _service = service ?? dc.DataConnectService.instance;
|
||||
AuthRepositoryImpl({dc.DataConnectService? service})
|
||||
: _service = service ?? dc.DataConnectService.instance;
|
||||
|
||||
final dc.DataConnectService _service;
|
||||
|
||||
@@ -36,11 +35,8 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final firebase.UserCredential credential =
|
||||
await _service.auth.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
final firebase.UserCredential credential = await _service.auth
|
||||
.signInWithEmailAndPassword(email: email, password: password);
|
||||
|
||||
final firebase.User? firebaseUser = credential.user;
|
||||
if (firebaseUser == null) {
|
||||
@@ -60,9 +56,7 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
technicalMessage: 'Firebase error code: ${e.code}',
|
||||
);
|
||||
} else if (e.code == 'network-request-failed') {
|
||||
throw NetworkException(
|
||||
technicalMessage: 'Firebase: ${e.message}',
|
||||
);
|
||||
throw NetworkException(technicalMessage: 'Firebase: ${e.message}');
|
||||
} else {
|
||||
throw SignInFailedException(
|
||||
technicalMessage: 'Firebase auth error: ${e.message}',
|
||||
@@ -71,9 +65,7 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
} on domain.AppException {
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
throw SignInFailedException(
|
||||
technicalMessage: 'Unexpected error: $e',
|
||||
);
|
||||
throw SignInFailedException(technicalMessage: 'Unexpected error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,11 +80,8 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
|
||||
try {
|
||||
// Step 1: Try to create Firebase Auth user
|
||||
final firebase.UserCredential credential =
|
||||
await _service.auth.createUserWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
final firebase.UserCredential credential = await _service.auth
|
||||
.createUserWithEmailAndPassword(email: email, password: password);
|
||||
|
||||
firebaseUser = credential.user;
|
||||
if (firebaseUser == null) {
|
||||
@@ -111,9 +100,7 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
);
|
||||
} on firebase.FirebaseAuthException catch (e) {
|
||||
if (e.code == 'weak-password') {
|
||||
throw WeakPasswordException(
|
||||
technicalMessage: 'Firebase: ${e.message}',
|
||||
);
|
||||
throw WeakPasswordException(technicalMessage: 'Firebase: ${e.message}');
|
||||
} else if (e.code == 'email-already-in-use') {
|
||||
// Email exists in Firebase Auth - try to sign in and complete registration
|
||||
return await _handleExistingFirebaseAccount(
|
||||
@@ -122,9 +109,7 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
companyName: companyName,
|
||||
);
|
||||
} else if (e.code == 'network-request-failed') {
|
||||
throw NetworkException(
|
||||
technicalMessage: 'Firebase: ${e.message}',
|
||||
);
|
||||
throw NetworkException(technicalMessage: 'Firebase: ${e.message}');
|
||||
} else {
|
||||
throw SignUpFailedException(
|
||||
technicalMessage: 'Firebase auth error: ${e.message}',
|
||||
@@ -133,15 +118,17 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
} on domain.AppException {
|
||||
// Rollback for our known exceptions
|
||||
await _rollbackSignUp(
|
||||
firebaseUser: firebaseUser, businessId: createdBusinessId);
|
||||
firebaseUser: firebaseUser,
|
||||
businessId: createdBusinessId,
|
||||
);
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
// Rollback: Clean up any partially created resources
|
||||
await _rollbackSignUp(
|
||||
firebaseUser: firebaseUser, businessId: createdBusinessId);
|
||||
throw SignUpFailedException(
|
||||
technicalMessage: 'Unexpected error: $e',
|
||||
firebaseUser: firebaseUser,
|
||||
businessId: createdBusinessId,
|
||||
);
|
||||
throw SignUpFailedException(technicalMessage: 'Unexpected error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,16 +148,15 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
required String password,
|
||||
required String companyName,
|
||||
}) async {
|
||||
developer.log('Email exists in Firebase, attempting sign-in: $email',
|
||||
name: 'AuthRepository');
|
||||
developer.log(
|
||||
'Email exists in Firebase, attempting sign-in: $email',
|
||||
name: 'AuthRepository',
|
||||
);
|
||||
|
||||
try {
|
||||
// Try to sign in with the provided password
|
||||
final firebase.UserCredential credential =
|
||||
await _service.auth.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
final firebase.UserCredential credential = await _service.auth
|
||||
.signInWithEmailAndPassword(email: email, password: password);
|
||||
|
||||
final firebase.User? firebaseUser = credential.user;
|
||||
if (firebaseUser == null) {
|
||||
@@ -180,32 +166,40 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
}
|
||||
|
||||
// Sign-in succeeded! Check if user already has a BUSINESS account in PostgreSQL
|
||||
final bool hasBusinessAccount =
|
||||
await _checkBusinessUserExists(firebaseUser.uid);
|
||||
final bool hasBusinessAccount = await _checkBusinessUserExists(
|
||||
firebaseUser.uid,
|
||||
);
|
||||
|
||||
if (hasBusinessAccount) {
|
||||
// User already has a KROW Client account
|
||||
developer.log('User already has BUSINESS account: ${firebaseUser.uid}',
|
||||
name: 'AuthRepository');
|
||||
developer.log(
|
||||
'User already has BUSINESS account: ${firebaseUser.uid}',
|
||||
name: 'AuthRepository',
|
||||
);
|
||||
throw AccountExistsException(
|
||||
technicalMessage: 'User ${firebaseUser.uid} already has BUSINESS role',
|
||||
technicalMessage:
|
||||
'User ${firebaseUser.uid} already has BUSINESS role',
|
||||
);
|
||||
}
|
||||
|
||||
// User exists in Firebase but not in KROW PostgreSQL - create the entities
|
||||
developer.log(
|
||||
'Creating BUSINESS account for existing Firebase user: ${firebaseUser.uid}',
|
||||
name: 'AuthRepository');
|
||||
'Creating BUSINESS account for existing Firebase user: ${firebaseUser.uid}',
|
||||
name: 'AuthRepository',
|
||||
);
|
||||
return await _createBusinessAndUser(
|
||||
firebaseUser: firebaseUser,
|
||||
companyName: companyName,
|
||||
email: email,
|
||||
onBusinessCreated: (_) {}, // No rollback needed for existing Firebase user
|
||||
onBusinessCreated:
|
||||
(_) {}, // No rollback needed for existing Firebase user
|
||||
);
|
||||
} on firebase.FirebaseAuthException catch (e) {
|
||||
// Sign-in failed - check why
|
||||
developer.log('Sign-in failed with code: ${e.code}',
|
||||
name: 'AuthRepository');
|
||||
developer.log(
|
||||
'Sign-in failed with code: ${e.code}',
|
||||
name: 'AuthRepository',
|
||||
);
|
||||
|
||||
if (e.code == 'wrong-password' || e.code == 'invalid-credential') {
|
||||
// Password doesn't match - check what providers are available
|
||||
@@ -229,8 +223,10 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
// We can't distinguish between "wrong password" and "no password provider"
|
||||
// due to Firebase deprecating fetchSignInMethodsForEmail.
|
||||
// The PasswordMismatchException message covers both scenarios.
|
||||
developer.log('Password mismatch or different provider for: $email',
|
||||
name: 'AuthRepository');
|
||||
developer.log(
|
||||
'Password mismatch or different provider for: $email',
|
||||
name: 'AuthRepository',
|
||||
);
|
||||
throw PasswordMismatchException(
|
||||
technicalMessage:
|
||||
'Email $email: password mismatch or different auth provider',
|
||||
@@ -242,7 +238,8 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
Future<bool> _checkBusinessUserExists(String firebaseUserId) async {
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response =
|
||||
await _service.run(
|
||||
() => _service.connector.getUserById(id: firebaseUserId).execute());
|
||||
() => _service.connector.getUserById(id: firebaseUserId).execute(),
|
||||
);
|
||||
final dc.GetUserByIdUser? user = response.data.user;
|
||||
return user != null &&
|
||||
(user.userRole == 'BUSINESS' || user.userRole == 'BOTH');
|
||||
@@ -258,14 +255,16 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
// Create Business entity in PostgreSQL
|
||||
|
||||
final OperationResult<dc.CreateBusinessData, dc.CreateBusinessVariables>
|
||||
createBusinessResponse = await _service.run(() => _service.connector
|
||||
.createBusiness(
|
||||
businessName: companyName,
|
||||
userId: firebaseUser.uid,
|
||||
rateGroup: dc.BusinessRateGroup.STANDARD,
|
||||
status: dc.BusinessStatus.PENDING,
|
||||
)
|
||||
.execute());
|
||||
createBusinessResponse = await _service.run(
|
||||
() => _service.connector
|
||||
.createBusiness(
|
||||
businessName: companyName,
|
||||
userId: firebaseUser.uid,
|
||||
rateGroup: dc.BusinessRateGroup.STANDARD,
|
||||
status: dc.BusinessStatus.PENDING,
|
||||
)
|
||||
.execute(),
|
||||
);
|
||||
|
||||
final dc.CreateBusinessBusinessInsert businessData =
|
||||
createBusinessResponse.data.business_insert;
|
||||
@@ -273,28 +272,28 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
|
||||
// Check if User entity already exists in PostgreSQL
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> userResult =
|
||||
await _service.run(() =>
|
||||
_service.connector.getUserById(id: firebaseUser.uid).execute());
|
||||
await _service.run(
|
||||
() => _service.connector.getUserById(id: firebaseUser.uid).execute(),
|
||||
);
|
||||
final dc.GetUserByIdUser? existingUser = userResult.data.user;
|
||||
|
||||
if (existingUser != null) {
|
||||
// User exists (likely in another app like STAFF). Update role to BOTH.
|
||||
await _service.run(() => _service.connector
|
||||
.updateUser(
|
||||
id: firebaseUser.uid,
|
||||
)
|
||||
.userRole('BOTH')
|
||||
.execute());
|
||||
await _service.run(
|
||||
() => _service.connector
|
||||
.updateUser(id: firebaseUser.uid)
|
||||
.userRole('BOTH')
|
||||
.execute(),
|
||||
);
|
||||
} else {
|
||||
// Create new User entity in PostgreSQL
|
||||
await _service.run(() => _service.connector
|
||||
.createUser(
|
||||
id: firebaseUser.uid,
|
||||
role: dc.UserBaseRole.USER,
|
||||
)
|
||||
.email(email)
|
||||
.userRole('BUSINESS')
|
||||
.execute());
|
||||
await _service.run(
|
||||
() => _service.connector
|
||||
.createUser(id: firebaseUser.uid, role: dc.UserBaseRole.USER)
|
||||
.email(email)
|
||||
.userRole('BUSINESS')
|
||||
.execute(),
|
||||
);
|
||||
}
|
||||
|
||||
return _getUserProfile(
|
||||
@@ -340,7 +339,8 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
@override
|
||||
Future<domain.User> signInWithSocial({required String provider}) {
|
||||
throw UnimplementedError(
|
||||
'Social authentication with $provider is not yet implemented.');
|
||||
'Social authentication with $provider is not yet implemented.',
|
||||
);
|
||||
}
|
||||
|
||||
Future<domain.User> _getUserProfile({
|
||||
@@ -349,8 +349,9 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
bool requireBusinessRole = false,
|
||||
}) async {
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response =
|
||||
await _service.run(() =>
|
||||
_service.connector.getUserById(id: firebaseUserId).execute());
|
||||
await _service.run(
|
||||
() => _service.connector.getUserById(id: firebaseUserId).execute(),
|
||||
);
|
||||
final dc.GetUserByIdUser? user = response.data.user;
|
||||
if (user == null) {
|
||||
throw UserNotFoundException(
|
||||
@@ -383,22 +384,22 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
role: user.role.stringValue,
|
||||
);
|
||||
|
||||
final QueryResult<dc.GetBusinessesByUserIdData,
|
||||
dc.GetBusinessesByUserIdVariables> businessResponse =
|
||||
await _service.run(() => _service.connector
|
||||
.getBusinessesByUserId(
|
||||
userId: firebaseUserId,
|
||||
)
|
||||
.execute());
|
||||
final QueryResult<
|
||||
dc.GetBusinessesByUserIdData,
|
||||
dc.GetBusinessesByUserIdVariables
|
||||
>
|
||||
businessResponse = await _service.run(
|
||||
() => _service.connector
|
||||
.getBusinessesByUserId(userId: firebaseUserId)
|
||||
.execute(),
|
||||
);
|
||||
final dc.GetBusinessesByUserIdBusinesses? business =
|
||||
businessResponse.data.businesses.isNotEmpty
|
||||
? businessResponse.data.businesses.first
|
||||
: null;
|
||||
? businessResponse.data.businesses.first
|
||||
: null;
|
||||
|
||||
dc.ClientSessionStore.instance.setSession(
|
||||
dc.ClientSession(
|
||||
user: domainUser,
|
||||
userPhotoUrl: user.photoUrl,
|
||||
business: business == null
|
||||
? null
|
||||
: dc.ClientBusinessSession(
|
||||
|
||||
@@ -19,26 +19,43 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
|
||||
final DateTime now = DateTime.now();
|
||||
final int daysFromMonday = now.weekday - DateTime.monday;
|
||||
final DateTime monday =
|
||||
DateTime(now.year, now.month, now.day).subtract(Duration(days: daysFromMonday));
|
||||
final DateTime weekRangeStart = DateTime(monday.year, monday.month, monday.day);
|
||||
final DateTime weekRangeEnd =
|
||||
DateTime(monday.year, monday.month, monday.day + 13, 23, 59, 59, 999);
|
||||
final fdc.QueryResult<dc.GetCompletedShiftsByBusinessIdData,
|
||||
dc.GetCompletedShiftsByBusinessIdVariables> completedResult =
|
||||
await _service.connector
|
||||
.getCompletedShiftsByBusinessId(
|
||||
businessId: businessId,
|
||||
dateFrom: _service.toTimestamp(weekRangeStart),
|
||||
dateTo: _service.toTimestamp(weekRangeEnd),
|
||||
)
|
||||
.execute();
|
||||
final DateTime monday = DateTime(
|
||||
now.year,
|
||||
now.month,
|
||||
now.day,
|
||||
).subtract(Duration(days: daysFromMonday));
|
||||
final DateTime weekRangeStart = DateTime(
|
||||
monday.year,
|
||||
monday.month,
|
||||
monday.day,
|
||||
);
|
||||
final DateTime weekRangeEnd = DateTime(
|
||||
monday.year,
|
||||
monday.month,
|
||||
monday.day + 13,
|
||||
23,
|
||||
59,
|
||||
59,
|
||||
999,
|
||||
);
|
||||
final fdc.QueryResult<
|
||||
dc.GetCompletedShiftsByBusinessIdData,
|
||||
dc.GetCompletedShiftsByBusinessIdVariables
|
||||
>
|
||||
completedResult = await _service.connector
|
||||
.getCompletedShiftsByBusinessId(
|
||||
businessId: businessId,
|
||||
dateFrom: _service.toTimestamp(weekRangeStart),
|
||||
dateTo: _service.toTimestamp(weekRangeEnd),
|
||||
)
|
||||
.execute();
|
||||
|
||||
double weeklySpending = 0.0;
|
||||
double next7DaysSpending = 0.0;
|
||||
int weeklyShifts = 0;
|
||||
int next7DaysScheduled = 0;
|
||||
for (final dc.GetCompletedShiftsByBusinessIdShifts shift in completedResult.data.shifts) {
|
||||
for (final dc.GetCompletedShiftsByBusinessIdShifts shift
|
||||
in completedResult.data.shifts) {
|
||||
final DateTime? shiftDate = shift.date?.toDateTime();
|
||||
if (shiftDate == null) {
|
||||
continue;
|
||||
@@ -58,17 +75,27 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
}
|
||||
|
||||
final DateTime start = DateTime(now.year, now.month, now.day);
|
||||
final DateTime end = DateTime(now.year, now.month, now.day, 23, 59, 59, 999);
|
||||
final DateTime end = DateTime(
|
||||
now.year,
|
||||
now.month,
|
||||
now.day,
|
||||
23,
|
||||
59,
|
||||
59,
|
||||
999,
|
||||
);
|
||||
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
await _service.connector
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
start: _service.toTimestamp(start),
|
||||
end: _service.toTimestamp(end),
|
||||
)
|
||||
.execute();
|
||||
final fdc.QueryResult<
|
||||
dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables
|
||||
>
|
||||
result = await _service.connector
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
start: _service.toTimestamp(start),
|
||||
end: _service.toTimestamp(end),
|
||||
)
|
||||
.execute();
|
||||
|
||||
int totalNeeded = 0;
|
||||
int totalFilled = 0;
|
||||
@@ -90,12 +117,47 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
}
|
||||
|
||||
@override
|
||||
UserSessionData getUserSessionData() {
|
||||
Future<UserSessionData> getUserSessionData() async {
|
||||
final dc.ClientSession? session = dc.ClientSessionStore.instance.session;
|
||||
return UserSessionData(
|
||||
businessName: session?.business?.businessName ?? '',
|
||||
photoUrl: null, // Business photo isn't currently in session
|
||||
);
|
||||
final dc.ClientBusinessSession? business = session?.business;
|
||||
|
||||
// If session data is available, return it immediately
|
||||
if (business != null) {
|
||||
return UserSessionData(
|
||||
businessName: business.businessName,
|
||||
photoUrl: business.companyLogoUrl,
|
||||
);
|
||||
}
|
||||
|
||||
return await _service.run(() async {
|
||||
// If session is not initialized, attempt to fetch business data to populate session
|
||||
final String businessId = await _service.getBusinessId();
|
||||
final fdc.QueryResult<dc.GetBusinessByIdData, dc.GetBusinessByIdVariables>
|
||||
businessResult = await _service.connector
|
||||
.getBusinessById(id: businessId)
|
||||
.execute();
|
||||
|
||||
if (businessResult.data.business == null) {
|
||||
throw Exception('Business data not found for ID: $businessId');
|
||||
}
|
||||
|
||||
final dc.ClientSession updatedSession = dc.ClientSession(
|
||||
business: dc.ClientBusinessSession(
|
||||
id: businessResult.data.business!.id,
|
||||
businessName: businessResult.data.business?.businessName ?? '',
|
||||
email: businessResult.data.business?.email ?? '',
|
||||
city: businessResult.data.business?.city ?? '',
|
||||
contactName: businessResult.data.business?.contactName ?? '',
|
||||
companyLogoUrl: businessResult.data.business?.companyLogoUrl,
|
||||
),
|
||||
);
|
||||
dc.ClientSessionStore.instance.setSession(updatedSession);
|
||||
|
||||
return UserSessionData(
|
||||
businessName: businessResult.data.business!.businessName,
|
||||
photoUrl: businessResult.data.business!.companyLogoUrl,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -108,33 +170,34 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
final fdc.Timestamp startTimestamp = _service.toTimestamp(start);
|
||||
final fdc.Timestamp endTimestamp = _service.toTimestamp(now);
|
||||
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
|
||||
await _service.connector
|
||||
.listShiftRolesByBusinessDateRangeCompletedOrders(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
)
|
||||
.execute();
|
||||
final fdc.QueryResult<
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables
|
||||
>
|
||||
result = await _service.connector
|
||||
.listShiftRolesByBusinessDateRangeCompletedOrders(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
)
|
||||
.execute();
|
||||
|
||||
return result.data.shiftRoles
|
||||
.map((
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
) {
|
||||
final String location = shiftRole.shift.location ?? shiftRole.shift.locationAddress ?? '';
|
||||
final String type = shiftRole.shift.order.orderType.stringValue;
|
||||
return ReorderItem(
|
||||
orderId: shiftRole.shift.order.id,
|
||||
title: '${shiftRole.role.name} - ${shiftRole.shift.title}',
|
||||
location: location,
|
||||
hourlyRate: shiftRole.role.costPerHour,
|
||||
hours: shiftRole.hours ?? 0,
|
||||
workers: shiftRole.count,
|
||||
type: type,
|
||||
);
|
||||
})
|
||||
.toList();
|
||||
return result.data.shiftRoles.map((
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
) {
|
||||
final String location =
|
||||
shiftRole.shift.location ?? shiftRole.shift.locationAddress ?? '';
|
||||
final String type = shiftRole.shift.order.orderType.stringValue;
|
||||
return ReorderItem(
|
||||
orderId: shiftRole.shift.order.id,
|
||||
title: '${shiftRole.role.name} - ${shiftRole.shift.title}',
|
||||
location: location,
|
||||
hourlyRate: shiftRole.role.costPerHour,
|
||||
hours: shiftRole.hours ?? 0,
|
||||
workers: shiftRole.count,
|
||||
type: type,
|
||||
);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ abstract interface class HomeRepositoryInterface {
|
||||
Future<HomeDashboardData> getDashboardData();
|
||||
|
||||
/// Fetches the user's session data (business name and photo).
|
||||
UserSessionData getUserSessionData();
|
||||
Future<UserSessionData> getUserSessionData();
|
||||
|
||||
/// Fetches recently completed shift roles for reorder suggestions.
|
||||
Future<List<ReorderItem>> getRecentReorders();
|
||||
|
||||
@@ -10,7 +10,7 @@ class GetUserSessionDataUseCase {
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
/// Executes the use case to get session data.
|
||||
UserSessionData call() {
|
||||
Future<UserSessionData> call() {
|
||||
return _repository.getUserSessionData();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class ClientHomeBloc extends Bloc<ClientHomeEvent, ClientHomeState>
|
||||
emit: emit,
|
||||
action: () async {
|
||||
// Get session data
|
||||
final UserSessionData sessionData = _getUserSessionDataUseCase();
|
||||
final UserSessionData sessionData = await _getUserSessionDataUseCase();
|
||||
|
||||
// Get dashboard data
|
||||
final HomeDashboardData data = await _getDashboardDataUseCase();
|
||||
|
||||
@@ -16,16 +16,16 @@ import '../../domain/repositories/hub_repository_interface.dart';
|
||||
|
||||
/// Implementation of [HubRepositoryInterface] backed by Data Connect.
|
||||
class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
HubRepositoryImpl({
|
||||
required dc.DataConnectService service,
|
||||
}) : _service = service;
|
||||
HubRepositoryImpl({required dc.DataConnectService service})
|
||||
: _service = service;
|
||||
|
||||
final dc.DataConnectService _service;
|
||||
|
||||
@override
|
||||
Future<List<domain.Hub>> getHubs() async {
|
||||
return _service.run(() async {
|
||||
final dc.GetBusinessesByUserIdBusinesses business = await _getBusinessForCurrentUser();
|
||||
final dc.GetBusinessesByUserIdBusinesses business =
|
||||
await _getBusinessForCurrentUser();
|
||||
final String teamId = await _getOrCreateTeamId(business);
|
||||
return _fetchHubsForTeam(teamId: teamId, businessId: business.id);
|
||||
});
|
||||
@@ -45,10 +45,12 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
String? zipCode,
|
||||
}) async {
|
||||
return _service.run(() async {
|
||||
final dc.GetBusinessesByUserIdBusinesses business = await _getBusinessForCurrentUser();
|
||||
final dc.GetBusinessesByUserIdBusinesses business =
|
||||
await _getBusinessForCurrentUser();
|
||||
final String teamId = await _getOrCreateTeamId(business);
|
||||
final _PlaceAddress? placeAddress =
|
||||
placeId == null || placeId.isEmpty ? null : await _fetchPlaceAddress(placeId);
|
||||
final _PlaceAddress? placeAddress = placeId == null || placeId.isEmpty
|
||||
? null
|
||||
: await _fetchPlaceAddress(placeId);
|
||||
final String? cityValue = city ?? placeAddress?.city ?? business.city;
|
||||
final String? stateValue = state ?? placeAddress?.state;
|
||||
final String? streetValue = street ?? placeAddress?.street;
|
||||
@@ -56,21 +58,17 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
final String? zipCodeValue = zipCode ?? placeAddress?.zipCode;
|
||||
|
||||
final OperationResult<dc.CreateTeamHubData, dc.CreateTeamHubVariables>
|
||||
result = await _service.connector
|
||||
.createTeamHub(
|
||||
teamId: teamId,
|
||||
hubName: name,
|
||||
address: address,
|
||||
)
|
||||
.placeId(placeId)
|
||||
.latitude(latitude)
|
||||
.longitude(longitude)
|
||||
.city(cityValue?.isNotEmpty == true ? cityValue : '')
|
||||
.state(stateValue)
|
||||
.street(streetValue)
|
||||
.country(countryValue)
|
||||
.zipCode(zipCodeValue)
|
||||
.execute();
|
||||
result = await _service.connector
|
||||
.createTeamHub(teamId: teamId, hubName: name, address: address)
|
||||
.placeId(placeId)
|
||||
.latitude(latitude)
|
||||
.longitude(longitude)
|
||||
.city(cityValue?.isNotEmpty == true ? cityValue : '')
|
||||
.state(stateValue)
|
||||
.street(streetValue)
|
||||
.country(countryValue)
|
||||
.zipCode(zipCodeValue)
|
||||
.execute();
|
||||
final String createdId = result.data.teamHub_insert.id;
|
||||
|
||||
final List<domain.Hub> hubs = await _fetchHubsForTeam(
|
||||
@@ -101,14 +99,13 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
return _service.run(() async {
|
||||
final String businessId = await _service.getBusinessId();
|
||||
|
||||
final QueryResult<dc.ListOrdersByBusinessAndTeamHubData,
|
||||
dc.ListOrdersByBusinessAndTeamHubVariables> result =
|
||||
await _service.connector
|
||||
.listOrdersByBusinessAndTeamHub(
|
||||
businessId: businessId,
|
||||
teamHubId: id,
|
||||
)
|
||||
.execute();
|
||||
final QueryResult<
|
||||
dc.ListOrdersByBusinessAndTeamHubData,
|
||||
dc.ListOrdersByBusinessAndTeamHubVariables
|
||||
>
|
||||
result = await _service.connector
|
||||
.listOrdersByBusinessAndTeamHub(businessId: businessId, teamHubId: id)
|
||||
.execute();
|
||||
|
||||
if (result.data.orders.isNotEmpty) {
|
||||
throw HubHasOrdersException(
|
||||
@@ -121,14 +118,14 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> assignNfcTag({
|
||||
required String hubId,
|
||||
required String nfcTagId,
|
||||
}) {
|
||||
throw UnimplementedError('NFC tag assignment is not supported for team hubs.');
|
||||
Future<void> assignNfcTag({required String hubId, required String nfcTagId}) {
|
||||
throw UnimplementedError(
|
||||
'NFC tag assignment is not supported for team hubs.',
|
||||
);
|
||||
}
|
||||
|
||||
Future<dc.GetBusinessesByUserIdBusinesses> _getBusinessForCurrentUser() async {
|
||||
Future<dc.GetBusinessesByUserIdBusinesses>
|
||||
_getBusinessForCurrentUser() async {
|
||||
final dc.ClientSession? session = dc.ClientSessionStore.instance.session;
|
||||
final dc.ClientBusinessSession? cachedBusiness = session?.business;
|
||||
if (cachedBusiness != null) {
|
||||
@@ -136,7 +133,9 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
id: cachedBusiness.id,
|
||||
businessName: cachedBusiness.businessName,
|
||||
userId: _service.auth.currentUser?.uid ?? '',
|
||||
rateGroup: const dc.Known<dc.BusinessRateGroup>(dc.BusinessRateGroup.STANDARD),
|
||||
rateGroup: const dc.Known<dc.BusinessRateGroup>(
|
||||
dc.BusinessRateGroup.STANDARD,
|
||||
),
|
||||
status: const dc.Known<dc.BusinessStatus>(dc.BusinessStatus.ACTIVE),
|
||||
contactName: cachedBusiness.contactName,
|
||||
companyLogoUrl: cachedBusiness.companyLogoUrl,
|
||||
@@ -160,11 +159,13 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
);
|
||||
}
|
||||
|
||||
final QueryResult<dc.GetBusinessesByUserIdData,
|
||||
dc.GetBusinessesByUserIdVariables> result =
|
||||
await _service.connector.getBusinessesByUserId(
|
||||
userId: user.uid,
|
||||
).execute();
|
||||
final QueryResult<
|
||||
dc.GetBusinessesByUserIdData,
|
||||
dc.GetBusinessesByUserIdVariables
|
||||
>
|
||||
result = await _service.connector
|
||||
.getBusinessesByUserId(userId: user.uid)
|
||||
.execute();
|
||||
if (result.data.businesses.isEmpty) {
|
||||
await _service.auth.signOut();
|
||||
throw BusinessNotFoundException(
|
||||
@@ -172,12 +173,11 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
);
|
||||
}
|
||||
|
||||
final dc.GetBusinessesByUserIdBusinesses business = result.data.businesses.first;
|
||||
final dc.GetBusinessesByUserIdBusinesses business =
|
||||
result.data.businesses.first;
|
||||
if (session != null) {
|
||||
dc.ClientSessionStore.instance.setSession(
|
||||
dc.ClientSession(
|
||||
user: session.user,
|
||||
userPhotoUrl: session.userPhotoUrl,
|
||||
business: dc.ClientBusinessSession(
|
||||
id: business.id,
|
||||
businessName: business.businessName,
|
||||
@@ -197,26 +197,26 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
dc.GetBusinessesByUserIdBusinesses business,
|
||||
) async {
|
||||
final QueryResult<dc.GetTeamsByOwnerIdData, dc.GetTeamsByOwnerIdVariables>
|
||||
teamsResult = await _service.connector.getTeamsByOwnerId(
|
||||
ownerId: business.id,
|
||||
).execute();
|
||||
teamsResult = await _service.connector
|
||||
.getTeamsByOwnerId(ownerId: business.id)
|
||||
.execute();
|
||||
if (teamsResult.data.teams.isNotEmpty) {
|
||||
return teamsResult.data.teams.first.id;
|
||||
}
|
||||
|
||||
final dc.CreateTeamVariablesBuilder createTeamBuilder = _service.connector.createTeam(
|
||||
teamName: '${business.businessName} Team',
|
||||
ownerId: business.id,
|
||||
ownerName: business.contactName ?? '',
|
||||
ownerRole: 'OWNER',
|
||||
);
|
||||
final dc.CreateTeamVariablesBuilder createTeamBuilder = _service.connector
|
||||
.createTeam(
|
||||
teamName: '${business.businessName} Team',
|
||||
ownerId: business.id,
|
||||
ownerName: business.contactName ?? '',
|
||||
ownerRole: 'OWNER',
|
||||
);
|
||||
if (business.email != null) {
|
||||
createTeamBuilder.email(business.email);
|
||||
}
|
||||
|
||||
final OperationResult<dc.CreateTeamData, dc.CreateTeamVariables>
|
||||
createTeamResult =
|
||||
await createTeamBuilder.execute();
|
||||
createTeamResult = await createTeamBuilder.execute();
|
||||
final String teamId = createTeamResult.data.team_insert.id;
|
||||
|
||||
return teamId;
|
||||
@@ -226,11 +226,13 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
required String teamId,
|
||||
required String businessId,
|
||||
}) async {
|
||||
final QueryResult<dc.GetTeamHubsByTeamIdData,
|
||||
dc.GetTeamHubsByTeamIdVariables> hubsResult =
|
||||
await _service.connector.getTeamHubsByTeamId(
|
||||
teamId: teamId,
|
||||
).execute();
|
||||
final QueryResult<
|
||||
dc.GetTeamHubsByTeamIdData,
|
||||
dc.GetTeamHubsByTeamIdVariables
|
||||
>
|
||||
hubsResult = await _service.connector
|
||||
.getTeamHubsByTeamId(teamId: teamId)
|
||||
.execute();
|
||||
|
||||
return hubsResult.data.teamHubs
|
||||
.map(
|
||||
@@ -240,10 +242,9 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
name: hub.hubName,
|
||||
address: hub.address,
|
||||
nfcTagId: null,
|
||||
status:
|
||||
hub.isActive
|
||||
? domain.HubStatus.active
|
||||
: domain.HubStatus.inactive,
|
||||
status: hub.isActive
|
||||
? domain.HubStatus.active
|
||||
: domain.HubStatus.inactive,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
@@ -288,7 +289,8 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
|
||||
for (final dynamic entry in components) {
|
||||
final Map<String, dynamic> component = entry as Map<String, dynamic>;
|
||||
final List<dynamic> types = component['types'] as List<dynamic>? ?? <dynamic>[];
|
||||
final List<dynamic> types =
|
||||
component['types'] as List<dynamic>? ?? <dynamic>[];
|
||||
final String? longName = component['long_name'] as String?;
|
||||
final String? shortName = component['short_name'] as String?;
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ class SettingsProfileHeader extends StatelessWidget {
|
||||
final dc.ClientSession? session = dc.ClientSessionStore.instance.session;
|
||||
final String businessName =
|
||||
session?.business?.businessName ?? 'Your Company';
|
||||
final String email = session?.user.email ?? 'client@example.com';
|
||||
final String? photoUrl = session?.userPhotoUrl;
|
||||
final String email = session?.business?.email ?? 'client@example.com';
|
||||
final String? photoUrl = session?.business?.companyLogoUrl;
|
||||
final String avatarLetter = businessName.trim().isNotEmpty
|
||||
? businessName.trim()[0].toUpperCase()
|
||||
: 'C';
|
||||
|
||||
Reference in New Issue
Block a user