fix: Update staff application to connect with data connect
This commit is contained in:
@@ -42,10 +42,13 @@ class BillingConnectorRepositoryImpl implements BillingConnectorRepository {
|
||||
return _service.run(() async {
|
||||
final QueryResult<dc.ListInvoicesByBusinessIdData, dc.ListInvoicesByBusinessIdVariables> result = await _service.connector
|
||||
.listInvoicesByBusinessId(businessId: businessId)
|
||||
.limit(10)
|
||||
.limit(20)
|
||||
.execute();
|
||||
|
||||
return result.data.invoices.map(_mapInvoice).toList();
|
||||
return result.data.invoices
|
||||
.map(_mapInvoice)
|
||||
.where((Invoice i) => i.status == InvoiceStatus.paid)
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -59,7 +62,7 @@ class BillingConnectorRepositoryImpl implements BillingConnectorRepository {
|
||||
return result.data.invoices
|
||||
.map(_mapInvoice)
|
||||
.where((Invoice i) =>
|
||||
i.status == InvoiceStatus.open || i.status == InvoiceStatus.disputed)
|
||||
i.status != InvoiceStatus.paid)
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
@@ -132,9 +135,61 @@ class BillingConnectorRepositoryImpl implements BillingConnectorRepository {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> approveInvoice({required String id}) async {
|
||||
return _service.run(() async {
|
||||
await _service.connector
|
||||
.updateInvoice(id: id)
|
||||
.status(dc.InvoiceStatus.APPROVED)
|
||||
.execute();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> disputeInvoice({required String id, required String reason}) async {
|
||||
return _service.run(() async {
|
||||
await _service.connector
|
||||
.updateInvoice(id: id)
|
||||
.status(dc.InvoiceStatus.DISPUTED)
|
||||
.disputeReason(reason)
|
||||
.execute();
|
||||
});
|
||||
}
|
||||
|
||||
// --- MAPPERS ---
|
||||
|
||||
Invoice _mapInvoice(dynamic invoice) {
|
||||
final List<dynamic> rolesData = invoice.roles is List ? invoice.roles : [];
|
||||
final List<InvoiceWorker> workers = rolesData.map((dynamic r) {
|
||||
final Map<String, dynamic> role = r as Map<String, dynamic>;
|
||||
|
||||
// Handle various possible key naming conventions in the JSON data
|
||||
final String name = role['name'] ?? role['staffName'] ?? role['fullName'] ?? 'Unknown';
|
||||
final String roleTitle = role['role'] ?? role['roleName'] ?? role['title'] ?? 'Staff';
|
||||
final double amount = (role['amount'] as num?)?.toDouble() ??
|
||||
(role['totalValue'] as num?)?.toDouble() ?? 0.0;
|
||||
final double hours = (role['hours'] as num?)?.toDouble() ??
|
||||
(role['workHours'] as num?)?.toDouble() ??
|
||||
(role['totalHours'] as num?)?.toDouble() ?? 0.0;
|
||||
final double rate = (role['rate'] as num?)?.toDouble() ??
|
||||
(role['hourlyRate'] as num?)?.toDouble() ?? 0.0;
|
||||
|
||||
final dynamic checkInVal = role['checkInTime'] ?? role['startTime'] ?? role['check_in_time'];
|
||||
final dynamic checkOutVal = role['checkOutTime'] ?? role['endTime'] ?? role['check_out_time'];
|
||||
|
||||
return InvoiceWorker(
|
||||
name: name,
|
||||
role: roleTitle,
|
||||
amount: amount,
|
||||
hours: hours,
|
||||
rate: rate,
|
||||
checkIn: _service.toDateTime(checkInVal),
|
||||
checkOut: _service.toDateTime(checkOutVal),
|
||||
breakMinutes: role['breakMinutes'] ?? role['break_minutes'] ?? 0,
|
||||
avatarUrl: role['avatarUrl'] ?? role['photoUrl'] ?? role['staffPhoto'],
|
||||
);
|
||||
}).toList();
|
||||
|
||||
return Invoice(
|
||||
id: invoice.id,
|
||||
eventId: invoice.orderId,
|
||||
@@ -145,9 +200,23 @@ class BillingConnectorRepositoryImpl implements BillingConnectorRepository {
|
||||
addonsAmount: invoice.otherCharges ?? 0,
|
||||
invoiceNumber: invoice.invoiceNumber,
|
||||
issueDate: _service.toDateTime(invoice.issueDate)!,
|
||||
title: invoice.order?.eventName,
|
||||
clientName: invoice.business?.businessName,
|
||||
locationAddress: invoice.order?.teamHub?.hubName ?? invoice.order?.teamHub?.address,
|
||||
staffCount: invoice.staffCount ?? (workers.isNotEmpty ? workers.length : 0),
|
||||
totalHours: _calculateTotalHours(rolesData),
|
||||
workers: workers,
|
||||
);
|
||||
}
|
||||
|
||||
double _calculateTotalHours(List<dynamic> roles) {
|
||||
return roles.fold<double>(0.0, (sum, role) {
|
||||
final hours = role['hours'] ?? role['workHours'] ?? role['totalHours'];
|
||||
if (hours is num) return sum + hours.toDouble();
|
||||
return sum;
|
||||
});
|
||||
}
|
||||
|
||||
BusinessBankAccount _mapBankAccount(dynamic account) {
|
||||
return BusinessBankAccountAdapter.fromPrimitives(
|
||||
id: account.id,
|
||||
|
||||
@@ -21,4 +21,10 @@ abstract interface class BillingConnectorRepository {
|
||||
required String businessId,
|
||||
required BillingPeriod period,
|
||||
});
|
||||
|
||||
/// Approves an invoice.
|
||||
Future<void> approveInvoice({required String id});
|
||||
|
||||
/// Disputes an invoice.
|
||||
Future<void> disputeInvoice({required String id, required String reason});
|
||||
}
|
||||
|
||||
@@ -178,6 +178,28 @@ class StaffConnectorRepositoryImpl implements StaffConnectorRepository {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Benefit>> getBenefits() async {
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
final QueryResult<ListBenefitsDataByStaffIdData,
|
||||
ListBenefitsDataByStaffIdVariables> response =
|
||||
await _service.connector
|
||||
.listBenefitsDataByStaffId(staffId: staffId)
|
||||
.execute();
|
||||
|
||||
return response.data.benefitsDatas.map((data) {
|
||||
final plan = data.vendorBenefitPlan;
|
||||
return Benefit(
|
||||
title: plan.title,
|
||||
entitlementHours: plan.total?.toDouble() ?? 0.0,
|
||||
usedHours: data.current.toDouble(),
|
||||
);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> signOut() async {
|
||||
try {
|
||||
|
||||
@@ -40,6 +40,11 @@ abstract interface class StaffConnectorRepository {
|
||||
/// Throws an exception if the profile cannot be retrieved.
|
||||
Future<Staff> getStaffProfile();
|
||||
|
||||
/// Fetches the benefits for the current authenticated user.
|
||||
///
|
||||
/// Returns a list of [Benefit] entities.
|
||||
Future<List<Benefit>> getBenefits();
|
||||
|
||||
/// Signs out the current user.
|
||||
///
|
||||
/// Clears the user's session and authentication state.
|
||||
|
||||
Reference in New Issue
Block a user