OpenDataCapture icon indicating copy to clipboard operation
OpenDataCapture copied to clipboard

Add username to export

Open david-roper opened this issue 4 months ago • 1 comments

will close issue #1220

problems with collecting sessions for then use the userids in the subject table export

Summary by CodeRabbit

  • New Features

    • Username included in all exported instrument data (CSV, TSV, Excel, JSON)
    • Sessions now surface associated user information for clearer tracing
  • Bug Fixes

    • Fixed null subject handling in session displays
    • Improved export validation and user-facing error reporting
  • Refactor

    • Exports now wait for records to populate before download, ensuring complete data
    • Better session-to-record matching so Username consistently appears
  • Tests

    • Updated download tests to reliably await data population before asserting outputs

✏️ Tip: You can customize this high-level summary in your review settings.

david-roper avatar Nov 05 '25 18:11 david-roper

Walkthrough

Fetches sessions with usernames and enriches instrument-visualization records with a Username field used in CSV/TSV/Excel/JSON exports; adds a frontend query hook, backend controller/service method to return SessionWithUser, updates schema and tests, and fixes a duplicate sheet append in the Excel util.

Changes

Cohort / File(s) Summary
Backend session endpoints
apps/api/src/sessions/sessions.controller.ts, apps/api/src/sessions/sessions.service.ts
Add findAllIncludeUsernames() controller and service methods to return sessions including user.username with access control and optional groupId; minor guard when adding subject groups in create.
Schema updates
packages/schemas/src/session/session.ts
Make subject nullable and add $SessionWithUser, SessionWithUser and SessionWithUserQueryParams to represent sessions including optional user.username.
Frontend session query hook
apps/web/src/hooks/useFindSessionQuery.ts
New useFindSessionQuery React Query hook that GETs /v1/sessions, validates response with $SessionWithUser.array() and returns UseQueryResult<SessionWithUser[]>.
Instrument visualization logic
apps/web/src/hooks/useInstrumentVisualization.ts
Refactor to async fetchRecords flow, integrate useFindSessionQuery, enrich records with Username (wide and long formats), and add error handling/notifications.
Visualization tests
apps/web/src/hooks/__tests__/useInstrumentVisualization.test.ts
Update tests to waitFor async record population, mock sessions with username, and adjust expected CSV/TSV/Excel/JSON outputs to include Username.
Export utility fix
apps/web/src/utils/excel.ts
Remove duplicate sheet append and use sanitized sheet name for appended sheet.
UI null-safety
apps/web/src/components/Sidebar/Sidebar.tsx, apps/web/src/routes/_app/instruments/render/$id.tsx
Add non-null assertions (subject! / subject!.id) and explicit ?? undefined to align with nullable subject.

Sequence Diagram

sequenceDiagram
    participant UI as Component
    participant Hook as useInstrumentVisualization
    participant SessionsQ as useFindSessionQuery
    participant API as /v1/sessions
    participant Export as ExportGenerator

    UI->>Hook: Trigger export
    Hook->>SessionsQ: Ensure sessions fetched (enabled)
    SessionsQ->>API: GET /v1/sessions (params)
    API-->>SessionsQ: SessionWithUser[] (includes user.username)
    SessionsQ-->>Hook: Sessions with usernames

    rect rgb(230,245,235)
    Note over Hook: Async enrichment of records
    Hook->>Hook: fetchRecords() match by sessionId
    Hook->>Hook: attach Username from session.user.username
    Hook->>Hook: set enriched records state
    end

    Hook->>Export: Build rows (include Username)
    Export-->>UI: Downloadable file (CSV/TSV/Excel/JSON)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Check access control and empty-result handling in findAllIncludeUsernames.
  • Verify async enrichment in useInstrumentVisualization.ts for race conditions and proper error notifications.
  • Confirm places using subject! are safe given the schema change.
  • Validate updated tests correctly await and assert Username presence.

Possibly related PRs

  • DouglasNeuroInformatics/OpenDataCapture#1210 — modifies instrument export logic and tests to add user/subject identifier columns and long-format exports; closely related.
  • DouglasNeuroInformatics/OpenDataCapture#1224 — surfaces session user information into exports and session includes; directly related to threading username into exports.
  • DouglasNeuroInformatics/OpenDataCapture#982 — earlier changes to the same visualization hook; related to robustness and export behavior adjustments.

Suggested reviewers

  • joshunrau

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and directly describes the main change: adding username functionality to export features across multiple formats.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 05 '25 18:11 coderabbitai[bot]

closed due to updated pr #1238

david-roper avatar Nov 26 '25 20:11 david-roper