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:firebase_data_connect/firebase_data_connect.dart';
|
||||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||||
import 'package:krow_domain/krow_domain.dart';
|
import 'package:krow_domain/krow_domain.dart';
|
||||||
import '../../domain/repositories/bank_account_repository.dart';
|
import '../../domain/repositories/bank_account_repository.dart';
|
||||||
|
|
||||||
/// Implementation of [BankAccountRepository] that integrates with Data Connect.
|
/// Implementation of [BankAccountRepository] that integrates with Data Connect.
|
||||||
class BankAccountRepositoryImpl
|
class BankAccountRepositoryImpl implements BankAccountRepository {
|
||||||
with DataErrorHandler
|
|
||||||
implements BankAccountRepository {
|
|
||||||
/// Creates a [BankAccountRepositoryImpl].
|
/// Creates a [BankAccountRepositoryImpl].
|
||||||
const BankAccountRepositoryImpl({
|
BankAccountRepositoryImpl({
|
||||||
required this.dataConnect,
|
DataConnectService? service,
|
||||||
required this.firebaseAuth,
|
}) : _service = service ?? DataConnectService.instance;
|
||||||
});
|
|
||||||
|
|
||||||
/// The Data Connect instance.
|
/// The Data Connect service.
|
||||||
final ExampleConnector dataConnect;
|
final DataConnectService _service;
|
||||||
/// The Firebase Auth instance.
|
|
||||||
final auth.FirebaseAuth firebaseAuth;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<BankAccount>> getAccounts() async {
|
Future<List<BankAccount>> getAccounts() async {
|
||||||
return executeProtected(() async {
|
return _service.run(() async {
|
||||||
final String staffId = _getStaffId();
|
final String staffId = await _service.getStaffId();
|
||||||
|
|
||||||
|
var x = staffId;
|
||||||
|
|
||||||
|
print(x);
|
||||||
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
||||||
result = await dataConnect
|
result = await _service.connector
|
||||||
.getAccountsByOwnerId(ownerId: staffId)
|
.getAccountsByOwnerId(ownerId: staffId)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
return result.data.accounts.map((GetAccountsByOwnerIdAccounts account) {
|
return result.data.accounts.map((GetAccountsByOwnerIdAccounts account) {
|
||||||
return BankAccountAdapter.fromPrimitives(
|
return BankAccountAdapter.fromPrimitives(
|
||||||
id: account.id,
|
id: account.id,
|
||||||
@@ -37,7 +34,9 @@ class BankAccountRepositoryImpl
|
|||||||
accountNumber: account.accountNumber,
|
accountNumber: account.accountNumber,
|
||||||
last4: account.last4,
|
last4: account.last4,
|
||||||
sortCode: account.routeNumber,
|
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,
|
isPrimary: account.isPrimary,
|
||||||
);
|
);
|
||||||
}).toList();
|
}).toList();
|
||||||
@@ -46,44 +45,31 @@ class BankAccountRepositoryImpl
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> addAccount(BankAccount account) async {
|
Future<void> addAccount(BankAccount account) async {
|
||||||
return executeProtected(() async {
|
return _service.run(() async {
|
||||||
final String staffId = _getStaffId();
|
final String staffId = await _service.getStaffId();
|
||||||
|
|
||||||
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
final QueryResult<GetAccountsByOwnerIdData, GetAccountsByOwnerIdVariables>
|
||||||
existingAccounts = await dataConnect
|
existingAccounts = await _service.connector
|
||||||
.getAccountsByOwnerId(ownerId: staffId)
|
.getAccountsByOwnerId(ownerId: staffId)
|
||||||
.execute();
|
.execute();
|
||||||
final bool hasAccounts = existingAccounts.data.accounts.isNotEmpty;
|
final bool hasAccounts = existingAccounts.data.accounts.isNotEmpty;
|
||||||
final bool isPrimary = !hasAccounts;
|
final bool isPrimary = !hasAccounts;
|
||||||
|
|
||||||
await dataConnect.createAccount(
|
await _service.connector
|
||||||
bank: account.bankName,
|
.createAccount(
|
||||||
type: AccountType.values.byName(BankAccountAdapter.typeToString(account.type)),
|
bank: account.bankName,
|
||||||
last4: _safeLast4(account.last4, account.accountNumber),
|
type: AccountType.values
|
||||||
ownerId: staffId,
|
.byName(BankAccountAdapter.typeToString(account.type)),
|
||||||
)
|
last4: _safeLast4(account.last4, account.accountNumber),
|
||||||
.isPrimary(isPrimary)
|
ownerId: staffId,
|
||||||
.accountNumber(account.accountNumber)
|
)
|
||||||
.routeNumber(account.sortCode)
|
.isPrimary(isPrimary)
|
||||||
.execute();
|
.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.
|
/// Ensures we have a last4 value, either from input or derived from account number.
|
||||||
String _safeLast4(String? last4, String accountNumber) {
|
String _safeLast4(String? last4, String accountNumber) {
|
||||||
if (last4 != null && last4.isNotEmpty) {
|
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:flutter_modular/flutter_modular.dart';
|
||||||
import 'package:krow_core/core.dart';
|
import 'package:krow_core/core.dart';
|
||||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||||
@@ -17,12 +17,7 @@ class StaffBankAccountModule extends Module {
|
|||||||
@override
|
@override
|
||||||
void binds(Injector i) {
|
void binds(Injector i) {
|
||||||
// Repositories
|
// Repositories
|
||||||
i.addLazySingleton<BankAccountRepository>(
|
i.addLazySingleton<BankAccountRepository>(BankAccountRepositoryImpl.new);
|
||||||
() => BankAccountRepositoryImpl(
|
|
||||||
firebaseAuth: auth.FirebaseAuth.instance,
|
|
||||||
dataConnect: ExampleConnector.instance,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Use Cases
|
// Use Cases
|
||||||
i.addLazySingleton<GetBankAccountsUseCase>(GetBankAccountsUseCase.new);
|
i.addLazySingleton<GetBankAccountsUseCase>(GetBankAccountsUseCase.new);
|
||||||
@@ -41,7 +36,7 @@ class StaffBankAccountModule extends Module {
|
|||||||
void routes(RouteManager r) {
|
void routes(RouteManager r) {
|
||||||
r.child(
|
r.child(
|
||||||
StaffPaths.childRoute(StaffPaths.bankAccount, StaffPaths.bankAccount),
|
StaffPaths.childRoute(StaffPaths.bankAccount, StaffPaths.bankAccount),
|
||||||
child: (_) => const BankAccountPage(),
|
child: (BuildContext context) => const BankAccountPage(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user