prepareTrend method

List<Map<String, dynamic>> prepareTrend(
  1. List<Document> docs,
  2. String type
)

Processes a list of documents and returns a time-bucketed list of doctor registrations.

docs - List of Appwrite documents. type - Either 'weekly' or 'monthly'.

Implementation

List<Map<String, dynamic>> prepareTrend(
  List<models.Document> docs,
  String type,
) {
  final trendMap = <String, int>{};

  for (final doc in docs) {
    final createdOn = doc.data['createdOn'];
    if (createdOn == null) continue;

    final date = DateTime.parse(createdOn);
    final key =
        (type == 'weekly')
            ? '${date.year}-W${getWeek(date)}'
            : DateFormat('yyyy-MM').format(date);

    trendMap[key] = (trendMap[key] ?? 0) + 1;
  }

  final result =
      trendMap.entries
          .map((e) => {'createdOn': e.key, 'noOfDoctors': e.value})
          .toList();

  result.sort(
    (a, b) => (a['createdOn'] as String).compareTo(b['createdOn'] as String),
  );
  return result;
}