feat(payments-repository): Refactor PaymentsRepositoryImpl to utilize DataConnectService for payment operations and simplify staff ID retrieval
This commit is contained in:
@@ -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' as dc;
|
||||||
|
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||||
import 'package:krow_domain/krow_domain.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';
|
import '../../domain/repositories/payments_repository.dart';
|
||||||
|
|
||||||
class PaymentsRepositoryImpl
|
class PaymentsRepositoryImpl
|
||||||
with dc.DataErrorHandler
|
|
||||||
implements PaymentsRepository {
|
implements PaymentsRepository {
|
||||||
|
|
||||||
PaymentsRepositoryImpl() : _dataConnect = dc.ExampleConnector.instance;
|
PaymentsRepositoryImpl() : _service = DataConnectService.instance;
|
||||||
final dc.ExampleConnector _dataConnect;
|
final DataConnectService _service;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<PaymentSummary> getPaymentSummary() async {
|
Future<PaymentSummary> getPaymentSummary() async {
|
||||||
return executeProtected(() async {
|
return _service.run(() async {
|
||||||
final String currentStaffId = await _getStaffId();
|
final String currentStaffId = await _service.getStaffId();
|
||||||
|
|
||||||
// Fetch recent payments with a limit
|
// Fetch recent payments with a limit
|
||||||
// Note: limit is chained on the query builder
|
final response = await _service.connector.listRecentPaymentsByStaffId(
|
||||||
final QueryResult<dc.ListRecentPaymentsByStaffIdData, dc.ListRecentPaymentsByStaffIdVariables> result =
|
|
||||||
await _dataConnect.listRecentPaymentsByStaffId(
|
|
||||||
staffId: currentStaffId,
|
staffId: currentStaffId,
|
||||||
).limit(100).execute();
|
).limit(100).execute();
|
||||||
|
|
||||||
final List<dc.ListRecentPaymentsByStaffIdRecentPayments> payments = result.data.recentPayments;
|
final List<dc.ListRecentPaymentsByStaffIdRecentPayments> payments = response.data.recentPayments;
|
||||||
|
|
||||||
double weekly = 0;
|
double weekly = 0;
|
||||||
double monthly = 0;
|
double monthly = 0;
|
||||||
@@ -103,7 +32,7 @@ class PaymentsRepositoryImpl
|
|||||||
final DateTime startOfMonth = DateTime(now.year, now.month, 1);
|
final DateTime startOfMonth = DateTime(now.year, now.month, 1);
|
||||||
|
|
||||||
for (final dc.ListRecentPaymentsByStaffIdRecentPayments p in payments) {
|
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 double amount = p.invoice.amount;
|
||||||
final String? status = p.status?.stringValue;
|
final String? status = p.status?.stringValue;
|
||||||
|
|
||||||
@@ -129,11 +58,10 @@ class PaymentsRepositoryImpl
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<StaffPayment>> getPaymentHistory(String period) async {
|
Future<List<StaffPayment>> getPaymentHistory(String period) async {
|
||||||
return executeProtected(() async {
|
return _service.run(() async {
|
||||||
final String currentStaffId = await _getStaffId();
|
final String currentStaffId = await _service.getStaffId();
|
||||||
|
|
||||||
final QueryResult<dc.ListRecentPaymentsByStaffIdData, dc.ListRecentPaymentsByStaffIdVariables> response =
|
final response = await _service.connector
|
||||||
await _dataConnect
|
|
||||||
.listRecentPaymentsByStaffId(staffId: currentStaffId)
|
.listRecentPaymentsByStaffId(staffId: currentStaffId)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
@@ -144,7 +72,7 @@ class PaymentsRepositoryImpl
|
|||||||
assignmentId: payment.applicationId,
|
assignmentId: payment.applicationId,
|
||||||
amount: payment.invoice.amount,
|
amount: payment.invoice.amount,
|
||||||
status: PaymentAdapter.toPaymentStatus(payment.status?.stringValue ?? 'UNKNOWN'),
|
status: PaymentAdapter.toPaymentStatus(payment.status?.stringValue ?? 'UNKNOWN'),
|
||||||
paidAt: _toDateTime(payment.invoice.issueDate),
|
paidAt: _service.toDateTime(payment.invoice.issueDate),
|
||||||
);
|
);
|
||||||
}).toList();
|
}).toList();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user