getActiveSession method

Future<LearningSessionModel?> getActiveSession({
  1. required String topicId,
  2. required String sessionType,
})

Returns the most recent active (not ended) session for the given topic and session type, or null if none exists.

Implementation

Future<LearningSessionModel?> getActiveSession({
  required String topicId,
  required String sessionType,
}) async {
  final userId = _client.auth.currentUser?.id;
  if (userId == null) return null;

  final response = await _client
      .from('learning_sessions')
      .select()
      .eq('user_id', userId)
      .eq('topic_id', topicId)
      .eq('session_type', sessionType)
      .isFilter('ended_at', null)
      .order('started_at', ascending: false)
      .limit(1)
      .maybeSingle();

  if (response == null) return null;
  return LearningSessionModel.fromJson(response);
}