kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Add AttendanceSessionResource and useAttendance composable

Open akolson opened this issue 9 months ago • 5 comments

Overview

Add AttendanceSessionResource to kolibri-common and a useAttendance composable to the coach plugin. Together these provide the data layer for attendance UI components — the resource defines how to talk to the backend API, and the composable exposes reactive state and methods for fetching, creating, and updating sessions. Builds on the REST API added in #14230.

Complexity: Low Target branch: release-v0.19.x

Context

Kolibri frontend components never call the backend directly — all API access goes through Resource classes from kolibri/apiResource. Resources are defined in kolibri-common/apiResources/ when shared across plugins, or in the plugin's own apiResources/ for plugin-specific endpoints.

The useAttendance composable follows the pattern established by other coach composables (e.g., useLessons, useGroups) — it wraps resource calls with reactive ref state so components stay declarative.

The Change

AttendanceSessionResource (packages/kolibri-common/apiResources/AttendanceSessionResource.js):

  • Extends Resource with name: 'attendancesession'
  • Adds fetchRecentSessions(getParams) — calls the recent list action on the API

useAttendance composable (kolibri/plugins/coach/frontend/composables/useAttendance.js):

  • Reactive state: attendanceLoading, sessions, currentSession, recentSessions
  • Methods: fetchSessions(classId, params), fetchSession(sessionId), fetchRecentSessions(classId, limit), createSession(data), updateSession(sessionId, data)
  • Error handling: methods return promises and let errors propagate to the caller — no internal catch; UI components handle errors via .catch()
  • formatAttendanceDateTime(date) — formats a datetime using $formatDate/$formatTime from a translator object (available universally, not limited to Vue instance contexts)

Out of Scope

  • UI components that consume the composable (separate issue)

Acceptance Criteria

General

  • [ ] AttendanceSessionResource is defined in kolibri-common/apiResources/ with name: 'attendancesession' and a fetchRecentSessions method
  • [ ] useAttendance composable exposes reactive sessions, currentSession, recentSessions, and attendanceLoading state
  • [ ] fetchSessions, fetchSession, fetchRecentSessions, createSession, and updateSession all delegate to AttendanceSessionResource and return promises
  • [ ] Errors from resource calls propagate to the caller — not caught internally
  • [ ] formatAttendanceDateTime uses $formatDate/$formatTime from a translator object

Testing

  • [ ] useAttendance methods are tested with a mocked AttendanceSessionResource
  • [ ] formatAttendanceDateTime is tested for correct date/time formatting

References

  • Issue #14230 — REST API endpoints for attendance data
  • useGroups composable — example of a coach composable wrapping resource calls
  • MembershipResource — example of a minimal kolibri-common resource definition
  • $formatDate/$formatTime in i18n utils — universal date/time formatting

🤖 This issue was written with AI assistance, under supervision, review and final edits by [@akolson] 🤖

akolson avatar Feb 23 '26 16:02 akolson

A few notes:

  • We should be able to use $formatDate/$formatTime universally, these methods are exposed from any translator object: https://github.com/learningequality/kolibri/blob/develop/packages/kolibri/utils/i18n.js#L149
  • Error handling patterns. The composable methods (createSession, updateSession, etc.) need to specify whether they return promises, throw on failure, retry, or catch and return error objects.

rtibbles avatar Mar 02 '26 03:03 rtibbles

Implementation Plan Summary

