getPathEnrollments method

Future<List<LearningPathEnrollmentModel>> getPathEnrollments(
  1. String pathId
)

Implementation

Future<List<LearningPathEnrollmentModel>> getPathEnrollments(
  String pathId,
) async {
  final response = await _client
      .from('learning_path_enrollments')
      .select()
      .eq('path_id', pathId)
      .order('enrolled_at');
  final enrollments = (response as List<dynamic>)
      .cast<Map<String, dynamic>>();

  // Fetch profile labels separately — learning_path_enrollments FK points to
  // auth.users, not profiles, so PostgREST cannot resolve a join.
  final userIds = enrollments.map((e) => e['user_id'] as String).toSet();
  final profileMap = <String, Map<String, dynamic>>{};
  if (userIds.isNotEmpty) {
    final profiles = await _client
        .from('profiles')
        .select('id, display_name, email, username')
        .inFilter('id', userIds.toList());
    for (final p in profiles as List<dynamic>) {
      final pm = p as Map<String, dynamic>;
      profileMap[pm['id'] as String] = pm;
    }
  }

  return enrollments.map((r) {
    // Inject a 'profiles' key so fromJson can parse the user label.
    final uid = r['user_id'] as String;
    if (profileMap.containsKey(uid)) {
      r['profiles'] = profileMap[uid];
    }
    return LearningPathEnrollmentModel.fromJson(r);
  }).toList();
}