# ntini773/sleeplabs architecture

> Architecture diagram of the GitHub repository ntini773/sleeplabs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ntini773/sleeplabs
- Repository: https://github.com/ntini773/sleeplabs

## Overview

Resting Notes is a browser-based listening study with delayed email invitations to a reflection survey and an administrator dashboard. The participant workflow is enrollment and randomized session creation, timed listening, server-side qualification, invitation delivery, authenticated reflection access and submission. The app also supports returning participants and resending invitations; administrators can review study progress. Several function implementations and the admin page are not excerpted, so their major responsibilities are included from the file tree, but uncertain connections are omitted. The database schema is represented by its initial migration.

Main entry points: [`app/study-app.tsx`](https://github.com/ntini773/sleeplabs/blob/main/app/study-app.tsx) (Study app), [`supabase/functions/resend-invitation/index.ts`](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/resend-invitation/index.ts) (Resend invitation), [`supabase/migrations/001_study_schema.sql`](https://github.com/ntini773/sleeplabs/blob/main/supabase/migrations/001_study_schema.sql) (Study database), [`supabase/functions/morning-invitations/index.ts`](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/morning-invitations/index.ts) (Invitation job)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_participant["Participant experience"]
  node_study_ui["Study app<br/>[study-app.tsx]"]
  node_home["Home route<br/>[page.tsx]"]
end

subgraph group_study["Study session services"]
  node_start_session["Start session<br/>[index.ts]"]
  node_qualify_session["Qualify session<br/>[index.ts]"]
  node_status_check["Study status<br/>[index.ts]"]
  node_study_db[("Study database")]
end

subgraph group_reflection["Reflection delivery"]
  node_survey_access["Survey access<br/>[index.ts]"]
  node_morning_invites["Invitation job<br/>[index.ts]"]
  node_resend_invite["Resend invitation<br/>[index.ts]"]
end

subgraph group_operations["Study operations"]
  node_admin_page["Admin page<br/>[page.tsx]"]
  node_admin_dashboard["Admin dashboard API<br/>[index.ts]"]
  node_http_helpers["HTTP helpers<br/>[http.ts]"]
  node_log_helpers["Safe logging<br/>[log.ts]"]
end

node_participant_actor(("Participant"))
node_resend_service["Resend email"]
node_auth_service["Supabase Auth"]

node_participant_actor -->|"opens"| node_home
node_home -->|"renders"| node_study_ui
node_study_ui -->|"checks status"| node_status_check
node_study_ui -.->|"starts session"| node_start_session
node_study_ui -.->|"reports listening"| node_qualify_session
node_study_ui -.->|"checks or submits"| node_survey_access
node_study_ui -.->|"requests resend"| node_resend_invite
node_start_session -->|"creates participant and session"| node_study_db
node_qualify_session -->|"checks and updates session"| node_study_db
node_status_check -.->|"reads session"| node_study_db
node_survey_access -->|"reads and saves reflection"| node_study_db
node_survey_access -->|"validates user"| node_auth_service
node_morning_invites -->|"claims queued sessions"| node_study_db
node_morning_invites -->|"sends invitations"| node_resend_service
node_morning_invites -->|"generates sign-in link"| node_auth_service
node_resend_invite -->|"checks and updates session"| node_study_db
node_resend_invite -->|"sends replacement email"| node_resend_service
node_resend_invite -->|"generates sign-in link"| node_auth_service
node_admin_dashboard -->|"loads study records"| node_study_db
node_admin_dashboard -->|"checks admin identity"| node_auth_service
node_admin_page -.->|"loads dashboard data"| node_admin_dashboard
node_start_session -->|"uses response and hash helpers"| node_http_helpers
node_qualify_session -->|"uses response and hash helpers"| node_http_helpers
node_survey_access -->|"uses response and auth helpers"| node_http_helpers
node_morning_invites -->|"uses response helpers"| node_http_helpers
node_morning_invites -->|"redacts log messages"| node_log_helpers
node_resend_invite -->|"uses response and hash helpers"| node_http_helpers
node_resend_invite -->|"redacts log messages"| node_log_helpers
node_admin_dashboard -->|"uses response and auth helpers"| node_http_helpers

click node_study_ui "https://github.com/ntini773/sleeplabs/blob/main/app/study-app.tsx"
click node_home "https://github.com/ntini773/sleeplabs/blob/main/app/page.tsx"
click node_start_session "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/start-session/index.ts"
click node_qualify_session "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/qualify-session/index.ts"
click node_status_check "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/study-status/index.ts"
click node_study_db "https://github.com/ntini773/sleeplabs/blob/main/supabase/migrations/001_study_schema.sql"
click node_survey_access "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/survey-access/index.ts"
click node_morning_invites "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/morning-invitations/index.ts"
click node_resend_invite "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/resend-invitation/index.ts"
click node_admin_page "https://github.com/ntini773/sleeplabs/blob/main/app/admin/page.tsx"
click node_admin_dashboard "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/admin-dashboard/index.ts"
click node_http_helpers "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/_shared/http.ts"
click node_log_helpers "https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/_shared/log.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_study_ui,node_home toneBlue
class node_start_session,node_qualify_session,node_status_check,node_study_db toneAmber
class node_survey_access,node_morning_invites,node_resend_invite toneMint
class node_admin_page,node_admin_dashboard,node_http_helpers,node_log_helpers toneRose
class node_participant_actor,node_resend_service,node_auth_service toneIndigo
```

## Components

### Participant experience

- [Study app](https://github.com/ntini773/sleeplabs/blob/main/app/study-app.tsx): `app/study-app.tsx`
- [Home route](https://github.com/ntini773/sleeplabs/blob/main/app/page.tsx): `app/page.tsx`

### Study session services

- [Start session](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/start-session/index.ts): `supabase/functions/start-session/index.ts`
- [Qualify session](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/qualify-session/index.ts): `supabase/functions/qualify-session/index.ts`
- [Study status](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/study-status/index.ts): `supabase/functions/study-status/index.ts`
- [Study database](https://github.com/ntini773/sleeplabs/blob/main/supabase/migrations/001_study_schema.sql): `supabase/migrations/001_study_schema.sql`

### Reflection delivery

- [Survey access](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/survey-access/index.ts): `supabase/functions/survey-access/index.ts`
- [Invitation job](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/morning-invitations/index.ts): `supabase/functions/morning-invitations/index.ts`
- [Resend invitation](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/resend-invitation/index.ts): `supabase/functions/resend-invitation/index.ts`

### Study operations

- [Admin page](https://github.com/ntini773/sleeplabs/blob/main/app/admin/page.tsx): `app/admin/page.tsx`
- [Admin dashboard API](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/admin-dashboard/index.ts): `supabase/functions/admin-dashboard/index.ts`
- [HTTP helpers](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/_shared/http.ts): `supabase/functions/_shared/http.ts`
- [Safe logging](https://github.com/ntini773/sleeplabs/blob/main/supabase/functions/_shared/log.ts): `supabase/functions/_shared/log.ts`

### Other components

- **Participant**
- **Resend email**
- **Supabase Auth**

## Connections

- Participant → Home route: opens (evidence: `app/page.tsx`)
- Home route → Study app: renders (evidence: `app/page.tsx`)
- Study app → Study status: checks status (evidence: `app/study-app.tsx`)
- Study app → Start session: starts session
- Study app → Qualify session: reports listening
- Study app → Survey access: checks or submits
- Study app → Resend invitation: requests resend
- Start session → Study database: creates participant and session (evidence: `supabase/functions/start-session/index.ts`)
- Qualify session → Study database: checks and updates session (evidence: `supabase/functions/qualify-session/index.ts`)
- Study status → Study database: reads session
- Survey access → Study database: reads and saves reflection (evidence: `supabase/functions/survey-access/index.ts`)
- Survey access → Supabase Auth: validates user (evidence: `supabase/functions/survey-access/index.ts`)
- Invitation job → Study database: claims queued sessions (evidence: `supabase/functions/morning-invitations/index.ts`)
- Invitation job → Resend email: sends invitations (evidence: `supabase/functions/morning-invitations/index.ts`)
- Invitation job → Supabase Auth: generates sign-in link (evidence: `supabase/functions/morning-invitations/index.ts`)
- Resend invitation → Study database: checks and updates session (evidence: `supabase/functions/resend-invitation/index.ts`)
- Resend invitation → Resend email: sends replacement email (evidence: `supabase/functions/resend-invitation/index.ts`)
- Resend invitation → Supabase Auth: generates sign-in link (evidence: `supabase/functions/resend-invitation/index.ts`)
- Admin dashboard API → Study database: loads study records (evidence: `supabase/functions/admin-dashboard/index.ts`)
- Admin dashboard API → Supabase Auth: checks admin identity (evidence: `supabase/functions/admin-dashboard/index.ts`)
- Admin page → Admin dashboard API: loads dashboard data
- Start session → HTTP helpers: uses response and hash helpers (evidence: `supabase/functions/start-session/index.ts`)
- Qualify session → HTTP helpers: uses response and hash helpers (evidence: `supabase/functions/qualify-session/index.ts`)
- Survey access → HTTP helpers: uses response and auth helpers (evidence: `supabase/functions/survey-access/index.ts`)
- Invitation job → HTTP helpers: uses response helpers (evidence: `supabase/functions/morning-invitations/index.ts`)
- Invitation job → Safe logging: redacts log messages (evidence: `supabase/functions/morning-invitations/index.ts`)
- Resend invitation → HTTP helpers: uses response and hash helpers (evidence: `supabase/functions/resend-invitation/index.ts`)
- Resend invitation → Safe logging: redacts log messages (evidence: `supabase/functions/resend-invitation/index.ts`)
- Admin dashboard API → HTTP helpers: uses response and auth helpers (evidence: `supabase/functions/admin-dashboard/index.ts`)