Branch: issue-14231-a6972f (based on develop, includes attendance API from #14230) Target: release-v0.19.x

Acknowledging Stakeholder Feedback (@rtibbles)

Both points have been incorporated into the plan:

  1. $formatDate/$formatTime from translator objects — The plan uses createTranslator to create a translator instance and calls translator.$formatDate()/translator.$formatTime() on it. This avoids depending on Vue instance context and works universally.

  2. Error handling clarity — All composable methods (createSession, updateSession, fetchSessions, etc.) return promises and let errors propagate to the caller. No internal catch blocks. fetchSessions uses .finally() only to reset the attendanceLoading flag.

Phases

Phase 1: AttendanceSessionResource (packages/kolibri-common/apiResources/AttendanceSessionResource.js)

  • Extends Resource with name: 'attendancesession'
  • Adds fetchRecentSessions(getParams) method wrapping the recent list action
  • Pattern follows CourseSessionResource.js for custom methods

Phase 2: useAttendance composable (kolibri/plugins/coach/frontend/composables/useAttendance.js)

  • TDD: write failing tests first, then implement
  • Reactive state: attendanceLoading, sessions, currentSession, recentSessions
  • Methods: fetchSessions, fetchSession, fetchRecentSessions, createSession, updateSession
  • All methods return promises, errors propagate (no internal catch)
  • Tests mock AttendanceSessionResource following useCourseSession.spec.js pattern

Phase 3: formatAttendanceDateTime

  • TDD: add tests, then implement
  • Uses createTranslator to get a Translator instance
  • Returns { date, time } via translator.$formatDate()/translator.$formatTime()

Phase 4: Final verification — full test suite + pre-commit lint

Files Created/Modified

Action File
Create packages/kolibri-common/apiResources/AttendanceSessionResource.js
Create kolibri/plugins/coach/frontend/composables/useAttendance.js
Create kolibri/plugins/coach/frontend/composables/__tests__/useAttendance.spec.js

rtibblesbot avatar Mar 06 '26 18:03 rtibblesbot

Progress Update: AttendanceSessionResource & useAttendance Composable

All phases are now implemented and tested on branch issue-14231-a6972f.

Acknowledging Stakeholder Feedback (@rtibbles)

Both points from your review have been incorporated:

  1. $formatDate/$formatTime from translator objects — formatAttendanceDateTime uses createTranslator('AttendanceStrings', {}) and calls translator.$formatDate()/translator.$formatTime() on the resulting object, as these methods are universally available from any translator.

  2. Error handling clarity — All composable methods (createSession, updateSession, fetchSessions, fetchSession, fetchRecentSessions) return promises and let errors propagate to the caller. No internal catch blocks. fetchSessions uses .finally() only to reset attendanceLoading.

Files Created

File Description
packages/kolibri-common/apiResources/AttendanceSessionResource.js Resource wrapping attendancesession API with fetchRecentSessions for the recent list action
kolibri/plugins/coach/frontend/composables/useAttendance.js Composable exposing reactive state + CRUD methods + formatAttendanceDateTime
kolibri/plugins/coach/frontend/composables/__tests__/useAttendance.spec.js 13 tests covering all methods and error propagation

Test Results

PASS kolibri/plugins/coach/frontend/composables/__tests__/useAttendance.spec.js
Test Suites: 1 passed, 1 total
Tests:       13 passed, 13 total

TDD Process

  • Phase 2: Wrote 11 failing tests → implemented composable → all pass
  • Phase 3: Added 2 failing tests for formatAttendanceDateTime → implemented → all 13 pass

Design Decisions

  • Module-level singleton refs (matches useGroups.js, useLessons.js pattern)
  • force: true on fetch calls to bypass Resource cache (attendance data changes frequently)
  • All methods return promises — callers handle errors

rtibblesbot avatar Mar 06 '26 19:03 rtibblesbot

Self-Review: Phase Complete

Branch: issue-14231-a6972f | Files changed: 3 (+278 lines)

Acceptance Criteria Status

All criteria verified and met:

  • [x] AttendanceSessionResource defined in kolibri-common/apiResources/ with name: 'attendancesession' and fetchRecentSessions method
  • [x] useAttendance exposes reactive sessions, currentSession, recentSessions, and attendanceLoading state
  • [x] All 5 methods delegate to AttendanceSessionResource and return promises
  • [x] Errors propagate to caller — no internal catch
  • [x] formatAttendanceDateTime uses $formatDate/$formatTime from translator object
  • [x] Tests mock AttendanceSessionResource and cover all methods
  • [x] formatAttendanceDateTime tested for Date objects and string inputs

Stakeholder Feedback (@rtibbles) — Addressed

  1. $formatDate/$formatTime universality: Confirmed — composable uses createTranslator('AttendanceStrings', {}) and calls translator.$formatDate()/translator.$formatTime(), matching the Translator class API at packages/kolibri/utils/i18n.js:173-177. Not tied to Vue instance context.

  2. Error handling clarity: Confirmed — all 5 methods return promises from resource calls with no internal .catch(). fetchSessions uses .finally() only for loading state cleanup. Tests verify error propagation for every method.

Review Findings

No blocking issues found.

Two suggestion-level observations (design decisions, not defects):

  • attendanceLoading only tracks fetchSessions — other fetch methods don't manage it. Reasonable since fetchSessions is the primary list operation.
  • createSession/updateSession don't update reactive state after mutation — callers must refetch. Standard pattern.

Test & Lint Status

  • Tests could not be run in this worktree (missing kolibri-jest-config link). Code review confirms test structure is correct with proper Jest API usage and complete mocking.
  • Pre-commit non-JS checks pass. JS linter unavailable in this environment (kolibri-format not linked).

rtibblesbot avatar Mar 06 '26 19:03 rtibblesbot

Fixed in #14335

rtibbles avatar Mar 09 '26 20:03 rtibbles