385 lines
15 KiB
Markdown
385 lines
15 KiB
Markdown
# VitFit V2 — Data Model
|
|
|
|
## Entity Overview
|
|
|
|
```
|
|
Organization
|
|
├── Users (SystemAdmin | Owner | Coach | Client)
|
|
│ ├── DeviceTokens (per user, for remember-me / iPad kiosk)
|
|
│ └── CoachClientLinks (Coach ↔ Client many-to-many)
|
|
├── Branding (1-to-1 with Organization)
|
|
├── Videos
|
|
│ └── Categories
|
|
├── Workouts (group display workouts)
|
|
│ └── WorkoutItems (future: replaces video1-4 columns)
|
|
├── Timers
|
|
├── CrossfitWorkouts
|
|
├── ClientWorkouts (1-on-1 programming per client)
|
|
├── CalendarSessions (scheduled coach/client sessions)
|
|
└── AuditLogs
|
|
```
|
|
|
|
All tables except `AuditLogs` and `DeviceTokens` have an `organizationId` foreign key. Every query filters by the requesting user's `organizationId`. This is the tenant isolation boundary.
|
|
|
|
---
|
|
|
|
## Model Definitions
|
|
|
|
### Organization
|
|
|
|
```
|
|
Organizations
|
|
├── id INTEGER PK
|
|
├── name STRING NOT NULL "CrossFit Downtown"
|
|
├── slug STRING UNIQUE "crossfit-downtown" (URL-safe, future subdomain use)
|
|
├── timezone STRING DEFAULT 'UTC' IANA string e.g. "America/New_York"
|
|
├── features TEXT (JSON array) ["client-management", "calendar"]
|
|
├── isActive BOOLEAN DEFAULT true
|
|
├── licenseOwner STRING contact name for the license holder
|
|
├── licenseEmail STRING contact email for support
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
`timezone` is used server-side for any query that needs "today" relative to the gym's local time (e.g., "today's workouts"). All timestamps are still stored as UTC; this field only affects boundary calculations. See `API_STANDARDS.md` for the query pattern.
|
|
|
|
Notes:
|
|
- `slug` is auto-generated from `name` on creation; can be edited by SystemAdmin
|
|
- `features` controls which modules are accessible (see `PRODUCT_VISION.md`)
|
|
- One Organization is seeded at install time (the gym that purchased this instance)
|
|
|
|
---
|
|
|
|
### User
|
|
|
|
Replaces V1's `Users` table. Extended with `organizationId`, expanded `role`, `isActive`, and profile fields.
|
|
|
|
```
|
|
Users
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id (nullable for SystemAdmin)
|
|
├── username STRING UNIQUE NOT NULL
|
|
├── email STRING UNIQUE (optional for now; required for client portal later)
|
|
├── password STRING NOT NULL (bcrypt hash)
|
|
├── role ENUM('SystemAdmin', 'Owner', 'Coach', 'Client')
|
|
├── firstName STRING
|
|
├── lastName STRING
|
|
├── isActive BOOLEAN DEFAULT true (soft delete — deactivate, never hard delete)
|
|
├── lastLoginAt DATE
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
Notes:
|
|
- `organizationId` is NULL only for SystemAdmin accounts
|
|
- Deactivated users (`isActive: false`) cannot log in; their data and audit trail are preserved
|
|
- `email` is nullable in V2 but should be populated for clients who need a login/reset flow
|
|
- V1 `username` + `password` + `role` columns are preserved exactly — migration just adds new columns
|
|
|
|
---
|
|
|
|
### DeviceToken
|
|
|
|
Supports the iPad kiosk "remember this device" feature and "remember me" on personal devices.
|
|
|
|
```
|
|
DeviceTokens
|
|
├── id INTEGER PK
|
|
├── userId INTEGER FK → Users.id
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── token STRING UNIQUE NOT NULL (UUID or cryptographic random, stored hashed)
|
|
├── deviceName STRING "Gym iPad", "Jason's MacBook"
|
|
├── deviceType ENUM('kiosk', 'personal') kiosk = 90-day TTL, personal = 30-day TTL
|
|
├── lastUsedAt DATE
|
|
├── expiresAt DATE
|
|
├── isRevoked BOOLEAN DEFAULT false
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
Flow:
|
|
1. Login with "Remember this device" → server creates DeviceToken, returns it in response
|
|
2. Client stores token in localStorage as `deviceToken`
|
|
3. On page load: if JWT missing/expired, call `POST /auth/device-login` with stored token
|
|
4. Server finds non-revoked, non-expired token → issues fresh JWT → normal session
|
|
5. Owner/SystemAdmin can revoke device tokens from the user management UI
|
|
|
|
---
|
|
|
|
### Branding
|
|
|
|
One-to-one with Organization. Stores per-org visual customization.
|
|
|
|
```
|
|
Brandings
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id UNIQUE
|
|
├── appName STRING DEFAULT 'VitFit'
|
|
├── logoPath STRING (path within uploads/ or null for default)
|
|
├── primaryColor STRING DEFAULT '#00FF88' (CSS hex)
|
|
├── secondaryColor STRING DEFAULT '#4488FF'
|
|
├── workColor STRING DEFAULT '#00FF88' (timer work phase)
|
|
├── restColor STRING DEFAULT '#4488FF' (timer rest phase)
|
|
├── moveColor STRING DEFAULT '#FF4444' (timer move/transition phase)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
---
|
|
|
|
### Category
|
|
|
|
Unchanged from V1, plus `organizationId`.
|
|
|
|
```
|
|
Categories
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── name STRING NOT NULL
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
UNIQUE (organizationId, name)
|
|
```
|
|
|
|
---
|
|
|
|
### Video
|
|
|
|
Unchanged from V1, plus `organizationId` and `mimeType`.
|
|
|
|
```
|
|
Videos
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── filename STRING NOT NULL (stored in UPLOADS_DIR)
|
|
├── description STRING
|
|
├── category STRING (matches a Category.name — kept as string for V1 compat)
|
|
├── mimeType STRING (e.g., 'video/mp4' — validated on upload)
|
|
├── fileSizeBytes INTEGER (stored at upload time for storage reports)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
Notes:
|
|
- `category` kept as a string (not FK) to match V1 schema. A future migration can normalize this.
|
|
- `fileSizeBytes` added so storage reports don't need to re-stat every file on disk
|
|
|
|
---
|
|
|
|
### Workout (Group Display)
|
|
|
|
V1 structure preserved + `organizationId`. The 4-slot columns stay for backward compat.
|
|
|
|
```
|
|
Workouts
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── name STRING
|
|
├── workoutDate DATE
|
|
├── video1 INTEGER FK → Videos.id (nullable)
|
|
├── video2 INTEGER FK → Videos.id (nullable)
|
|
├── video3 INTEGER FK → Videos.id (nullable)
|
|
├── video4 INTEGER FK → Videos.id (nullable)
|
|
├── number1 INTEGER
|
|
├── number2 INTEGER
|
|
├── number3 INTEGER
|
|
├── number4 INTEGER
|
|
├── createdByUserId INTEGER FK → Users.id (new — who created it)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
---
|
|
|
|
### Timer
|
|
|
|
V1 structure preserved + `organizationId` + `name`.
|
|
|
|
```
|
|
Timers
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── name STRING NOT NULL (new — human-readable timer name)
|
|
├── totalTime INTEGER (seconds)
|
|
├── setTime INTEGER
|
|
├── restTime INTEGER
|
|
├── moveTime INTEGER
|
|
├── setCount INTEGER
|
|
├── exerciseCount INTEGER
|
|
├── lapCount INTEGER
|
|
├── createdByUserId INTEGER FK → Users.id
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
---
|
|
|
|
### CrossfitWorkout
|
|
|
|
V1 structure preserved + `organizationId` + `createdByUserId`.
|
|
|
|
```
|
|
CrossfitWorkouts
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── name STRING
|
|
├── workoutDate DATE
|
|
├── workoutItems TEXT (JSON array of strings)
|
|
├── createdByUserId INTEGER FK → Users.id
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
---
|
|
|
|
### CoachClientLink
|
|
|
|
Many-to-many relationship between Coach users and Client users.
|
|
|
|
```
|
|
CoachClientLinks
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── coachId INTEGER FK → Users.id (must have role = 'Coach')
|
|
├── clientId INTEGER FK → Users.id (must have role = 'Client')
|
|
├── isActive BOOLEAN DEFAULT true
|
|
├── assignedAt DATE
|
|
├── assignedByUserId INTEGER FK → Users.id (Owner or SystemAdmin who made the assignment)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
UNIQUE (coachId, clientId)
|
|
```
|
|
|
|
---
|
|
|
|
### ClientWorkout
|
|
|
|
A workout assigned to a specific client by their coach. Different from group Workouts — this is 1-on-1 programming.
|
|
|
|
```
|
|
ClientWorkouts
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── clientId INTEGER FK → Users.id
|
|
├── coachId INTEGER FK → Users.id
|
|
├── name STRING
|
|
├── scheduledDate DATE
|
|
├── notes TEXT (coach notes visible to client)
|
|
├── items TEXT (JSON array) (flexible: [{type, videoId, reps, sets, notes}])
|
|
├── completedAt DATE (nullable) (client or coach marks as done)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
`items` is a flexible JSON structure rather than fixed video slots:
|
|
```json
|
|
[
|
|
{ "type": "video", "videoId": 12, "reps": 10, "sets": 3, "notes": "Full ROM" },
|
|
{ "type": "text", "text": "200m run between sets" },
|
|
{ "type": "rest", "durationSeconds": 60 }
|
|
]
|
|
```
|
|
|
|
This avoids the 4-slot limitation of the group Workout model.
|
|
|
|
---
|
|
|
|
### CalendarSession
|
|
|
|
A scheduled session between a coach and client (or a group class).
|
|
|
|
```
|
|
CalendarSessions
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id
|
|
├── coachId INTEGER FK → Users.id
|
|
├── clientId INTEGER FK → Users.id (nullable — null = group/open session)
|
|
├── title STRING "Weekly Check-in", "Leg Day"
|
|
├── sessionType ENUM('personal', 'group', 'assessment', 'other')
|
|
├── startAt DATETIME NOT NULL
|
|
├── endAt DATETIME NOT NULL
|
|
├── locationNotes STRING "Gym floor B", "Zoom link: ..."
|
|
├── coachNotes TEXT (coach-only, client cannot see)
|
|
├── sharedNotes TEXT (both coach and client can see)
|
|
├── status ENUM('scheduled', 'completed', 'cancelled', 'no-show') DEFAULT 'scheduled'
|
|
├── linkedWorkoutId INTEGER FK → ClientWorkouts.id (nullable — the workout for this session)
|
|
├── createdAt DATE
|
|
└── updatedAt DATE
|
|
```
|
|
|
|
---
|
|
|
|
### AuditLog
|
|
|
|
Immutable record of all significant actions. Never updated or deleted. See `AUDIT_LOGS.md` for full event catalog.
|
|
|
|
```
|
|
AuditLogs
|
|
├── id INTEGER PK
|
|
├── organizationId INTEGER FK → Organizations.id (nullable for SystemAdmin actions)
|
|
├── userId INTEGER FK → Users.id (nullable for system-generated events)
|
|
├── userRole STRING (role at time of action — denormalized for log integrity)
|
|
├── action STRING NOT NULL e.g., 'video.upload', 'user.create', 'timer.delete'
|
|
├── entityType STRING 'video', 'workout', 'user', etc.
|
|
├── entityId INTEGER (nullable) ID of the affected record
|
|
├── details TEXT (JSON) {before: {...}, after: {...}, metadata: {...}}
|
|
├── ipAddress STRING
|
|
├── userAgent STRING
|
|
├── createdAt DATE (the only timestamp — audit logs have no updatedAt)
|
|
```
|
|
|
|
Note: `organizationId` and `userId` on AuditLog are **not** FK-constrained (no `REFERENCES` clause). This ensures log records survive even if the user or org is deleted. They are informational, not relational.
|
|
|
|
---
|
|
|
|
## Migration from V1
|
|
|
|
| V1 Table | V2 Table | Changes |
|
|
|----------|----------|---------|
|
|
| Users | Users | + organizationId, email, firstName, lastName, isActive, lastLoginAt |
|
|
| Videos | Videos | + organizationId, mimeType, fileSizeBytes |
|
|
| Categories | Categories | + organizationId |
|
|
| Workouts | Workouts | + organizationId, createdByUserId |
|
|
| Timers | Timers | + organizationId, name, createdByUserId |
|
|
| CrossfitWorkouts | CrossfitWorkouts | + organizationId, createdByUserId |
|
|
| (none) | Organizations | New — one seeded row |
|
|
| (none) | Brandings | New — one seeded row (default colors) |
|
|
| (none) | DeviceTokens | New |
|
|
| (none) | CoachClientLinks | New |
|
|
| (none) | ClientWorkouts | New |
|
|
| (none) | CalendarSessions | New |
|
|
| (none) | AuditLogs | New |
|
|
|
|
All V1 data is preserved. New columns are nullable or have defaults so existing rows are valid without backfilling.
|
|
|
|
---
|
|
|
|
## Soft Delete Policy
|
|
|
|
Defines which entities use soft delete vs hard delete and what happens to related data.
|
|
|
|
| Entity | Delete Type | Reason |
|
|
|--------|-------------|--------|
|
|
| User | Soft (`isActive = false`) | Preserve audit trail, workout history, coach-client history |
|
|
| Video | Hard delete + file removal | Files are large; no benefit to keeping orphaned video rows |
|
|
| Category | Hard delete (guarded) | Blocked if any Video references it |
|
|
| Workout (group) | Hard delete | Content, not records — no history value after removal |
|
|
| Timer | Hard delete | Configuration only |
|
|
| CrossfitWorkout | Hard delete | Content only |
|
|
| ClientWorkout | Soft (`deletedAt` timestamp) | Part of a client's training history; coach may want to recover |
|
|
| CalendarSession | Soft (`status = 'cancelled'`) | Already has a cancelled state; never truly deleted |
|
|
| CoachClientLink | Soft (`isActive = false`) | Client history must remain readable after coach reassignment |
|
|
| AuditLog | Never deleted | Immutable by design |
|
|
|
|
### Deleted Video + Historical Workout Integrity
|
|
|
|
When a Video is hard-deleted, group Workouts that reference it retain the FK values (now pointing to a deleted row). The API must handle this gracefully:
|
|
|
|
- `GET /api/v1/workouts` joins Videos with `LEFT JOIN` (not `INNER JOIN`) so workouts with deleted videos still appear
|
|
- The frontend displays `[Video removed]` in place of missing video details
|
|
- Workout functionality (display, audit history) is preserved even if the underlying video is gone
|
|
|
|
### Permanent Purge
|
|
|
|
Owners cannot permanently delete soft-deleted records through the UI. A SystemAdmin can purge via direct DB access if required (e.g., GDPR request). This is a manual operation and must be audit-logged externally (note in the customer file) since the log record cannot log its own deletion.
|