feat(payments-repository): Refactor PaymentsRepositoryImpl to utilize DataConnectService for payment operations and simplify staff ID retrieval

This commit is contained in:
Achintha Isuru
2026-02-16 16:10:47 -05:00
parent dcb76db1f8
commit a10617f17d

View File

@@ -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<String> _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<dc.GetStaffByUserIdData, dc.GetStaffByUserIdVariables> 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<PaymentSummary> 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<dc.ListRecentPaymentsByStaffIdData, dc.ListRecentPaymentsByStaffIdVariables> result =
await _dataConnect.listRecentPaymentsByStaffId(
final response = await _service.connector.listRecentPaymentsByStaffId(
staffId: currentStaffId,
).limit(100).execute();
final List<dc.ListRecentPaymentsByStaffIdRecentPayments> payments = result.data.recentPayments;
final List<dc.ListRecentPaymentsByStaffIdRecentPayments> 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<List<StaffPayment>> getPaymentHistory(String period) async {
return executeProtected(() async {
final String currentStaffId = await _getStaffId();
return _service.run(() async {
final String currentStaffId = await _service.getStaffId();
final QueryResult<dc.ListRecentPaymentsByStaffIdData, dc.ListRecentPaymentsByStaffIdVariables> 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();
});