fix: resolve payments compilation error and remove redundant datasource layer
This commit is contained in:
@@ -2,41 +2,75 @@ 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/src/session/staff_session_store.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import '../../domain/repositories/payments_repository.dart';
|
||||
|
||||
class PaymentsRepositoryImpl implements PaymentsRepository {
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
|
||||
PaymentsRepositoryImpl() : _dataConnect = dc.ExampleConnector.instance;
|
||||
|
||||
String? _cachedStaffId;
|
||||
|
||||
Future<String> _getStaffId() async {
|
||||
// 1. Check Session Store
|
||||
final StaffSession? session = 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 user = _auth.currentUser;
|
||||
if (user == null) {
|
||||
throw Exception('User is not authenticated');
|
||||
}
|
||||
|
||||
try {
|
||||
final response = await _dataConnect.getStaffByUserId(userId: user.uid).execute();
|
||||
if (response.data.staffs.isNotEmpty) {
|
||||
_cachedStaffId = response.data.staffs.first.id;
|
||||
return _cachedStaffId!;
|
||||
}
|
||||
} catch (e) {
|
||||
// Log or handle error
|
||||
}
|
||||
|
||||
// 4. Fallback
|
||||
return user.uid;
|
||||
}
|
||||
|
||||
/// Helper to convert Data Connect Timestamp to DateTime
|
||||
DateTime? _toDateTime(dynamic t) {
|
||||
if (t == null) return null;
|
||||
if (t is DateTime) return t;
|
||||
if (t is String) return DateTime.tryParse(t);
|
||||
|
||||
try {
|
||||
// Attempt to deserialize via standard methods
|
||||
return DateTime.tryParse(t.toJson() as String);
|
||||
} catch (_) {
|
||||
try {
|
||||
return DateTime.tryParse(t.toString());
|
||||
} catch (e) {
|
||||
return null;
|
||||
if (t is Timestamp) {
|
||||
return t.toDateTime();
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
if (t.runtimeType.toString().contains('Timestamp')) {
|
||||
return (t as dynamic).toDate();
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
return DateTime.tryParse(t.toString());
|
||||
} catch (_) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PaymentSummary> getPaymentSummary() async {
|
||||
final StaffSession? session = StaffSessionStore.instance.session;
|
||||
if (session?.staff?.id == null) {
|
||||
return const PaymentSummary(
|
||||
weeklyEarnings: 0,
|
||||
monthlyEarnings: 0,
|
||||
pendingEarnings: 0,
|
||||
totalEarnings: 0,
|
||||
);
|
||||
}
|
||||
|
||||
final String currentStaffId = session!.staff!.id;
|
||||
final String currentStaffId = await _getStaffId();
|
||||
|
||||
// Fetch recent payments with a limit
|
||||
// Note: limit is chained on the query builder
|
||||
@@ -82,10 +116,7 @@ class PaymentsRepositoryImpl implements PaymentsRepository {
|
||||
|
||||
@override
|
||||
Future<List<StaffPayment>> getPaymentHistory(String period) async {
|
||||
final StaffSession? session = StaffSessionStore.instance.session;
|
||||
if (session?.staff?.id == null) return <StaffPayment>[];
|
||||
|
||||
final String currentStaffId = session!.staff!.id;
|
||||
final String currentStaffId = await _getStaffId();
|
||||
|
||||
try {
|
||||
final QueryResult<dc.ListRecentPaymentsByStaffIdData, dc.ListRecentPaymentsByStaffIdVariables> response =
|
||||
|
||||
@@ -210,7 +210,7 @@ class _PaymentsPageState extends State<PaymentsPage> {
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
const SizedBox(height: 100),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user