From a10617f17d196663011736babc190eb71adb5090 Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Mon, 16 Feb 2026 16:10:47 -0500 Subject: [PATCH] feat(payments-repository): Refactor PaymentsRepositoryImpl to utilize DataConnectService for payment operations and simplify staff ID retrieval --- .../payments_repository_impl.dart | 98 +++---------------- 1 file changed, 13 insertions(+), 85 deletions(-) diff --git a/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart b/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart index a0791ab5..42cdb1af 100644 --- a/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart +++ b/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart @@ -1,97 +1,26 @@ -import 'package:firebase_data_connect/firebase_data_connect.dart'; import 'package:krow_data_connect/krow_data_connect.dart' as dc; - +import 'package:krow_data_connect/krow_data_connect.dart'; import 'package:krow_domain/krow_domain.dart'; -import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; -import 'package:krow_core/core.dart'; + import '../../domain/repositories/payments_repository.dart'; class PaymentsRepositoryImpl - with dc.DataErrorHandler implements PaymentsRepository { - PaymentsRepositoryImpl() : _dataConnect = dc.ExampleConnector.instance; - final dc.ExampleConnector _dataConnect; - final firebase_auth.FirebaseAuth _auth = firebase_auth.FirebaseAuth.instance; - - String? _cachedStaffId; - - Future _getStaffId() async { - // 1. Check Session Store - final dc.StaffSession? session = dc.StaffSessionStore.instance.session; - if (session?.staff?.id != null) { - return session!.staff!.id; - } - - // 2. Check Cache - if (_cachedStaffId != null) return _cachedStaffId!; - - // 3. Fetch from Data Connect using Firebase UID - final firebase_auth.User? user = _auth.currentUser; - if (user == null) { - throw const NotAuthenticatedException( - technicalMessage: 'User is not authenticated', - ); - } - - // This call is protected by parent execution context if called within executeProtected, - // otherwise we might need to wrap it if called standalone. - // For now we assume it's called from public methods which are protected. - final QueryResult response = await _dataConnect.getStaffByUserId(userId: user.uid).execute(); - if (response.data.staffs.isNotEmpty) { - _cachedStaffId = response.data.staffs.first.id; - return _cachedStaffId!; - } - - // 4. Fallback - return user.uid; - } - - /// Helper to convert Data Connect Timestamp to DateTime - DateTime? _toDateTime(dynamic t) { - if (t == null) return null; - DateTime? dt; - if (t is DateTime) { - dt = t; - } else if (t is String) { - dt = DateTime.tryParse(t); - } else { - try { - if (t is Timestamp) { - dt = t.toDateTime(); - } - } catch (_) {} - - try { - if (dt == null && t.runtimeType.toString().contains('Timestamp')) { - dt = (t as dynamic).toDate(); - } - } catch (_) {} - - try { - dt ??= DateTime.tryParse(t.toString()); - } catch (_) {} - } - - if (dt != null) { - return DateTimeUtils.toDeviceTime(dt); - } - return null; - } + PaymentsRepositoryImpl() : _service = DataConnectService.instance; + final DataConnectService _service; @override Future getPaymentSummary() async { - return executeProtected(() async { - final String currentStaffId = await _getStaffId(); + return _service.run(() async { + final String currentStaffId = await _service.getStaffId(); // Fetch recent payments with a limit - // Note: limit is chained on the query builder - final QueryResult result = - await _dataConnect.listRecentPaymentsByStaffId( + final response = await _service.connector.listRecentPaymentsByStaffId( staffId: currentStaffId, ).limit(100).execute(); - final List payments = result.data.recentPayments; + final List payments = response.data.recentPayments; double weekly = 0; double monthly = 0; @@ -103,7 +32,7 @@ class PaymentsRepositoryImpl final DateTime startOfMonth = DateTime(now.year, now.month, 1); for (final dc.ListRecentPaymentsByStaffIdRecentPayments p in payments) { - final DateTime? date = _toDateTime(p.invoice.issueDate) ?? _toDateTime(p.createdAt); + final DateTime? date = _service.toDateTime(p.invoice.issueDate) ?? _service.toDateTime(p.createdAt); final double amount = p.invoice.amount; final String? status = p.status?.stringValue; @@ -129,11 +58,10 @@ class PaymentsRepositoryImpl @override Future> getPaymentHistory(String period) async { - return executeProtected(() async { - final String currentStaffId = await _getStaffId(); + return _service.run(() async { + final String currentStaffId = await _service.getStaffId(); - final QueryResult response = - await _dataConnect + final response = await _service.connector .listRecentPaymentsByStaffId(staffId: currentStaffId) .execute(); @@ -144,7 +72,7 @@ class PaymentsRepositoryImpl assignmentId: payment.applicationId, amount: payment.invoice.amount, status: PaymentAdapter.toPaymentStatus(payment.status?.stringValue ?? 'UNKNOWN'), - paidAt: _toDateTime(payment.invoice.issueDate), + paidAt: _service.toDateTime(payment.invoice.issueDate), ); }).toList(); });