fetchOrganizationDetails method
Fetches organizations from the database based on the date range.
Updates the state with a list of OrganizationDetailsModel containing counts for devices, mothers, and tests.
Implementation
Future<void> fetchOrganizationDetails() async {
emit(state.copyWith(status: OrganizationStatus.loading));
try {
// First, fetch all organizations
final organizations = await _fetchOrganizationsFromDb(
fromDate: state.fromDate,
tillDate: state.tillDate,
);
// Create a list to store all organization details
final List<OrganizationDetailsModel> organizationDetails = [];
// For each organization, fetch the counts from other collections
for (final org in organizations) {
final String orgId = org.$id;
// Fetch counts for this organization from different collections
final deviceCount = await _getDeviceCount(orgId);
final motherCount = await _getMotherCount(orgId);
final testCount = await _getTestCount(orgId);
final doctorCount = await _getDoctorCount(orgId);
// Create an OrganizationDetailsModel for this organization
organizationDetails.add(
OrganizationDetailsModel(
organizations: [org],
deviceCount: deviceCount,
motherCount: motherCount,
testCount: testCount,
doctorCount: doctorCount,
),
);
}
emit(
state.copyWith(
organizationDetails: organizationDetails,
filteredOrganizationDetails: organizationDetails,
status: OrganizationStatus.loaded,
clearError: true,
),
);
applySearchFilter();
} catch (e) {
emit(
state.copyWith(
status: OrganizationStatus.error,
errorMessage: "Error fetching organization details: $e",
),
);
}
}