Merge pull request #384 from Oloodi/feature/centralized-data-error-handling
refactor: centralize data connect error handling and stabilize mobile applications
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
library client_authentication;
|
||||
library;
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart' as firebase;
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
|
||||
@@ -21,9 +21,9 @@ import '../../domain/repositories/auth_repository_interface.dart';
|
||||
///
|
||||
/// This implementation integrates with Firebase Authentication for user
|
||||
/// identity management and Krow's Data Connect SDK for storing user profile data.
|
||||
class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
final firebase.FirebaseAuth _firebaseAuth;
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
class AuthRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements AuthRepositoryInterface {
|
||||
|
||||
/// Creates an [AuthRepositoryImpl] with the real dependencies.
|
||||
AuthRepositoryImpl({
|
||||
@@ -31,6 +31,8 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
required dc.ExampleConnector dataConnect,
|
||||
}) : _firebaseAuth = firebaseAuth,
|
||||
_dataConnect = dataConnect;
|
||||
final firebase.FirebaseAuth _firebaseAuth;
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
|
||||
@override
|
||||
Future<domain.User> signInWithEmail({
|
||||
@@ -221,16 +223,12 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
}
|
||||
|
||||
/// Checks if a user with BUSINESS role exists in PostgreSQL.
|
||||
|
||||
Future<bool> _checkBusinessUserExists(String firebaseUserId) async {
|
||||
try {
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response =
|
||||
await _dataConnect.getUserById(id: firebaseUserId).execute();
|
||||
final dc.GetUserByIdUser? user = response.data?.user;
|
||||
return user != null && user.userRole == 'BUSINESS';
|
||||
} catch (e) {
|
||||
developer.log('Error checking business user: $e', name: 'AuthRepository');
|
||||
return false;
|
||||
}
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response =
|
||||
await executeProtected(() => _dataConnect.getUserById(id: firebaseUserId).execute());
|
||||
final dc.GetUserByIdUser? user = response.data.user;
|
||||
return user != null && user.userRole == 'BUSINESS';
|
||||
}
|
||||
|
||||
/// Creates Business and User entities in PostgreSQL for a Firebase user.
|
||||
@@ -241,38 +239,30 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
required void Function(String businessId) onBusinessCreated,
|
||||
}) async {
|
||||
// Create Business entity in PostgreSQL
|
||||
|
||||
final OperationResult<dc.CreateBusinessData, dc.CreateBusinessVariables> createBusinessResponse =
|
||||
await _dataConnect.createBusiness(
|
||||
await executeProtected(() => _dataConnect.createBusiness(
|
||||
businessName: companyName,
|
||||
userId: firebaseUser.uid,
|
||||
rateGroup: dc.BusinessRateGroup.STANDARD,
|
||||
status: dc.BusinessStatus.PENDING,
|
||||
).execute();
|
||||
).execute());
|
||||
|
||||
final dc.CreateBusinessBusinessInsert? businessData = createBusinessResponse.data?.business_insert;
|
||||
if (businessData == null) {
|
||||
throw const SignUpFailedException(
|
||||
technicalMessage: 'Business creation failed in PostgreSQL',
|
||||
);
|
||||
}
|
||||
final dc.CreateBusinessBusinessInsert businessData = createBusinessResponse.data.business_insert;
|
||||
onBusinessCreated(businessData.id);
|
||||
|
||||
// Create User entity in PostgreSQL
|
||||
|
||||
final OperationResult<dc.CreateUserData, dc.CreateUserVariables> createUserResponse =
|
||||
await _dataConnect.createUser(
|
||||
await executeProtected(() => _dataConnect.createUser(
|
||||
id: firebaseUser.uid,
|
||||
role: dc.UserBaseRole.USER,
|
||||
)
|
||||
.email(email)
|
||||
.userRole('BUSINESS')
|
||||
.execute();
|
||||
.execute());
|
||||
|
||||
final dc.CreateUserUserInsert? newUserData = createUserResponse.data?.user_insert;
|
||||
if (newUserData == null) {
|
||||
throw const SignUpFailedException(
|
||||
technicalMessage: 'User profile creation failed in PostgreSQL',
|
||||
);
|
||||
}
|
||||
final dc.CreateUserUserInsert newUserData = createUserResponse.data.user_insert;
|
||||
|
||||
return _getUserProfile(
|
||||
firebaseUserId: firebaseUser.uid,
|
||||
@@ -323,8 +313,9 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
required String? fallbackEmail,
|
||||
bool requireBusinessRole = false,
|
||||
}) async {
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response = await _dataConnect.getUserById(id: firebaseUserId).execute();
|
||||
final dc.GetUserByIdUser? user = response.data?.user;
|
||||
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response =
|
||||
await executeProtected(() => _dataConnect.getUserById(id: firebaseUserId).execute());
|
||||
final dc.GetUserByIdUser? user = response.data.user;
|
||||
if (user == null) {
|
||||
throw UserNotFoundException(
|
||||
technicalMessage: 'Firebase UID $firebaseUserId not found in users table',
|
||||
@@ -351,9 +342,10 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
role: user.role.stringValue,
|
||||
);
|
||||
|
||||
final QueryResult<dc.GetBusinessesByUserIdData, dc.GetBusinessesByUserIdVariables> businessResponse = await _dataConnect.getBusinessesByUserId(
|
||||
final QueryResult<dc.GetBusinessesByUserIdData, dc.GetBusinessesByUserIdVariables> businessResponse =
|
||||
await executeProtected(() => _dataConnect.getBusinessesByUserId(
|
||||
userId: firebaseUserId,
|
||||
).execute();
|
||||
).execute());
|
||||
final dc.GetBusinessesByUserIdBusinesses? business = businessResponse.data.businesses.isNotEmpty
|
||||
? businessResponse.data.businesses.first
|
||||
: null;
|
||||
|
||||
@@ -2,14 +2,14 @@ import 'package:krow_core/core.dart';
|
||||
|
||||
/// Arguments for the [SignInWithEmailUseCase].
|
||||
class SignInWithEmailArguments extends UseCaseArgument {
|
||||
|
||||
const SignInWithEmailArguments({required this.email, required this.password});
|
||||
/// The user's email address.
|
||||
final String email;
|
||||
|
||||
/// The user's password.
|
||||
final String password;
|
||||
|
||||
const SignInWithEmailArguments({required this.email, required this.password});
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[email, password];
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ import 'package:krow_core/core.dart';
|
||||
|
||||
/// Arguments for the [SignInWithSocialUseCase].
|
||||
class SignInWithSocialArguments extends UseCaseArgument {
|
||||
/// The social provider name (e.g. 'google' or 'apple').
|
||||
final String provider;
|
||||
|
||||
const SignInWithSocialArguments({required this.provider});
|
||||
/// The social provider name (e.g. 'google' or 'apple').
|
||||
final String provider;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[provider];
|
||||
|
||||
@@ -2,6 +2,12 @@ import 'package:krow_core/core.dart';
|
||||
|
||||
/// Arguments for the [SignUpWithEmailUseCase].
|
||||
class SignUpWithEmailArguments extends UseCaseArgument {
|
||||
|
||||
const SignUpWithEmailArguments({
|
||||
required this.companyName,
|
||||
required this.email,
|
||||
required this.password,
|
||||
});
|
||||
/// The name of the company.
|
||||
final String companyName;
|
||||
|
||||
@@ -11,12 +17,6 @@ class SignUpWithEmailArguments extends UseCaseArgument {
|
||||
/// The user's password.
|
||||
final String password;
|
||||
|
||||
const SignUpWithEmailArguments({
|
||||
required this.companyName,
|
||||
required this.email,
|
||||
required this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[companyName, email, password];
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ import '../repositories/auth_repository_interface.dart';
|
||||
/// via email/password credentials.
|
||||
class SignInWithEmailUseCase
|
||||
implements UseCase<SignInWithEmailArguments, User> {
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
const SignInWithEmailUseCase(this._repository);
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
/// Executes the sign-in operation.
|
||||
@override
|
||||
|
||||
@@ -6,9 +6,9 @@ import '../repositories/auth_repository_interface.dart';
|
||||
/// Use case for signing in a client via social providers (Google/Apple).
|
||||
class SignInWithSocialUseCase
|
||||
implements UseCase<SignInWithSocialArguments, User> {
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
const SignInWithSocialUseCase(this._repository);
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
/// Executes the social sign-in operation.
|
||||
@override
|
||||
|
||||
@@ -6,9 +6,9 @@ import '../repositories/auth_repository_interface.dart';
|
||||
/// This use case handles the termination of the user's session and
|
||||
/// clearing of any local authentication tokens.
|
||||
class SignOutUseCase implements NoInputUseCase<void> {
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
const SignOutUseCase(this._repository);
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
/// Executes the sign-out operation.
|
||||
@override
|
||||
|
||||
@@ -9,9 +9,9 @@ import '../repositories/auth_repository_interface.dart';
|
||||
/// email, password, and company details.
|
||||
class SignUpWithEmailUseCase
|
||||
implements UseCase<SignUpWithEmailArguments, User> {
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
const SignUpWithEmailUseCase(this._repository);
|
||||
final AuthRepositoryInterface _repository;
|
||||
|
||||
/// Executes the sign-up operation.
|
||||
@override
|
||||
|
||||
@@ -25,10 +25,6 @@ import 'client_auth_state.dart';
|
||||
/// * Session Termination
|
||||
class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
with BlocErrorHandler<ClientAuthState> {
|
||||
final SignInWithEmailUseCase _signInWithEmail;
|
||||
final SignUpWithEmailUseCase _signUpWithEmail;
|
||||
final SignInWithSocialUseCase _signInWithSocial;
|
||||
final SignOutUseCase _signOut;
|
||||
|
||||
/// Initializes the BLoC with the required use cases and initial state.
|
||||
ClientAuthBloc({
|
||||
@@ -46,6 +42,10 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
on<ClientSocialSignInRequested>(_onSocialSignInRequested);
|
||||
on<ClientSignOutRequested>(_onSignOutRequested);
|
||||
}
|
||||
final SignInWithEmailUseCase _signInWithEmail;
|
||||
final SignUpWithEmailUseCase _signUpWithEmail;
|
||||
final SignInWithSocialUseCase _signInWithSocial;
|
||||
final SignOutUseCase _signOut;
|
||||
|
||||
/// Handles the [ClientSignInRequested] event.
|
||||
Future<void> _onSignInRequested(
|
||||
@@ -57,12 +57,12 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final user = await _signInWithEmail(
|
||||
final User user = await _signInWithEmail(
|
||||
SignInWithEmailArguments(email: event.email, password: event.password),
|
||||
);
|
||||
emit(state.copyWith(status: ClientAuthStatus.authenticated, user: user));
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientAuthStatus.error,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -79,7 +79,7 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final user = await _signUpWithEmail(
|
||||
final User user = await _signUpWithEmail(
|
||||
SignUpWithEmailArguments(
|
||||
companyName: event.companyName,
|
||||
email: event.email,
|
||||
@@ -88,7 +88,7 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
);
|
||||
emit(state.copyWith(status: ClientAuthStatus.authenticated, user: user));
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientAuthStatus.error,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -105,12 +105,12 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final user = await _signInWithSocial(
|
||||
final User user = await _signInWithSocial(
|
||||
SignInWithSocialArguments(provider: event.provider),
|
||||
);
|
||||
emit(state.copyWith(status: ClientAuthStatus.authenticated, user: user));
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientAuthStatus.error,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -130,7 +130,7 @@ class ClientAuthBloc extends Bloc<ClientAuthEvent, ClientAuthState>
|
||||
await _signOut();
|
||||
emit(state.copyWith(status: ClientAuthStatus.signedOut, user: null));
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientAuthStatus.error,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
|
||||
@@ -10,10 +10,10 @@ abstract class ClientAuthEvent extends Equatable {
|
||||
|
||||
/// Event dispatched when a user attempts to sign in with email and password.
|
||||
class ClientSignInRequested extends ClientAuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
const ClientSignInRequested({required this.email, required this.password});
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[email, password];
|
||||
@@ -21,15 +21,15 @@ class ClientSignInRequested extends ClientAuthEvent {
|
||||
|
||||
/// Event dispatched when a user attempts to create a new business account.
|
||||
class ClientSignUpRequested extends ClientAuthEvent {
|
||||
final String companyName;
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
const ClientSignUpRequested({
|
||||
required this.companyName,
|
||||
required this.email,
|
||||
required this.password,
|
||||
});
|
||||
final String companyName;
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[companyName, email, password];
|
||||
@@ -37,9 +37,9 @@ class ClientSignUpRequested extends ClientAuthEvent {
|
||||
|
||||
/// Event dispatched for third-party authentication (Google/Apple).
|
||||
class ClientSocialSignInRequested extends ClientAuthEvent {
|
||||
final String provider;
|
||||
|
||||
const ClientSocialSignInRequested({required this.provider});
|
||||
final String provider;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[provider];
|
||||
|
||||
@@ -21,6 +21,12 @@ enum ClientAuthStatus {
|
||||
|
||||
/// Represents the state of the client authentication flow.
|
||||
class ClientAuthState extends Equatable {
|
||||
|
||||
const ClientAuthState({
|
||||
this.status = ClientAuthStatus.initial,
|
||||
this.user,
|
||||
this.errorMessage,
|
||||
});
|
||||
/// Current status of the authentication process.
|
||||
final ClientAuthStatus status;
|
||||
|
||||
@@ -30,12 +36,6 @@ class ClientAuthState extends Equatable {
|
||||
/// Optional error message when status is [ClientAuthStatus.error].
|
||||
final String? errorMessage;
|
||||
|
||||
const ClientAuthState({
|
||||
this.status = ClientAuthStatus.initial,
|
||||
this.user,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// Creates a copy of this state with the given fields replaced by the new values.
|
||||
ClientAuthState copyWith({
|
||||
ClientAuthStatus? status,
|
||||
|
||||
@@ -11,7 +11,6 @@ import '../blocs/client_auth_event.dart';
|
||||
import '../blocs/client_auth_state.dart';
|
||||
import '../widgets/client_sign_up_page/client_sign_up_form.dart';
|
||||
import '../widgets/common/auth_divider.dart';
|
||||
import '../widgets/common/auth_social_button.dart';
|
||||
|
||||
/// Page for client users to sign up for a new account.
|
||||
///
|
||||
|
||||
@@ -7,12 +7,6 @@ import 'package:flutter/material.dart';
|
||||
/// This widget handles user input for email and password and delegates
|
||||
/// authentication events to the parent via callbacks.
|
||||
class ClientSignInForm extends StatefulWidget {
|
||||
/// Callback when the sign-in button is pressed.
|
||||
final void Function({required String email, required String password})
|
||||
onSignIn;
|
||||
|
||||
/// Whether the authentication is currently loading.
|
||||
final bool isLoading;
|
||||
|
||||
/// Creates a [ClientSignInForm].
|
||||
const ClientSignInForm({
|
||||
@@ -20,6 +14,12 @@ class ClientSignInForm extends StatefulWidget {
|
||||
required this.onSignIn,
|
||||
this.isLoading = false,
|
||||
});
|
||||
/// Callback when the sign-in button is pressed.
|
||||
final void Function({required String email, required String password})
|
||||
onSignIn;
|
||||
|
||||
/// Whether the authentication is currently loading.
|
||||
final bool isLoading;
|
||||
|
||||
@override
|
||||
State<ClientSignInForm> createState() => _ClientSignInFormState();
|
||||
|
||||
@@ -7,6 +7,13 @@ import 'package:flutter/material.dart';
|
||||
/// This widget handles user input for company name, email, and password,
|
||||
/// and delegates registration events to the parent via callbacks.
|
||||
class ClientSignUpForm extends StatefulWidget {
|
||||
|
||||
/// Creates a [ClientSignUpForm].
|
||||
const ClientSignUpForm({
|
||||
super.key,
|
||||
required this.onSignUp,
|
||||
this.isLoading = false,
|
||||
});
|
||||
/// Callback when the sign-up button is pressed.
|
||||
final void Function({
|
||||
required String companyName,
|
||||
@@ -18,13 +25,6 @@ class ClientSignUpForm extends StatefulWidget {
|
||||
/// Whether the authentication is currently loading.
|
||||
final bool isLoading;
|
||||
|
||||
/// Creates a [ClientSignUpForm].
|
||||
const ClientSignUpForm({
|
||||
super.key,
|
||||
required this.onSignUp,
|
||||
this.isLoading = false,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ClientSignUpForm> createState() => _ClientSignUpFormState();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import 'package:flutter/material.dart';
|
||||
///
|
||||
/// Displays a horizontal line with text in the middle (e.g., "Or continue with").
|
||||
class AuthDivider extends StatelessWidget {
|
||||
/// The text to display in the center of the divider.
|
||||
final String text;
|
||||
|
||||
/// Creates an [AuthDivider].
|
||||
const AuthDivider({super.key, required this.text});
|
||||
/// The text to display in the center of the divider.
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -6,14 +6,6 @@ import 'package:flutter/material.dart';
|
||||
/// This widget wraps [UiButton.secondary] to provide a consistent look and feel
|
||||
/// for social sign-in/sign-up buttons (e.g., Google, Apple).
|
||||
class AuthSocialButton extends StatelessWidget {
|
||||
/// The localizable text to display on the button (e.g., "Continue with Google").
|
||||
final String text;
|
||||
|
||||
/// The icon representing the social provider.
|
||||
final IconData icon;
|
||||
|
||||
/// Callback to execute when the button is tapped.
|
||||
final VoidCallback onPressed;
|
||||
|
||||
/// Creates an [AuthSocialButton].
|
||||
///
|
||||
@@ -24,6 +16,14 @@ class AuthSocialButton extends StatelessWidget {
|
||||
required this.icon,
|
||||
required this.onPressed,
|
||||
});
|
||||
/// The localizable text to display on the button (e.g., "Continue with Google").
|
||||
final String text;
|
||||
|
||||
/// The icon representing the social provider.
|
||||
final IconData icon;
|
||||
|
||||
/// Callback to execute when the button is tapped.
|
||||
final VoidCallback onPressed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -3,14 +3,14 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// A widget that displays a section title with a leading icon.
|
||||
class SectionTitle extends StatelessWidget {
|
||||
|
||||
const SectionTitle({super.key, required this.title, required this.subtitle});
|
||||
/// The title of the section.
|
||||
final String title;
|
||||
|
||||
/// The subtitle of the section.
|
||||
final String subtitle;
|
||||
|
||||
const SectionTitle({super.key, required this.title, required this.subtitle});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
|
||||
@@ -16,13 +16,11 @@ import 'presentation/pages/billing_page.dart';
|
||||
class BillingModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
// Mock repositories (TODO: Replace with real implementations)
|
||||
i.addSingleton<FinancialRepositoryMock>(FinancialRepositoryMock.new);
|
||||
|
||||
|
||||
// Repositories
|
||||
i.addSingleton<BillingRepository>(
|
||||
() => BillingRepositoryImpl(
|
||||
financialRepository: i.get<FinancialRepositoryMock>(),
|
||||
dataConnect: ExampleConnector.instance,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -12,28 +12,34 @@ import '../../domain/repositories/billing_repository.dart';
|
||||
/// It strictly adheres to the Clean Architecture data layer responsibilities:
|
||||
/// - No business logic (except necessary data transformation/filtering).
|
||||
/// - Delegates to data sources.
|
||||
class BillingRepositoryImpl implements BillingRepository {
|
||||
class BillingRepositoryImpl
|
||||
with data_connect.DataErrorHandler
|
||||
implements BillingRepository {
|
||||
/// Creates a [BillingRepositoryImpl].
|
||||
///
|
||||
/// Requires the [financialRepository] to fetch financial data.
|
||||
BillingRepositoryImpl({
|
||||
required data_connect.FinancialRepositoryMock financialRepository,
|
||||
required data_connect.ExampleConnector dataConnect,
|
||||
}) : _financialRepository = financialRepository,
|
||||
_dataConnect = dataConnect;
|
||||
}) : _dataConnect = dataConnect;
|
||||
|
||||
final data_connect.FinancialRepositoryMock _financialRepository;
|
||||
final data_connect.ExampleConnector _dataConnect;
|
||||
|
||||
/// Fetches the current bill amount by aggregating open invoices.
|
||||
@override
|
||||
@override
|
||||
Future<double> getCurrentBillAmount() async {
|
||||
// In a real app, this might be an aggregate query.
|
||||
// Simulating fetching invoices and summing up.
|
||||
final List<Invoice> invoices = await _financialRepository.getInvoices(
|
||||
'current_business',
|
||||
);
|
||||
return invoices
|
||||
final String? businessId =
|
||||
data_connect.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
final fdc.QueryResult<data_connect.ListInvoicesByBusinessIdData, data_connect.ListInvoicesByBusinessIdVariables> result = await executeProtected(() => _dataConnect
|
||||
.listInvoicesByBusinessId(businessId: businessId)
|
||||
.execute());
|
||||
|
||||
return result.data.invoices
|
||||
.map(_mapInvoice)
|
||||
.where((Invoice i) => i.status == InvoiceStatus.open)
|
||||
.fold<double>(
|
||||
0.0,
|
||||
@@ -50,25 +56,32 @@ class BillingRepositoryImpl implements BillingRepository {
|
||||
return <Invoice>[];
|
||||
}
|
||||
|
||||
final fdc.QueryResult<data_connect.ListInvoicesByBusinessIdData,
|
||||
data_connect.ListInvoicesByBusinessIdVariables> result =
|
||||
await _dataConnect
|
||||
final fdc.QueryResult<data_connect.ListInvoicesByBusinessIdData, data_connect.ListInvoicesByBusinessIdVariables> result = await executeProtected(() => _dataConnect
|
||||
.listInvoicesByBusinessId(
|
||||
businessId: businessId,
|
||||
)
|
||||
.limit(10)
|
||||
.execute();
|
||||
.execute());
|
||||
|
||||
return result.data.invoices.map(_mapInvoice).toList();
|
||||
}
|
||||
|
||||
/// Fetches pending invoices (Open or Disputed).
|
||||
@override
|
||||
@override
|
||||
Future<List<Invoice>> getPendingInvoices() async {
|
||||
final List<Invoice> invoices = await _financialRepository.getInvoices(
|
||||
'current_business',
|
||||
);
|
||||
return invoices
|
||||
final String? businessId =
|
||||
data_connect.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return <Invoice>[];
|
||||
}
|
||||
|
||||
final fdc.QueryResult<data_connect.ListInvoicesByBusinessIdData, data_connect.ListInvoicesByBusinessIdVariables> result = await executeProtected(() => _dataConnect
|
||||
.listInvoicesByBusinessId(businessId: businessId)
|
||||
.execute());
|
||||
|
||||
return result.data.invoices
|
||||
.map(_mapInvoice)
|
||||
.where(
|
||||
(Invoice i) =>
|
||||
i.status == InvoiceStatus.open ||
|
||||
@@ -111,16 +124,13 @@ class BillingRepositoryImpl implements BillingRepository {
|
||||
end = DateTime(now.year, now.month + 1, 0, 23, 59, 59, 999);
|
||||
}
|
||||
|
||||
final fdc.QueryResult<
|
||||
data_connect.ListShiftRolesByBusinessAndDatesSummaryData,
|
||||
data_connect.ListShiftRolesByBusinessAndDatesSummaryVariables> result =
|
||||
await _dataConnect
|
||||
final fdc.QueryResult<data_connect.ListShiftRolesByBusinessAndDatesSummaryData, data_connect.ListShiftRolesByBusinessAndDatesSummaryVariables> result = await executeProtected(() => _dataConnect
|
||||
.listShiftRolesByBusinessAndDatesSummary(
|
||||
businessId: businessId,
|
||||
start: _toTimestamp(start),
|
||||
end: _toTimestamp(end),
|
||||
)
|
||||
.execute();
|
||||
.execute());
|
||||
|
||||
final List<data_connect.ListShiftRolesByBusinessAndDatesSummaryShiftRoles>
|
||||
shiftRoles = result.data.shiftRoles;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import '../../domain/models/billing_period.dart';
|
||||
import '../../domain/usecases/get_current_bill_amount.dart';
|
||||
import '../../domain/usecases/get_invoice_history.dart';
|
||||
import '../../domain/usecases/get_pending_invoices.dart';
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import 'package:client_main/src/presentation/blocs/client_main_cubit.dart';
|
||||
import 'package:client_main/src/presentation/blocs/client_main_state.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockIModularNavigator extends Mock implements IModularNavigator {}
|
||||
|
||||
void main() {
|
||||
group('ClientMainCubit', () {
|
||||
late MockIModularNavigator navigator;
|
||||
|
||||
setUp(() {
|
||||
navigator = MockIModularNavigator();
|
||||
when(() => navigator.path).thenReturn('/home');
|
||||
when(() => navigator.addListener(any())).thenReturn(null);
|
||||
// Stub addListener to avoid errors when Cubit adds listener
|
||||
// Note: addListener might be on Modular directly or via some other mechanic,
|
||||
// but for this unit test we just want to suppress errors if possible or let the Cubit work.
|
||||
// Actually Modular.to.addListener calls Modular.navigatorDelegate.addListener if it exists?
|
||||
// Modular.to.addListener uses the internal RouterDelegate.
|
||||
// Mocking Modular internals is hard.
|
||||
|
||||
// Let's rely on the fact that we mocked navigatorDelegate.
|
||||
Modular.navigatorDelegate = navigator;
|
||||
});
|
||||
|
||||
test('initial state is correct', () {
|
||||
final ClientMainCubit cubit = ClientMainCubit();
|
||||
expect(cubit.state, const ClientMainState(currentIndex: 2));
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
// Note: Testing actual route changes requires more complex Modular mocking
|
||||
// or integration tests, but the structure allows it.
|
||||
});
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import '../../domain/repositories/client_create_order_repository_interface.dart'
|
||||
/// It follows the KROW Clean Architecture by keeping the data layer focused
|
||||
/// on delegation and data mapping, without business logic.
|
||||
class ClientCreateOrderRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements ClientCreateOrderRepositoryInterface {
|
||||
ClientCreateOrderRepositoryImpl({
|
||||
required firebase.FirebaseAuth firebaseAuth,
|
||||
@@ -74,22 +75,20 @@ class ClientCreateOrderRepositoryImpl
|
||||
order.date.day,
|
||||
);
|
||||
final fdc.Timestamp orderTimestamp = _toTimestamp(orderDateOnly);
|
||||
final fdc.OperationResult<dc.CreateOrderData, dc.CreateOrderVariables> orderResult = await _dataConnect
|
||||
.createOrder(
|
||||
businessId: businessId,
|
||||
orderType: dc.OrderType.ONE_TIME,
|
||||
teamHubId: hub.id,
|
||||
)
|
||||
.vendorId(vendorId)
|
||||
.eventName(order.eventName)
|
||||
.status(dc.OrderStatus.POSTED)
|
||||
.date(orderTimestamp)
|
||||
.execute();
|
||||
final fdc.OperationResult<dc.CreateOrderData, dc.CreateOrderVariables>
|
||||
orderResult = await executeProtected(() => _dataConnect
|
||||
.createOrder(
|
||||
businessId: businessId,
|
||||
orderType: dc.OrderType.ONE_TIME,
|
||||
teamHubId: hub.id,
|
||||
)
|
||||
.vendorId(vendorId)
|
||||
.eventName(order.eventName)
|
||||
.status(dc.OrderStatus.POSTED)
|
||||
.date(orderTimestamp)
|
||||
.execute());
|
||||
|
||||
final String? orderId = orderResult.data?.order_insert.id;
|
||||
if (orderId == null) {
|
||||
throw Exception('Order creation failed.');
|
||||
}
|
||||
final String orderId = orderResult.data.order_insert.id;
|
||||
|
||||
final int workersNeeded = order.positions.fold<int>(
|
||||
0,
|
||||
@@ -98,29 +97,27 @@ class ClientCreateOrderRepositoryImpl
|
||||
final String shiftTitle = 'Shift 1 ${_formatDate(order.date)}';
|
||||
final double shiftCost = _calculateShiftCost(order);
|
||||
|
||||
final fdc.OperationResult<dc.CreateShiftData, dc.CreateShiftVariables> shiftResult = await _dataConnect
|
||||
.createShift(title: shiftTitle, orderId: orderId)
|
||||
.date(orderTimestamp)
|
||||
.location(hub.name)
|
||||
.locationAddress(hub.address)
|
||||
.latitude(hub.latitude)
|
||||
.longitude(hub.longitude)
|
||||
.placeId(hub.placeId)
|
||||
.city(hub.city)
|
||||
.state(hub.state)
|
||||
.street(hub.street)
|
||||
.country(hub.country)
|
||||
.status(dc.ShiftStatus.PENDING)
|
||||
.workersNeeded(workersNeeded)
|
||||
.filled(0)
|
||||
.durationDays(1)
|
||||
.cost(shiftCost)
|
||||
.execute();
|
||||
final fdc.OperationResult<dc.CreateShiftData, dc.CreateShiftVariables>
|
||||
shiftResult = await executeProtected(() => _dataConnect
|
||||
.createShift(title: shiftTitle, orderId: orderId)
|
||||
.date(orderTimestamp)
|
||||
.location(hub.name)
|
||||
.locationAddress(hub.address)
|
||||
.latitude(hub.latitude)
|
||||
.longitude(hub.longitude)
|
||||
.placeId(hub.placeId)
|
||||
.city(hub.city)
|
||||
.state(hub.state)
|
||||
.street(hub.street)
|
||||
.country(hub.country)
|
||||
.status(dc.ShiftStatus.PENDING)
|
||||
.workersNeeded(workersNeeded)
|
||||
.filled(0)
|
||||
.durationDays(1)
|
||||
.cost(shiftCost)
|
||||
.execute());
|
||||
|
||||
final String? shiftId = shiftResult.data?.shift_insert.id;
|
||||
if (shiftId == null) {
|
||||
throw Exception('Shift creation failed.');
|
||||
}
|
||||
final String shiftId = shiftResult.data.shift_insert.id;
|
||||
|
||||
for (final domain.OneTimeOrderPosition position in order.positions) {
|
||||
final DateTime start = _parseTime(order.date, position.startTime);
|
||||
@@ -135,7 +132,7 @@ class ClientCreateOrderRepositoryImpl
|
||||
'CreateOneTimeOrder shiftRole: start=${start.toIso8601String()} end=${normalizedEnd.toIso8601String()}',
|
||||
);
|
||||
|
||||
await _dataConnect
|
||||
await executeProtected(() => _dataConnect
|
||||
.createShiftRole(
|
||||
shiftId: shiftId,
|
||||
roleId: position.role,
|
||||
@@ -146,13 +143,13 @@ class ClientCreateOrderRepositoryImpl
|
||||
.hours(hours)
|
||||
.breakType(_breakDurationFromValue(position.lunchBreak))
|
||||
.totalValue(totalValue)
|
||||
.execute();
|
||||
.execute());
|
||||
}
|
||||
|
||||
await _dataConnect
|
||||
await executeProtected(() => _dataConnect
|
||||
.updateOrder(id: orderId, teamHubId: hub.id)
|
||||
.shifts(fdc.AnyValue(<String>[shiftId]))
|
||||
.execute();
|
||||
.execute());
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -25,7 +25,6 @@ class ClientHomeModule extends Module {
|
||||
// Repositories
|
||||
i.addLazySingleton<HomeRepositoryInterface>(
|
||||
() => HomeRepositoryImpl(
|
||||
i.get<HomeRepositoryMock>(),
|
||||
ExampleConnector.instance,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart' as fdc;
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart' as dc;
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import '../../domain/repositories/home_repository_interface.dart';
|
||||
|
||||
@@ -8,17 +8,14 @@ import '../../domain/repositories/home_repository_interface.dart';
|
||||
/// This implementation resides in the data layer and acts as a bridge between the
|
||||
/// domain layer and the data source (in this case, a mock from data_connect).
|
||||
class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
final HomeRepositoryMock _mock;
|
||||
final ExampleConnector _dataConnect;
|
||||
|
||||
/// Creates a [HomeRepositoryImpl].
|
||||
///
|
||||
/// Requires a [HomeRepositoryMock] to perform data operations.
|
||||
HomeRepositoryImpl(this._mock, this._dataConnect);
|
||||
HomeRepositoryImpl(this._dataConnect);
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
|
||||
@override
|
||||
Future<HomeDashboardData> getDashboardData() async {
|
||||
final String? businessId = ClientSessionStore.instance.session?.business?.id;
|
||||
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return const HomeDashboardData(
|
||||
weeklySpending: 0,
|
||||
@@ -38,8 +35,8 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
final DateTime weekRangeEnd =
|
||||
DateTime(monday.year, monday.month, monday.day + 13, 23, 59, 59, 999);
|
||||
final fdc.QueryResult<
|
||||
GetCompletedShiftsByBusinessIdData,
|
||||
GetCompletedShiftsByBusinessIdVariables> completedResult =
|
||||
dc.GetCompletedShiftsByBusinessIdData,
|
||||
dc.GetCompletedShiftsByBusinessIdVariables> completedResult =
|
||||
await _dataConnect
|
||||
.getCompletedShiftsByBusinessId(
|
||||
businessId: businessId,
|
||||
@@ -47,16 +44,13 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
dateTo: _toTimestamp(weekRangeEnd),
|
||||
)
|
||||
.execute();
|
||||
print(
|
||||
'Home spending: businessId=$businessId dateFrom=${weekRangeStart.toIso8601String()} '
|
||||
'dateTo=${weekRangeEnd.toIso8601String()} shifts=${completedResult.data.shifts.length}',
|
||||
);
|
||||
|
||||
|
||||
double weeklySpending = 0.0;
|
||||
double next7DaysSpending = 0.0;
|
||||
int weeklyShifts = 0;
|
||||
int next7DaysScheduled = 0;
|
||||
for (final GetCompletedShiftsByBusinessIdShifts shift
|
||||
for (final dc.GetCompletedShiftsByBusinessIdShifts shift
|
||||
in completedResult.data.shifts) {
|
||||
final DateTime? shiftDate = shift.date?.toDateTime();
|
||||
if (shiftDate == null) {
|
||||
@@ -80,8 +74,8 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
final DateTime end = DateTime(now.year, now.month, now.day, 23, 59, 59, 999);
|
||||
|
||||
final fdc.QueryResult<
|
||||
ListShiftRolesByBusinessAndDateRangeData,
|
||||
ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
await _dataConnect
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
@@ -89,18 +83,11 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
end: _toTimestamp(end),
|
||||
)
|
||||
.execute();
|
||||
print(
|
||||
'Home coverage: businessId=$businessId '
|
||||
'startLocal=${start.toIso8601String()} '
|
||||
'endLocal=${end.toIso8601String()} '
|
||||
'startUtc=${_toTimestamp(start).toJson()} '
|
||||
'endUtc=${_toTimestamp(end).toJson()} '
|
||||
'shiftRoles=${result.data.shiftRoles.length}',
|
||||
);
|
||||
|
||||
|
||||
int totalNeeded = 0;
|
||||
int totalFilled = 0;
|
||||
for (final ListShiftRolesByBusinessAndDateRangeShiftRoles shiftRole
|
||||
for (final dc.ListShiftRolesByBusinessAndDateRangeShiftRoles shiftRole
|
||||
in result.data.shiftRoles) {
|
||||
totalNeeded += shiftRole.count;
|
||||
totalFilled += shiftRole.assigned ?? 0;
|
||||
@@ -118,16 +105,16 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
|
||||
@override
|
||||
UserSessionData getUserSessionData() {
|
||||
final (String businessName, String? photoUrl) = _mock.getUserSession();
|
||||
final dc.ClientSession? session = dc.ClientSessionStore.instance.session;
|
||||
return UserSessionData(
|
||||
businessName: businessName,
|
||||
photoUrl: photoUrl,
|
||||
businessName: session?.business?.businessName ?? '',
|
||||
photoUrl: null, // Business photo isn't currently in session
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ReorderItem>> getRecentReorders() async {
|
||||
final String? businessId = ClientSessionStore.instance.session?.business?.id;
|
||||
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return const <ReorderItem>[];
|
||||
}
|
||||
@@ -138,27 +125,20 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
final fdc.Timestamp endTimestamp = _toTimestamp(now);
|
||||
|
||||
final fdc.QueryResult<
|
||||
ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
|
||||
await _dataConnect.listShiftRolesByBusinessDateRangeCompletedOrders(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
).execute();
|
||||
|
||||
print(
|
||||
'Home reorder: completed shiftRoles=${result.data.shiftRoles.length}',
|
||||
);
|
||||
|
||||
|
||||
return result.data.shiftRoles.map((
|
||||
ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
) {
|
||||
print(
|
||||
'Home reorder item: orderId=${shiftRole.shift.order.id} '
|
||||
'shiftId=${shiftRole.shiftId} roleId=${shiftRole.roleId} '
|
||||
'orderType=${shiftRole.shift.order.orderType.stringValue} '
|
||||
'hours=${shiftRole.hours} count=${shiftRole.count}',
|
||||
);
|
||||
|
||||
final String location =
|
||||
shiftRole.shift.location ??
|
||||
shiftRole.shift.locationAddress ??
|
||||
|
||||
@@ -2,17 +2,17 @@ import 'package:krow_domain/krow_domain.dart';
|
||||
|
||||
/// User session data for the home page.
|
||||
class UserSessionData {
|
||||
/// The business name of the logged-in user.
|
||||
final String businessName;
|
||||
|
||||
/// The photo URL of the logged-in user (optional).
|
||||
final String? photoUrl;
|
||||
|
||||
/// Creates a [UserSessionData].
|
||||
const UserSessionData({
|
||||
required this.businessName,
|
||||
this.photoUrl,
|
||||
});
|
||||
/// The business name of the logged-in user.
|
||||
final String businessName;
|
||||
|
||||
/// The photo URL of the logged-in user (optional).
|
||||
final String? photoUrl;
|
||||
}
|
||||
|
||||
/// Interface for the Client Home repository.
|
||||
|
||||
@@ -7,10 +7,10 @@ import '../repositories/home_repository_interface.dart';
|
||||
/// This use case coordinates with the [HomeRepositoryInterface] to retrieve
|
||||
/// the [HomeDashboardData] required for the dashboard display.
|
||||
class GetDashboardDataUseCase implements NoInputUseCase<HomeDashboardData> {
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [GetDashboardDataUseCase].
|
||||
GetDashboardDataUseCase(this._repository);
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<HomeDashboardData> call() {
|
||||
|
||||
@@ -4,10 +4,10 @@ import '../repositories/home_repository_interface.dart';
|
||||
|
||||
/// Use case to fetch recent completed shift roles for reorder suggestions.
|
||||
class GetRecentReordersUseCase implements NoInputUseCase<List<ReorderItem>> {
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [GetRecentReordersUseCase].
|
||||
GetRecentReordersUseCase(this._repository);
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<List<ReorderItem>> call() {
|
||||
|
||||
@@ -4,10 +4,10 @@ import '../repositories/home_repository_interface.dart';
|
||||
///
|
||||
/// Returns the user's business name and photo URL for display in the header.
|
||||
class GetUserSessionDataUseCase {
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [GetUserSessionDataUseCase].
|
||||
GetUserSessionDataUseCase(this._repository);
|
||||
final HomeRepositoryInterface _repository;
|
||||
|
||||
/// Executes the use case to get session data.
|
||||
UserSessionData call() {
|
||||
|
||||
@@ -9,9 +9,6 @@ import 'client_home_state.dart';
|
||||
|
||||
/// BLoC responsible for managing the state and business logic of the client home dashboard.
|
||||
class ClientHomeBloc extends Bloc<ClientHomeEvent, ClientHomeState> {
|
||||
final GetDashboardDataUseCase _getDashboardDataUseCase;
|
||||
final GetRecentReordersUseCase _getRecentReordersUseCase;
|
||||
final GetUserSessionDataUseCase _getUserSessionDataUseCase;
|
||||
|
||||
ClientHomeBloc({
|
||||
required GetDashboardDataUseCase getDashboardDataUseCase,
|
||||
@@ -29,6 +26,9 @@ class ClientHomeBloc extends Bloc<ClientHomeEvent, ClientHomeState> {
|
||||
|
||||
add(ClientHomeStarted());
|
||||
}
|
||||
final GetDashboardDataUseCase _getDashboardDataUseCase;
|
||||
final GetRecentReordersUseCase _getRecentReordersUseCase;
|
||||
final GetUserSessionDataUseCase _getUserSessionDataUseCase;
|
||||
|
||||
Future<void> _onStarted(
|
||||
ClientHomeStarted event,
|
||||
@@ -83,7 +83,7 @@ class ClientHomeBloc extends Bloc<ClientHomeEvent, ClientHomeState> {
|
||||
Emitter<ClientHomeState> emit,
|
||||
) {
|
||||
final List<String> newList = List<String>.from(state.widgetOrder);
|
||||
int oldIndex = event.oldIndex;
|
||||
final int oldIndex = event.oldIndex;
|
||||
int newIndex = event.newIndex;
|
||||
|
||||
if (oldIndex < newIndex) {
|
||||
|
||||
@@ -12,17 +12,17 @@ class ClientHomeStarted extends ClientHomeEvent {}
|
||||
class ClientHomeEditModeToggled extends ClientHomeEvent {}
|
||||
|
||||
class ClientHomeWidgetVisibilityToggled extends ClientHomeEvent {
|
||||
final String widgetId;
|
||||
const ClientHomeWidgetVisibilityToggled(this.widgetId);
|
||||
final String widgetId;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[widgetId];
|
||||
}
|
||||
|
||||
class ClientHomeWidgetReordered extends ClientHomeEvent {
|
||||
const ClientHomeWidgetReordered(this.oldIndex, this.newIndex);
|
||||
final int oldIndex;
|
||||
final int newIndex;
|
||||
const ClientHomeWidgetReordered(this.oldIndex, this.newIndex);
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[oldIndex, newIndex];
|
||||
|
||||
@@ -6,15 +6,6 @@ enum ClientHomeStatus { initial, loading, success, error }
|
||||
|
||||
/// Represents the state of the client home dashboard.
|
||||
class ClientHomeState extends Equatable {
|
||||
final ClientHomeStatus status;
|
||||
final List<String> widgetOrder;
|
||||
final Map<String, bool> widgetVisibility;
|
||||
final bool isEditMode;
|
||||
final String? errorMessage;
|
||||
final HomeDashboardData dashboardData;
|
||||
final List<ReorderItem> reorderItems;
|
||||
final String businessName;
|
||||
final String? photoUrl;
|
||||
|
||||
const ClientHomeState({
|
||||
this.status = ClientHomeStatus.initial,
|
||||
@@ -46,6 +37,15 @@ class ClientHomeState extends Equatable {
|
||||
this.businessName = 'Your Company',
|
||||
this.photoUrl,
|
||||
});
|
||||
final ClientHomeStatus status;
|
||||
final List<String> widgetOrder;
|
||||
final Map<String, bool> widgetVisibility;
|
||||
final bool isEditMode;
|
||||
final String? errorMessage;
|
||||
final HomeDashboardData dashboardData;
|
||||
final List<ReorderItem> reorderItems;
|
||||
final String businessName;
|
||||
final String? photoUrl;
|
||||
|
||||
ClientHomeState copyWith({
|
||||
ClientHomeStatus? status,
|
||||
|
||||
@@ -4,14 +4,6 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// A widget that displays quick actions for the client.
|
||||
class ActionsWidget extends StatelessWidget {
|
||||
/// Callback when RAPID is pressed.
|
||||
final VoidCallback onRapidPressed;
|
||||
|
||||
/// Callback when Create Order is pressed.
|
||||
final VoidCallback onCreateOrderPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
/// Creates an [ActionsWidget].
|
||||
const ActionsWidget({
|
||||
@@ -20,6 +12,14 @@ class ActionsWidget extends StatelessWidget {
|
||||
required this.onCreateOrderPressed,
|
||||
this.subtitle,
|
||||
});
|
||||
/// Callback when RAPID is pressed.
|
||||
final VoidCallback onRapidPressed;
|
||||
|
||||
/// Callback when Create Order is pressed.
|
||||
final VoidCallback onCreateOrderPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -69,16 +69,6 @@ class ActionsWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _ActionCard extends StatelessWidget {
|
||||
final String title;
|
||||
final String subtitle;
|
||||
final IconData icon;
|
||||
final Color color;
|
||||
final Color borderColor;
|
||||
final Color iconBgColor;
|
||||
final Color iconColor;
|
||||
final Color textColor;
|
||||
final Color subtitleColor;
|
||||
final VoidCallback onTap;
|
||||
|
||||
const _ActionCard({
|
||||
required this.title,
|
||||
@@ -92,6 +82,16 @@ class _ActionCard extends StatelessWidget {
|
||||
required this.subtitleColor,
|
||||
required this.onTap,
|
||||
});
|
||||
final String title;
|
||||
final String subtitle;
|
||||
final IconData icon;
|
||||
final Color color;
|
||||
final Color borderColor;
|
||||
final Color iconBgColor;
|
||||
final Color iconColor;
|
||||
final Color textColor;
|
||||
final Color subtitleColor;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -10,14 +10,14 @@ import '../blocs/client_home_state.dart';
|
||||
/// Shows instructions for reordering widgets and provides a reset button
|
||||
/// to restore the default layout.
|
||||
class ClientHomeEditBanner extends StatelessWidget {
|
||||
/// The internationalization object for localized strings.
|
||||
final dynamic i18n;
|
||||
|
||||
/// Creates a [ClientHomeEditBanner].
|
||||
const ClientHomeEditBanner({
|
||||
required this.i18n,
|
||||
super.key,
|
||||
});
|
||||
/// The internationalization object for localized strings.
|
||||
final dynamic i18n;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -13,14 +13,14 @@ import 'header_icon_button.dart';
|
||||
/// Displays the user's business name, avatar, and action buttons
|
||||
/// (edit mode, notifications, settings).
|
||||
class ClientHomeHeader extends StatelessWidget {
|
||||
/// The internationalization object for localized strings.
|
||||
final dynamic i18n;
|
||||
|
||||
/// Creates a [ClientHomeHeader].
|
||||
const ClientHomeHeader({
|
||||
required this.i18n,
|
||||
super.key,
|
||||
});
|
||||
/// The internationalization object for localized strings.
|
||||
final dynamic i18n;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -3,11 +3,6 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// A dashboard widget that displays today's coverage status.
|
||||
class CoverageDashboard extends StatelessWidget {
|
||||
/// The list of shifts for today.
|
||||
final List<dynamic> shifts;
|
||||
|
||||
/// The list of applications for today's shifts.
|
||||
final List<dynamic> applications;
|
||||
|
||||
/// Creates a [CoverageDashboard].
|
||||
const CoverageDashboard({
|
||||
@@ -15,6 +10,11 @@ class CoverageDashboard extends StatelessWidget {
|
||||
required this.shifts,
|
||||
required this.applications,
|
||||
});
|
||||
/// The list of shifts for today.
|
||||
final List<dynamic> shifts;
|
||||
|
||||
/// The list of applications for today's shifts.
|
||||
final List<dynamic> applications;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -145,12 +145,6 @@ class CoverageDashboard extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _StatusCard extends StatelessWidget {
|
||||
final String label;
|
||||
final String value;
|
||||
final IconData icon;
|
||||
final bool isWarning;
|
||||
final bool isError;
|
||||
final bool isInfo;
|
||||
|
||||
const _StatusCard({
|
||||
required this.label,
|
||||
@@ -160,6 +154,12 @@ class _StatusCard extends StatelessWidget {
|
||||
this.isError = false,
|
||||
this.isInfo = false,
|
||||
});
|
||||
final String label;
|
||||
final String value;
|
||||
final IconData icon;
|
||||
final bool isWarning;
|
||||
final bool isError;
|
||||
final bool isInfo;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -3,6 +3,15 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// A widget that displays the daily coverage metrics.
|
||||
class CoverageWidget extends StatelessWidget {
|
||||
|
||||
/// Creates a [CoverageWidget].
|
||||
const CoverageWidget({
|
||||
super.key,
|
||||
this.totalNeeded = 0,
|
||||
this.totalConfirmed = 0,
|
||||
this.coveragePercent = 0,
|
||||
this.subtitle,
|
||||
});
|
||||
/// The total number of shifts needed.
|
||||
final int totalNeeded;
|
||||
|
||||
@@ -15,15 +24,6 @@ class CoverageWidget extends StatelessWidget {
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
/// Creates a [CoverageWidget].
|
||||
const CoverageWidget({
|
||||
super.key,
|
||||
this.totalNeeded = 0,
|
||||
this.totalConfirmed = 0,
|
||||
this.coveragePercent = 0,
|
||||
this.subtitle,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Color backgroundColor;
|
||||
@@ -114,11 +114,6 @@ class CoverageWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _MetricCard extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final Color iconColor;
|
||||
final String label;
|
||||
final String value;
|
||||
final Color? valueColor;
|
||||
|
||||
const _MetricCard({
|
||||
required this.icon,
|
||||
@@ -127,6 +122,11 @@ class _MetricCard extends StatelessWidget {
|
||||
required this.value,
|
||||
this.valueColor,
|
||||
});
|
||||
final IconData icon;
|
||||
final Color iconColor;
|
||||
final String label;
|
||||
final String value;
|
||||
final Color? valueColor;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -16,14 +16,6 @@ import 'client_home_sheets.dart';
|
||||
/// This widget encapsulates the logic for rendering different dashboard
|
||||
/// widgets based on their unique identifiers and current state.
|
||||
class DashboardWidgetBuilder extends StatelessWidget {
|
||||
/// The unique identifier for the widget to build.
|
||||
final String id;
|
||||
|
||||
/// The current dashboard state.
|
||||
final ClientHomeState state;
|
||||
|
||||
/// Whether the widget is in edit mode.
|
||||
final bool isEditMode;
|
||||
|
||||
/// Creates a [DashboardWidgetBuilder].
|
||||
const DashboardWidgetBuilder({
|
||||
@@ -32,6 +24,14 @@ class DashboardWidgetBuilder extends StatelessWidget {
|
||||
required this.isEditMode,
|
||||
super.key,
|
||||
});
|
||||
/// The unique identifier for the widget to build.
|
||||
final String id;
|
||||
|
||||
/// The current dashboard state.
|
||||
final ClientHomeState state;
|
||||
|
||||
/// Whether the widget is in edit mode.
|
||||
final bool isEditMode;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -9,6 +9,15 @@ import '../blocs/client_home_event.dart';
|
||||
/// Displays drag handles, visibility toggles, and wraps the actual widget
|
||||
/// content with appropriate styling for the edit state.
|
||||
class DraggableWidgetWrapper extends StatelessWidget {
|
||||
|
||||
/// Creates a [DraggableWidgetWrapper].
|
||||
const DraggableWidgetWrapper({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.child,
|
||||
required this.isVisible,
|
||||
super.key,
|
||||
});
|
||||
/// The unique identifier for this widget.
|
||||
final String id;
|
||||
|
||||
@@ -21,15 +30,6 @@ class DraggableWidgetWrapper extends StatelessWidget {
|
||||
/// Whether this widget is currently visible.
|
||||
final bool isVisible;
|
||||
|
||||
/// Creates a [DraggableWidgetWrapper].
|
||||
const DraggableWidgetWrapper({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.child,
|
||||
required this.isVisible,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
|
||||
@@ -6,6 +6,15 @@ import 'package:flutter/material.dart';
|
||||
/// Supports an optional badge for notification counts and an active state
|
||||
/// for toggled actions.
|
||||
class HeaderIconButton extends StatelessWidget {
|
||||
|
||||
/// Creates a [HeaderIconButton].
|
||||
const HeaderIconButton({
|
||||
required this.icon,
|
||||
this.badgeText,
|
||||
this.isActive = false,
|
||||
required this.onTap,
|
||||
super.key,
|
||||
});
|
||||
/// The icon to display.
|
||||
final IconData icon;
|
||||
|
||||
@@ -18,15 +27,6 @@ class HeaderIconButton extends StatelessWidget {
|
||||
/// Callback invoked when the button is tapped.
|
||||
final VoidCallback onTap;
|
||||
|
||||
/// Creates a [HeaderIconButton].
|
||||
const HeaderIconButton({
|
||||
required this.icon,
|
||||
this.badgeText,
|
||||
this.isActive = false,
|
||||
required this.onTap,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
|
||||
@@ -7,11 +7,6 @@ import 'coverage_dashboard.dart';
|
||||
|
||||
/// A widget that displays live activity information.
|
||||
class LiveActivityWidget extends StatefulWidget {
|
||||
/// Callback when "View all" is pressed.
|
||||
final VoidCallback onViewAllPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
/// Creates a [LiveActivityWidget].
|
||||
const LiveActivityWidget({
|
||||
@@ -19,6 +14,11 @@ class LiveActivityWidget extends StatefulWidget {
|
||||
required this.onViewAllPressed,
|
||||
this.subtitle
|
||||
});
|
||||
/// Callback when "View all" is pressed.
|
||||
final VoidCallback onViewAllPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
@override
|
||||
State<LiveActivityWidget> createState() => _LiveActivityWidgetState();
|
||||
@@ -178,6 +178,16 @@ class _LiveActivityWidgetState extends State<LiveActivityWidget> {
|
||||
}
|
||||
|
||||
class _LiveActivityData {
|
||||
|
||||
factory _LiveActivityData.empty() {
|
||||
return const _LiveActivityData(
|
||||
totalNeeded: 0,
|
||||
totalAssigned: 0,
|
||||
totalCost: 0,
|
||||
checkedInCount: 0,
|
||||
lateCount: 0,
|
||||
);
|
||||
}
|
||||
const _LiveActivityData({
|
||||
required this.totalNeeded,
|
||||
required this.totalAssigned,
|
||||
@@ -191,14 +201,4 @@ class _LiveActivityData {
|
||||
final double totalCost;
|
||||
final int checkedInCount;
|
||||
final int lateCount;
|
||||
|
||||
factory _LiveActivityData.empty() {
|
||||
return const _LiveActivityData(
|
||||
totalNeeded: 0,
|
||||
totalAssigned: 0,
|
||||
totalCost: 0,
|
||||
checkedInCount: 0,
|
||||
lateCount: 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,6 @@ import 'package:krow_domain/krow_domain.dart';
|
||||
|
||||
/// A widget that allows clients to reorder recent shifts.
|
||||
class ReorderWidget extends StatelessWidget {
|
||||
/// Recent completed orders for reorder.
|
||||
final List<ReorderItem> orders;
|
||||
|
||||
/// Callback when a reorder button is pressed.
|
||||
final Function(Map<String, dynamic> shiftData) onReorderPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
/// Creates a [ReorderWidget].
|
||||
const ReorderWidget({
|
||||
@@ -21,6 +13,14 @@ class ReorderWidget extends StatelessWidget {
|
||||
required this.onReorderPressed,
|
||||
this.subtitle,
|
||||
});
|
||||
/// Recent completed orders for reorder.
|
||||
final List<ReorderItem> orders;
|
||||
|
||||
/// Callback when a reorder button is pressed.
|
||||
final Function(Map<String, dynamic> shiftData) onReorderPressed;
|
||||
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -177,11 +177,6 @@ class ReorderWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _Badge extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String text;
|
||||
final Color color;
|
||||
final Color bg;
|
||||
final Color textColor;
|
||||
|
||||
const _Badge({
|
||||
required this.icon,
|
||||
@@ -190,6 +185,11 @@ class _Badge extends StatelessWidget {
|
||||
required this.bg,
|
||||
required this.textColor,
|
||||
});
|
||||
final IconData icon;
|
||||
final String text;
|
||||
final Color color;
|
||||
final Color bg;
|
||||
final Color textColor;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -29,14 +29,6 @@ class _VendorOption {
|
||||
/// This widget provides a comprehensive form matching the design patterns
|
||||
/// used in view_order_card.dart for consistency across the app.
|
||||
class ShiftOrderFormSheet extends StatefulWidget {
|
||||
/// Initial data for the form (e.g. from a reorder action).
|
||||
final Map<String, dynamic>? initialData;
|
||||
|
||||
/// Callback when the form is submitted.
|
||||
final Function(Map<String, dynamic> data) onSubmit;
|
||||
|
||||
/// Whether the submission is loading.
|
||||
final bool isLoading;
|
||||
|
||||
/// Creates a [ShiftOrderFormSheet].
|
||||
const ShiftOrderFormSheet({
|
||||
@@ -45,6 +37,14 @@ class ShiftOrderFormSheet extends StatefulWidget {
|
||||
required this.onSubmit,
|
||||
this.isLoading = false,
|
||||
});
|
||||
/// Initial data for the form (e.g. from a reorder action).
|
||||
final Map<String, dynamic>? initialData;
|
||||
|
||||
/// Callback when the form is submitted.
|
||||
final Function(Map<String, dynamic> data) onSubmit;
|
||||
|
||||
/// Whether the submission is loading.
|
||||
final bool isLoading;
|
||||
|
||||
@override
|
||||
State<ShiftOrderFormSheet> createState() => _ShiftOrderFormSheetState();
|
||||
@@ -222,10 +222,7 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
|
||||
.date(orderTimestamp)
|
||||
.execute();
|
||||
|
||||
final String? orderId = orderResult.data?.order_insert.id;
|
||||
if (orderId == null) {
|
||||
return;
|
||||
}
|
||||
final String orderId = orderResult.data.order_insert.id;
|
||||
|
||||
final int workersNeeded = _positions.fold<int>(
|
||||
0,
|
||||
@@ -255,10 +252,7 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
|
||||
.cost(shiftCost)
|
||||
.execute();
|
||||
|
||||
final String? shiftId = shiftResult.data?.shift_insert.id;
|
||||
if (shiftId == null) {
|
||||
return;
|
||||
}
|
||||
final String shiftId = shiftResult.data.shift_insert.id;
|
||||
|
||||
for (final Map<String, dynamic> pos in _positions) {
|
||||
final String roleId = pos['roleId']?.toString() ?? '';
|
||||
@@ -415,12 +409,12 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
|
||||
|
||||
final dc.ListShiftRolesByBusinessAndOrderShiftRolesShift firstShift =
|
||||
shiftRoles.first.shift;
|
||||
final dc.ListShiftRolesByBusinessAndOrderShiftRolesShiftOrderTeamHub?
|
||||
final dc.ListShiftRolesByBusinessAndOrderShiftRolesShiftOrderTeamHub
|
||||
teamHub = firstShift.order.teamHub;
|
||||
await _loadHubsAndSelect(
|
||||
placeId: teamHub?.placeId,
|
||||
hubName: teamHub?.hubName,
|
||||
address: teamHub?.address,
|
||||
placeId: teamHub.placeId,
|
||||
hubName: teamHub.hubName,
|
||||
address: teamHub.address,
|
||||
);
|
||||
_orderNameController.text = firstShift.order.eventName ?? '';
|
||||
|
||||
|
||||
@@ -4,6 +4,16 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// A widget that displays spending insights for the client.
|
||||
class SpendingWidget extends StatelessWidget {
|
||||
|
||||
/// Creates a [SpendingWidget].
|
||||
const SpendingWidget({
|
||||
super.key,
|
||||
required this.weeklySpending,
|
||||
required this.next7DaysSpending,
|
||||
required this.weeklyShifts,
|
||||
required this.next7DaysScheduled,
|
||||
this.subtitle,
|
||||
});
|
||||
/// The spending this week.
|
||||
final double weeklySpending;
|
||||
|
||||
@@ -19,16 +29,6 @@ class SpendingWidget extends StatelessWidget {
|
||||
/// Optional subtitle for the section.
|
||||
final String? subtitle;
|
||||
|
||||
/// Creates a [SpendingWidget].
|
||||
const SpendingWidget({
|
||||
super.key,
|
||||
required this.weeklySpending,
|
||||
required this.next7DaysSpending,
|
||||
required this.weeklyShifts,
|
||||
required this.next7DaysScheduled,
|
||||
this.subtitle,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final TranslationsClientHomeEn i18n = t.client_home;
|
||||
|
||||
@@ -23,7 +23,11 @@ dependencies:
|
||||
path: ../../../core_localization
|
||||
krow_domain: ^0.0.1
|
||||
krow_data_connect: ^0.0.1
|
||||
krow_core:
|
||||
path: ../../../core
|
||||
|
||||
firebase_data_connect: any
|
||||
intl: any
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
library client_hubs;
|
||||
library;
|
||||
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
|
||||
@@ -14,10 +14,11 @@ import 'package:krow_domain/krow_domain.dart'
|
||||
NotAuthenticatedException;
|
||||
|
||||
import '../../domain/repositories/hub_repository_interface.dart';
|
||||
import '../../util/hubs_constants.dart';
|
||||
|
||||
/// Implementation of [HubRepositoryInterface] backed by Data Connect.
|
||||
class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
class HubRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements HubRepositoryInterface {
|
||||
HubRepositoryImpl({
|
||||
required firebase.FirebaseAuth firebaseAuth,
|
||||
required dc.ExampleConnector dataConnect,
|
||||
@@ -57,27 +58,23 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
final String? countryValue = country ?? placeAddress?.country;
|
||||
final String? zipCodeValue = zipCode ?? placeAddress?.zipCode;
|
||||
|
||||
final OperationResult<dc.CreateTeamHubData, dc.CreateTeamHubVariables> result = await _dataConnect
|
||||
.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;
|
||||
if (createdId == null) {
|
||||
throw HubCreationFailedException(
|
||||
technicalMessage: 'teamHub_insert returned null for hub: $name',
|
||||
);
|
||||
}
|
||||
final OperationResult<dc.CreateTeamHubData, dc.CreateTeamHubVariables>
|
||||
result = await executeProtected(() => _dataConnect
|
||||
.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(
|
||||
teamId: teamId,
|
||||
@@ -111,14 +108,14 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
);
|
||||
}
|
||||
|
||||
final QueryResult<
|
||||
dc.ListOrdersByBusinessAndTeamHubData,
|
||||
dc.ListOrdersByBusinessAndTeamHubVariables> result = await _dataConnect
|
||||
.listOrdersByBusinessAndTeamHub(
|
||||
businessId: businessId,
|
||||
teamHubId: id,
|
||||
)
|
||||
.execute();
|
||||
final QueryResult<dc.ListOrdersByBusinessAndTeamHubData,
|
||||
dc.ListOrdersByBusinessAndTeamHubVariables> result =
|
||||
await executeProtected(() => _dataConnect
|
||||
.listOrdersByBusinessAndTeamHub(
|
||||
businessId: businessId,
|
||||
teamHubId: id,
|
||||
)
|
||||
.execute());
|
||||
|
||||
if (result.data.orders.isNotEmpty) {
|
||||
throw HubHasOrdersException(
|
||||
@@ -126,7 +123,7 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
);
|
||||
}
|
||||
|
||||
await _dataConnect.deleteTeamHub(id: id).execute();
|
||||
await executeProtected(() => _dataConnect.deleteTeamHub(id: id).execute());
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -169,9 +166,11 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
);
|
||||
}
|
||||
|
||||
final QueryResult<dc.GetBusinessesByUserIdData, dc.GetBusinessesByUserIdVariables> result = await _dataConnect.getBusinessesByUserId(
|
||||
userId: user.uid,
|
||||
).execute();
|
||||
final QueryResult<dc.GetBusinessesByUserIdData,
|
||||
dc.GetBusinessesByUserIdVariables> result =
|
||||
await executeProtected(() => _dataConnect.getBusinessesByUserId(
|
||||
userId: user.uid,
|
||||
).execute());
|
||||
if (result.data.businesses.isEmpty) {
|
||||
await _firebaseAuth.signOut();
|
||||
throw BusinessNotFoundException(
|
||||
@@ -203,9 +202,10 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
Future<String> _getOrCreateTeamId(
|
||||
dc.GetBusinessesByUserIdBusinesses business,
|
||||
) async {
|
||||
final QueryResult<dc.GetTeamsByOwnerIdData, dc.GetTeamsByOwnerIdVariables> teamsResult = await _dataConnect.getTeamsByOwnerId(
|
||||
ownerId: business.id,
|
||||
).execute();
|
||||
final QueryResult<dc.GetTeamsByOwnerIdData, dc.GetTeamsByOwnerIdVariables>
|
||||
teamsResult = await executeProtected(() => _dataConnect.getTeamsByOwnerId(
|
||||
ownerId: business.id,
|
||||
).execute());
|
||||
if (teamsResult.data.teams.isNotEmpty) {
|
||||
return teamsResult.data.teams.first.id;
|
||||
}
|
||||
@@ -220,13 +220,10 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
createTeamBuilder.email(business.email);
|
||||
}
|
||||
|
||||
final OperationResult<dc.CreateTeamData, dc.CreateTeamVariables> createTeamResult = await createTeamBuilder.execute();
|
||||
final String? teamId = createTeamResult.data?.team_insert.id;
|
||||
if (teamId == null) {
|
||||
throw HubCreationFailedException(
|
||||
technicalMessage: 'Team creation failed for business ${business.id}',
|
||||
);
|
||||
}
|
||||
final OperationResult<dc.CreateTeamData, dc.CreateTeamVariables>
|
||||
createTeamResult =
|
||||
await executeProtected(() => createTeamBuilder.execute());
|
||||
final String teamId = createTeamResult.data.team_insert.id;
|
||||
|
||||
return teamId;
|
||||
}
|
||||
@@ -235,9 +232,11 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
required String teamId,
|
||||
required String businessId,
|
||||
}) async {
|
||||
final QueryResult<dc.GetTeamHubsByTeamIdData, dc.GetTeamHubsByTeamIdVariables> hubsResult = await _dataConnect.getTeamHubsByTeamId(
|
||||
teamId: teamId,
|
||||
).execute();
|
||||
final QueryResult<dc.GetTeamHubsByTeamIdData,
|
||||
dc.GetTeamHubsByTeamIdVariables> hubsResult =
|
||||
await executeProtected(() => _dataConnect.getTeamHubsByTeamId(
|
||||
teamId: teamId,
|
||||
).execute());
|
||||
|
||||
return hubsResult.data.teamHubs
|
||||
.map(
|
||||
@@ -318,13 +317,13 @@ class HubRepositoryImpl implements HubRepositoryInterface {
|
||||
}
|
||||
}
|
||||
|
||||
final String? streetValue = <String?>[streetNumber, route]
|
||||
.where((String? value) => value != null && value!.isNotEmpty)
|
||||
final String streetValue = <String?>[streetNumber, route]
|
||||
.where((String? value) => value != null && value.isNotEmpty)
|
||||
.join(' ')
|
||||
.trim();
|
||||
|
||||
return _PlaceAddress(
|
||||
street: streetValue?.isEmpty == true ? null : streetValue,
|
||||
street: streetValue.isEmpty == true ? null : streetValue,
|
||||
city: city,
|
||||
state: state,
|
||||
country: country,
|
||||
|
||||
@@ -4,17 +4,17 @@ import 'package:krow_core/core.dart';
|
||||
///
|
||||
/// Encapsulates the hub ID and the NFC tag ID to be assigned.
|
||||
class AssignNfcTagArguments extends UseCaseArgument {
|
||||
|
||||
/// Creates an [AssignNfcTagArguments] instance.
|
||||
///
|
||||
/// Both [hubId] and [nfcTagId] are required.
|
||||
const AssignNfcTagArguments({required this.hubId, required this.nfcTagId});
|
||||
/// The unique identifier of the hub.
|
||||
final String hubId;
|
||||
|
||||
/// The unique identifier of the NFC tag.
|
||||
final String nfcTagId;
|
||||
|
||||
/// Creates an [AssignNfcTagArguments] instance.
|
||||
///
|
||||
/// Both [hubId] and [nfcTagId] are required.
|
||||
const AssignNfcTagArguments({required this.hubId, required this.nfcTagId});
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[hubId, nfcTagId];
|
||||
}
|
||||
|
||||
@@ -4,20 +4,6 @@ import 'package:krow_core/core.dart';
|
||||
///
|
||||
/// Encapsulates the name and address of the hub to be created.
|
||||
class CreateHubArguments extends UseCaseArgument {
|
||||
/// The name of the hub.
|
||||
final String name;
|
||||
|
||||
/// The physical address of the hub.
|
||||
final String address;
|
||||
|
||||
final String? placeId;
|
||||
final double? latitude;
|
||||
final double? longitude;
|
||||
final String? city;
|
||||
final String? state;
|
||||
final String? street;
|
||||
final String? country;
|
||||
final String? zipCode;
|
||||
|
||||
/// Creates a [CreateHubArguments] instance.
|
||||
///
|
||||
@@ -34,6 +20,20 @@ class CreateHubArguments extends UseCaseArgument {
|
||||
this.country,
|
||||
this.zipCode,
|
||||
});
|
||||
/// The name of the hub.
|
||||
final String name;
|
||||
|
||||
/// The physical address of the hub.
|
||||
final String address;
|
||||
|
||||
final String? placeId;
|
||||
final double? latitude;
|
||||
final double? longitude;
|
||||
final String? city;
|
||||
final String? state;
|
||||
final String? street;
|
||||
final String? country;
|
||||
final String? zipCode;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[
|
||||
|
||||
@@ -4,13 +4,13 @@ import 'package:krow_core/core.dart';
|
||||
///
|
||||
/// Encapsulates the hub ID of the hub to be deleted.
|
||||
class DeleteHubArguments extends UseCaseArgument {
|
||||
/// The unique identifier of the hub to delete.
|
||||
final String hubId;
|
||||
|
||||
/// Creates a [DeleteHubArguments] instance.
|
||||
///
|
||||
/// The [hubId] is required.
|
||||
const DeleteHubArguments({required this.hubId});
|
||||
/// The unique identifier of the hub to delete.
|
||||
final String hubId;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[hubId];
|
||||
|
||||
@@ -7,12 +7,12 @@ import '../repositories/hub_repository_interface.dart';
|
||||
/// This use case handles the association of a physical NFC tag with a specific
|
||||
/// hub by calling the [HubRepositoryInterface].
|
||||
class AssignNfcTagUseCase implements UseCase<AssignNfcTagArguments, void> {
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
/// Creates an [AssignNfcTagUseCase].
|
||||
///
|
||||
/// Requires a [HubRepositoryInterface] to interact with the backend.
|
||||
AssignNfcTagUseCase(this._repository);
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<void> call(AssignNfcTagArguments arguments) {
|
||||
|
||||
@@ -9,12 +9,12 @@ import '../repositories/hub_repository_interface.dart';
|
||||
/// [HubRepositoryInterface]. It requires [CreateHubArguments] which includes
|
||||
/// the name and address of the hub.
|
||||
class CreateHubUseCase implements UseCase<CreateHubArguments, Hub> {
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [CreateHubUseCase].
|
||||
///
|
||||
/// Requires a [HubRepositoryInterface] to perform the actual creation.
|
||||
CreateHubUseCase(this._repository);
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<Hub> call(CreateHubArguments arguments) {
|
||||
|
||||
@@ -6,12 +6,12 @@ import '../repositories/hub_repository_interface.dart';
|
||||
///
|
||||
/// This use case removes a hub from the system via the [HubRepositoryInterface].
|
||||
class DeleteHubUseCase implements UseCase<DeleteHubArguments, void> {
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [DeleteHubUseCase].
|
||||
///
|
||||
/// Requires a [HubRepositoryInterface] to perform the deletion.
|
||||
DeleteHubUseCase(this._repository);
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<void> call(DeleteHubArguments arguments) {
|
||||
|
||||
@@ -7,12 +7,12 @@ import '../repositories/hub_repository_interface.dart';
|
||||
/// This use case retrieves all hubs associated with the current client
|
||||
/// by interacting with the [HubRepositoryInterface].
|
||||
class GetHubsUseCase implements NoInputUseCase<List<Hub>> {
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
/// Creates a [GetHubsUseCase].
|
||||
///
|
||||
/// Requires a [HubRepositoryInterface] to fetch the data.
|
||||
GetHubsUseCase(this._repository);
|
||||
final HubRepositoryInterface _repository;
|
||||
|
||||
@override
|
||||
Future<List<Hub>> call() {
|
||||
|
||||
@@ -19,10 +19,6 @@ import 'client_hubs_state.dart';
|
||||
class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
with BlocErrorHandler<ClientHubsState>
|
||||
implements Disposable {
|
||||
final GetHubsUseCase _getHubsUseCase;
|
||||
final CreateHubUseCase _createHubUseCase;
|
||||
final DeleteHubUseCase _deleteHubUseCase;
|
||||
final AssignNfcTagUseCase _assignNfcTagUseCase;
|
||||
|
||||
ClientHubsBloc({
|
||||
required GetHubsUseCase getHubsUseCase,
|
||||
@@ -42,6 +38,10 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
on<ClientHubsAddDialogToggled>(_onAddDialogToggled);
|
||||
on<ClientHubsIdentifyDialogToggled>(_onIdentifyDialogToggled);
|
||||
}
|
||||
final GetHubsUseCase _getHubsUseCase;
|
||||
final CreateHubUseCase _createHubUseCase;
|
||||
final DeleteHubUseCase _deleteHubUseCase;
|
||||
final AssignNfcTagUseCase _assignNfcTagUseCase;
|
||||
|
||||
void _onAddDialogToggled(
|
||||
ClientHubsAddDialogToggled event,
|
||||
@@ -70,10 +70,10 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final hubs = await _getHubsUseCase();
|
||||
final List<Hub> hubs = await _getHubsUseCase();
|
||||
emit(state.copyWith(status: ClientHubsStatus.success, hubs: hubs));
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientHubsStatus.failure,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -103,7 +103,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
zipCode: event.zipCode,
|
||||
),
|
||||
);
|
||||
final hubs = await _getHubsUseCase();
|
||||
final List<Hub> hubs = await _getHubsUseCase();
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: ClientHubsStatus.actionSuccess,
|
||||
@@ -113,7 +113,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientHubsStatus.actionFailure,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -130,7 +130,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
emit: emit,
|
||||
action: () async {
|
||||
await _deleteHubUseCase(DeleteHubArguments(hubId: event.hubId));
|
||||
final hubs = await _getHubsUseCase();
|
||||
final List<Hub> hubs = await _getHubsUseCase();
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: ClientHubsStatus.actionSuccess,
|
||||
@@ -139,7 +139,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientHubsStatus.actionFailure,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
@@ -158,7 +158,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
await _assignNfcTagUseCase(
|
||||
AssignNfcTagArguments(hubId: event.hubId, nfcTagId: event.nfcTagId),
|
||||
);
|
||||
final hubs = await _getHubsUseCase();
|
||||
final List<Hub> hubs = await _getHubsUseCase();
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: ClientHubsStatus.actionSuccess,
|
||||
@@ -168,7 +168,7 @@ class ClientHubsBloc extends Bloc<ClientHubsEvent, ClientHubsState>
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: (errorKey) => state.copyWith(
|
||||
onError: (String errorKey) => state.copyWith(
|
||||
status: ClientHubsStatus.actionFailure,
|
||||
errorMessage: errorKey,
|
||||
),
|
||||
|
||||
@@ -16,16 +16,6 @@ class ClientHubsFetched extends ClientHubsEvent {
|
||||
|
||||
/// Event triggered to add a new hub.
|
||||
class ClientHubsAddRequested extends ClientHubsEvent {
|
||||
final String name;
|
||||
final String address;
|
||||
final String? placeId;
|
||||
final double? latitude;
|
||||
final double? longitude;
|
||||
final String? city;
|
||||
final String? state;
|
||||
final String? street;
|
||||
final String? country;
|
||||
final String? zipCode;
|
||||
|
||||
const ClientHubsAddRequested({
|
||||
required this.name,
|
||||
@@ -39,6 +29,16 @@ class ClientHubsAddRequested extends ClientHubsEvent {
|
||||
this.country,
|
||||
this.zipCode,
|
||||
});
|
||||
final String name;
|
||||
final String address;
|
||||
final String? placeId;
|
||||
final double? latitude;
|
||||
final double? longitude;
|
||||
final String? city;
|
||||
final String? state;
|
||||
final String? street;
|
||||
final String? country;
|
||||
final String? zipCode;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[
|
||||
@@ -57,9 +57,9 @@ class ClientHubsAddRequested extends ClientHubsEvent {
|
||||
|
||||
/// Event triggered to delete a hub.
|
||||
class ClientHubsDeleteRequested extends ClientHubsEvent {
|
||||
final String hubId;
|
||||
|
||||
const ClientHubsDeleteRequested(this.hubId);
|
||||
final String hubId;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[hubId];
|
||||
@@ -67,13 +67,13 @@ class ClientHubsDeleteRequested extends ClientHubsEvent {
|
||||
|
||||
/// Event triggered to assign an NFC tag to a hub.
|
||||
class ClientHubsNfcTagAssignRequested extends ClientHubsEvent {
|
||||
final String hubId;
|
||||
final String nfcTagId;
|
||||
|
||||
const ClientHubsNfcTagAssignRequested({
|
||||
required this.hubId,
|
||||
required this.nfcTagId,
|
||||
});
|
||||
final String hubId;
|
||||
final String nfcTagId;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[hubId, nfcTagId];
|
||||
@@ -86,9 +86,9 @@ class ClientHubsMessageCleared extends ClientHubsEvent {
|
||||
|
||||
/// Event triggered to toggle the visibility of the "Add Hub" dialog.
|
||||
class ClientHubsAddDialogToggled extends ClientHubsEvent {
|
||||
final bool visible;
|
||||
|
||||
const ClientHubsAddDialogToggled({required this.visible});
|
||||
final bool visible;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[visible];
|
||||
@@ -96,9 +96,9 @@ class ClientHubsAddDialogToggled extends ClientHubsEvent {
|
||||
|
||||
/// Event triggered to toggle the visibility of the "Identify NFC" dialog.
|
||||
class ClientHubsIdentifyDialogToggled extends ClientHubsEvent {
|
||||
final Hub? hub;
|
||||
|
||||
const ClientHubsIdentifyDialogToggled({this.hub});
|
||||
final Hub? hub;
|
||||
|
||||
@override
|
||||
List<Object?> get props => <Object?>[hub];
|
||||
|
||||
@@ -14,6 +14,15 @@ enum ClientHubsStatus {
|
||||
|
||||
/// State class for the ClientHubs BLoC.
|
||||
class ClientHubsState extends Equatable {
|
||||
|
||||
const ClientHubsState({
|
||||
this.status = ClientHubsStatus.initial,
|
||||
this.hubs = const <Hub>[],
|
||||
this.errorMessage,
|
||||
this.successMessage,
|
||||
this.showAddHubDialog = false,
|
||||
this.hubToIdentify,
|
||||
});
|
||||
final ClientHubsStatus status;
|
||||
final List<Hub> hubs;
|
||||
final String? errorMessage;
|
||||
@@ -26,15 +35,6 @@ class ClientHubsState extends Equatable {
|
||||
/// If null, the identification dialog is closed.
|
||||
final Hub? hubToIdentify;
|
||||
|
||||
const ClientHubsState({
|
||||
this.status = ClientHubsStatus.initial,
|
||||
this.hubs = const <Hub>[],
|
||||
this.errorMessage,
|
||||
this.successMessage,
|
||||
this.showAddHubDialog = false,
|
||||
this.hubToIdentify,
|
||||
});
|
||||
|
||||
ClientHubsState copyWith({
|
||||
ClientHubsStatus? status,
|
||||
List<Hub>? hubs,
|
||||
|
||||
@@ -7,6 +7,13 @@ import 'hub_address_autocomplete.dart';
|
||||
|
||||
/// A dialog for adding a new hub.
|
||||
class AddHubDialog extends StatefulWidget {
|
||||
|
||||
/// Creates an [AddHubDialog].
|
||||
const AddHubDialog({
|
||||
required this.onCreate,
|
||||
required this.onCancel,
|
||||
super.key,
|
||||
});
|
||||
/// Callback when the "Create Hub" button is pressed.
|
||||
final void Function(
|
||||
String name,
|
||||
@@ -19,13 +26,6 @@ class AddHubDialog extends StatefulWidget {
|
||||
/// Callback when the dialog is cancelled.
|
||||
final VoidCallback onCancel;
|
||||
|
||||
/// Creates an [AddHubDialog].
|
||||
const AddHubDialog({
|
||||
required this.onCreate,
|
||||
required this.onCancel,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<AddHubDialog> createState() => _AddHubDialogState();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class HubAddressAutocomplete extends StatelessWidget {
|
||||
);
|
||||
onSelected?.call(prediction);
|
||||
},
|
||||
itemBuilder: (_, _, Prediction prediction) {
|
||||
itemBuilder: (BuildContext context, int index, Prediction prediction) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(UiConstants.space2),
|
||||
child: Row(
|
||||
|
||||
@@ -5,14 +5,6 @@ import 'package:core_localization/core_localization.dart';
|
||||
|
||||
/// A card displaying information about a single hub.
|
||||
class HubCard extends StatelessWidget {
|
||||
/// The hub to display.
|
||||
final Hub hub;
|
||||
|
||||
/// Callback when the NFC button is pressed.
|
||||
final VoidCallback onNfcPressed;
|
||||
|
||||
/// Callback when the delete button is pressed.
|
||||
final VoidCallback onDeletePressed;
|
||||
|
||||
/// Creates a [HubCard].
|
||||
const HubCard({
|
||||
@@ -21,6 +13,14 @@ class HubCard extends StatelessWidget {
|
||||
required this.onDeletePressed,
|
||||
super.key,
|
||||
});
|
||||
/// The hub to display.
|
||||
final Hub hub;
|
||||
|
||||
/// Callback when the NFC button is pressed.
|
||||
final VoidCallback onNfcPressed;
|
||||
|
||||
/// Callback when the delete button is pressed.
|
||||
final VoidCallback onDeletePressed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -4,11 +4,11 @@ import 'package:core_localization/core_localization.dart';
|
||||
|
||||
/// Widget displayed when there are no hubs.
|
||||
class HubEmptyState extends StatelessWidget {
|
||||
/// Callback when the add button is pressed.
|
||||
final VoidCallback onAddPressed;
|
||||
|
||||
/// Creates a [HubEmptyState].
|
||||
const HubEmptyState({required this.onAddPressed, super.key});
|
||||
/// Callback when the add button is pressed.
|
||||
final VoidCallback onAddPressed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -5,14 +5,6 @@ import 'package:core_localization/core_localization.dart';
|
||||
|
||||
/// A dialog for identifying and assigning an NFC tag to a hub.
|
||||
class IdentifyNfcDialog extends StatefulWidget {
|
||||
/// The hub to assign the tag to.
|
||||
final Hub hub;
|
||||
|
||||
/// Callback when a tag is assigned.
|
||||
final Function(String nfcTagId) onAssign;
|
||||
|
||||
/// Callback when the dialog is cancelled.
|
||||
final VoidCallback onCancel;
|
||||
|
||||
/// Creates an [IdentifyNfcDialog].
|
||||
const IdentifyNfcDialog({
|
||||
@@ -21,6 +13,14 @@ class IdentifyNfcDialog extends StatefulWidget {
|
||||
required this.onCancel,
|
||||
super.key,
|
||||
});
|
||||
/// The hub to assign the tag to.
|
||||
final Hub hub;
|
||||
|
||||
/// Callback when a tag is assigned.
|
||||
final Function(String nfcTagId) onAssign;
|
||||
|
||||
/// Callback when the dialog is cancelled.
|
||||
final VoidCallback onCancel;
|
||||
|
||||
@override
|
||||
State<IdentifyNfcDialog> createState() => _IdentifyNfcDialogState();
|
||||
|
||||
@@ -6,7 +6,9 @@ import 'package:krow_domain/krow_domain.dart' as domain;
|
||||
import '../../domain/repositories/i_view_orders_repository.dart';
|
||||
|
||||
/// Implementation of [IViewOrdersRepository] using Data Connect.
|
||||
class ViewOrdersRepositoryImpl implements IViewOrdersRepository {
|
||||
class ViewOrdersRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements IViewOrdersRepository {
|
||||
final firebase.FirebaseAuth _firebaseAuth;
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
|
||||
@@ -29,13 +31,15 @@ class ViewOrdersRepositoryImpl implements IViewOrdersRepository {
|
||||
|
||||
final fdc.Timestamp startTimestamp = _toTimestamp(_startOfDay(start));
|
||||
final fdc.Timestamp endTimestamp = _toTimestamp(_endOfDay(end));
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData, dc.ListShiftRolesByBusinessAndDateRangeVariables> result = await _dataConnect
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
)
|
||||
.execute();
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
await executeProtected(() => _dataConnect
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
)
|
||||
.execute());
|
||||
print(
|
||||
'ViewOrders range start=${start.toIso8601String()} end=${end.toIso8601String()} shiftRoles=${result.data.shiftRoles.length}',
|
||||
);
|
||||
@@ -101,13 +105,15 @@ class ViewOrdersRepositoryImpl implements IViewOrdersRepository {
|
||||
|
||||
final fdc.Timestamp dayStart = _toTimestamp(_startOfDay(day));
|
||||
final fdc.Timestamp dayEnd = _toTimestamp(_endOfDay(day));
|
||||
final fdc.QueryResult<dc.ListAcceptedApplicationsByBusinessForDayData, dc.ListAcceptedApplicationsByBusinessForDayVariables> result = await _dataConnect
|
||||
.listAcceptedApplicationsByBusinessForDay(
|
||||
businessId: businessId,
|
||||
dayStart: dayStart,
|
||||
dayEnd: dayEnd,
|
||||
)
|
||||
.execute();
|
||||
final fdc.QueryResult<dc.ListAcceptedApplicationsByBusinessForDayData,
|
||||
dc.ListAcceptedApplicationsByBusinessForDayVariables> result =
|
||||
await executeProtected(() => _dataConnect
|
||||
.listAcceptedApplicationsByBusinessForDay(
|
||||
businessId: businessId,
|
||||
dayStart: dayStart,
|
||||
dayEnd: dayEnd,
|
||||
)
|
||||
.execute());
|
||||
|
||||
print(
|
||||
'ViewOrders day=${day.toIso8601String()} applications=${result.data.applications.length}',
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:view_orders/src/presentation/blocs/view_orders_cubit.dart';
|
||||
import 'package:view_orders/src/presentation/blocs/view_orders_state.dart';
|
||||
import 'package:view_orders/src/domain/usecases/get_orders_use_case.dart';
|
||||
import 'package:view_orders/src/domain/usecases/get_accepted_applications_for_day_use_case.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'package:view_orders/src/domain/arguments/orders_range_arguments.dart';
|
||||
import 'package:view_orders/src/domain/arguments/orders_day_arguments.dart';
|
||||
|
||||
class MockGetOrdersUseCase extends Mock implements GetOrdersUseCase {}
|
||||
class MockGetAcceptedAppsUseCase extends Mock implements GetAcceptedApplicationsForDayUseCase {}
|
||||
|
||||
void main() {
|
||||
group('ViewOrdersCubit', () {
|
||||
late GetOrdersUseCase getOrdersUseCase;
|
||||
late GetAcceptedApplicationsForDayUseCase getAcceptedAppsUseCase;
|
||||
|
||||
setUp(() {
|
||||
getOrdersUseCase = MockGetOrdersUseCase();
|
||||
getAcceptedAppsUseCase = MockGetAcceptedAppsUseCase();
|
||||
registerFallbackValue(OrdersRangeArguments(start: DateTime.now(), end: DateTime.now()));
|
||||
registerFallbackValue(OrdersDayArguments(day: DateTime.now()));
|
||||
});
|
||||
|
||||
test('initial state is correct', () {
|
||||
final cubit = ViewOrdersCubit(
|
||||
getOrdersUseCase: getOrdersUseCase,
|
||||
getAcceptedAppsUseCase: getAcceptedAppsUseCase,
|
||||
);
|
||||
expect(cubit.state.status, ViewOrdersStatus.initial);
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
blocTest<ViewOrdersCubit, ViewOrdersState>(
|
||||
'calculates upNextCount based on ALL loaded orders, not just the selected day',
|
||||
build: () {
|
||||
final mockOrders = [
|
||||
// Order 1: Today (Matches selected date)
|
||||
OrderItem(
|
||||
id: '1', orderId: '1', title: 'Order 1', clientName: 'Client',
|
||||
status: 'OPEN', date: '2026-02-04', startTime: '09:00', endTime: '17:00',
|
||||
location: 'Loc', locationAddress: 'Addr', filled: 0, workersNeeded: 1,
|
||||
hourlyRate: 20, hours: 8, totalValue: 160
|
||||
),
|
||||
// Order 2: Tomorrow (Different date)
|
||||
OrderItem(
|
||||
id: '2', orderId: '2', title: 'Order 2', clientName: 'Client',
|
||||
status: 'OPEN', date: '2026-02-05', startTime: '09:00', endTime: '17:00',
|
||||
location: 'Loc', locationAddress: 'Addr', filled: 0, workersNeeded: 1,
|
||||
hourlyRate: 20, hours: 8, totalValue: 160
|
||||
),
|
||||
];
|
||||
|
||||
when(() => getOrdersUseCase(any())).thenAnswer((_) async => mockOrders);
|
||||
when(() => getAcceptedAppsUseCase(any())).thenAnswer((_) async => {});
|
||||
|
||||
return ViewOrdersCubit(
|
||||
getOrdersUseCase: getOrdersUseCase,
|
||||
getAcceptedAppsUseCase: getAcceptedAppsUseCase,
|
||||
);
|
||||
},
|
||||
act: (cubit) async {
|
||||
// Wait for init to trigger load
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Select 'Today' (2026-02-04 matches Order 1)
|
||||
cubit.selectDate(DateTime(2026, 02, 04));
|
||||
},
|
||||
verify: (cubit) {
|
||||
// Assert:
|
||||
// 1. filteredOrders should only have 1 order (the one for the selected date)
|
||||
expect(cubit.state.filteredOrders.length, 1, reason: 'Should only show orders for selected filtered date');
|
||||
expect(cubit.state.filteredOrders.first.id, '1');
|
||||
|
||||
// 2. upNextCount should have 2 orders (Total for the loaded week)
|
||||
expect(cubit.state.upNextCount, 2, reason: 'Up Next count should include ALL orders in the week range');
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user