feat(bank-account-repository): Refactor BankAccountRepositoryImpl to utilize DataConnectService and remove FirebaseAuth dependency
This commit is contained in:
@@ -1,34 +1,31 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart' as auth;
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import '../../domain/repositories/bank_account_repository.dart';
|
||||
|
||||
/// Implementation of [BankAccountRepository] that integrates with Data Connect.
|
||||
class BankAccountRepositoryImpl
|
||||
with DataErrorHandler
|
||||
implements BankAccountRepository {
|
||||
class BankAccountRepositoryImpl implements BankAccountRepository {
|
||||
/// Creates a [BankAccountRepositoryImpl].
|
||||
const BankAccountRepositoryImpl({
|
||||
required this.dataConnect,
|
||||
required this.firebaseAuth,
|
||||
});
|
||||
BankAccountRepositoryImpl({
|
||||
DataConnectService? service,
|
||||
}) : _service = service ?? DataConnectService.instance;
|
||||
|
||||
/// The Data Connect instance.
|
||||
final ExampleConnector dataConnect;
|
||||
/// The Firebase Auth instance.
|
||||
final auth.FirebaseAuth firebaseAuth;
|
||||
/// The Data Connect service.
|
||||
final DataConnectService _service;
|
||||
|
||||
@override
|
||||
Future<List<BankAccount>> getAccounts() async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = _getStaffId();
|
||||
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
var x = staffId;
|
||||
|
||||
print(x);
|
||||
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
||||
result = await dataConnect
|
||||
result = await _service.connector
|
||||
.getAccountsByOwnerId(ownerId: staffId)
|
||||
.execute();
|
||||
|
||||
|
||||
return result.data.accounts.map((GetAccountsByOwnerIdAccounts account) {
|
||||
return BankAccountAdapter.fromPrimitives(
|
||||
id: account.id,
|
||||
@@ -37,7 +34,9 @@ class BankAccountRepositoryImpl
|
||||
accountNumber: account.accountNumber,
|
||||
last4: account.last4,
|
||||
sortCode: account.routeNumber,
|
||||
type: account.type is Known<AccountType> ? (account.type as Known<AccountType>).value.name : null,
|
||||
type: account.type is Known<AccountType>
|
||||
? (account.type as Known<AccountType>).value.name
|
||||
: null,
|
||||
isPrimary: account.isPrimary,
|
||||
);
|
||||
}).toList();
|
||||
@@ -46,44 +45,31 @@ class BankAccountRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> addAccount(BankAccount account) async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = _getStaffId();
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
||||
existingAccounts = await dataConnect
|
||||
existingAccounts = await _service.connector
|
||||
.getAccountsByOwnerId(ownerId: staffId)
|
||||
.execute();
|
||||
final bool hasAccounts = existingAccounts.data.accounts.isNotEmpty;
|
||||
final bool isPrimary = !hasAccounts;
|
||||
|
||||
await dataConnect.createAccount(
|
||||
bank: account.bankName,
|
||||
type: AccountType.values.byName(BankAccountAdapter.typeToString(account.type)),
|
||||
last4: _safeLast4(account.last4, account.accountNumber),
|
||||
ownerId: staffId,
|
||||
)
|
||||
.isPrimary(isPrimary)
|
||||
.accountNumber(account.accountNumber)
|
||||
.routeNumber(account.sortCode)
|
||||
.execute();
|
||||
await _service.connector
|
||||
.createAccount(
|
||||
bank: account.bankName,
|
||||
type: AccountType.values
|
||||
.byName(BankAccountAdapter.typeToString(account.type)),
|
||||
last4: _safeLast4(account.last4, account.accountNumber),
|
||||
ownerId: staffId,
|
||||
)
|
||||
.isPrimary(isPrimary)
|
||||
.accountNumber(account.accountNumber)
|
||||
.routeNumber(account.sortCode)
|
||||
.execute();
|
||||
});
|
||||
}
|
||||
|
||||
/// Helper to get the logged-in staff ID.
|
||||
String _getStaffId() {
|
||||
final auth.User? user = firebaseAuth.currentUser;
|
||||
if (user == null) {
|
||||
throw const NotAuthenticatedException(
|
||||
technicalMessage: 'User not authenticated');
|
||||
}
|
||||
|
||||
final String? staffId = StaffSessionStore.instance.session?.staff?.id;
|
||||
if (staffId == null || staffId.isEmpty) {
|
||||
throw const ServerException(technicalMessage: 'Staff profile is missing or session not initialized.');
|
||||
}
|
||||
return staffId;
|
||||
}
|
||||
|
||||
/// Ensures we have a last4 value, either from input or derived from account number.
|
||||
String _safeLast4(String? last4, String accountNumber) {
|
||||
if (last4 != null && last4.isNotEmpty) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart' as auth;
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
@@ -17,12 +17,7 @@ class StaffBankAccountModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
// Repositories
|
||||
i.addLazySingleton<BankAccountRepository>(
|
||||
() => BankAccountRepositoryImpl(
|
||||
firebaseAuth: auth.FirebaseAuth.instance,
|
||||
dataConnect: ExampleConnector.instance,
|
||||
),
|
||||
);
|
||||
i.addLazySingleton<BankAccountRepository>(BankAccountRepositoryImpl.new);
|
||||
|
||||
// Use Cases
|
||||
i.addLazySingleton<GetBankAccountsUseCase>(GetBankAccountsUseCase.new);
|
||||
@@ -41,7 +36,7 @@ class StaffBankAccountModule extends Module {
|
||||
void routes(RouteManager r) {
|
||||
r.child(
|
||||
StaffPaths.childRoute(StaffPaths.bankAccount, StaffPaths.bankAccount),
|
||||
child: (_) => const BankAccountPage(),
|
||||
child: (BuildContext context) => const BankAccountPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user