# TumbuhSehat v2.2.0 — Release Notes & Rollback Guide

**Release date:** 2026-11-16
**Branch:** `main`
**Tag:** `v2.2.0`
**Database:** MariaDB 10.11 / MySQL, schema `tutumsehat`, `utf8mb4_general_ci`

---

## 1. What this release contains

v2.2.0 is the **multi-entity therapy platform** release. It delivers the complete
clinical workflow — consultation → therapy package → enrollment → sessions →
goal monitoring — with genuine cross-role integration between the five roles
in the system.

### 1.1 New therapy domain (schema v3 + v4)

| Table | Purpose |
|---|---|
| `clinics` | Multi-clinic tenancy; `clinic_id` on `users` and `children` |
| `therapy_packages` | Reusable therapy packages (type, session count, goals, price) |
| `enrollments` | A child's enrollment in a package, with therapist assignment and payment state |
| `enrollment_goals` | Per-enrollment therapy goals with attainment tracking |
| `doctor_schedules` / `doctor_appointments` | Doctor working hours and booked consultations |
| `app_versions` | Release history with git metadata and rollback support |

Additional columns: `scheduled_sessions.enrollment_id`,
`therapy_programs.package_id`, `users.clinic_id`, `children.clinic_id`.

### 1.2 Roles and tenancy

- `super_admin` — platform-wide, `clinic_id IS NULL`.
- `clinic_admin` — replaces the operational scope of the legacy `admin` role and
  is confined to a single clinic.
- `doctor`, `therapist`, `parent` — clinic-scoped through their children.

Clinic scope is enforced **server-side on both reads and writes**. There is no
hardcoded admin bypass: `manage` is a wildcard permission held by the admin
role, and `super_admin` is simply a role with no clinic restriction.

### 1.3 Cross-role integration

| Surface | Role | Data |
|---|---|---|
| Clinic monitoring | `clinic_admin` | Enrollments, session counters, goal attainment, pending reschedules |
| Session summary + latest therapist notes + parent feedback | `doctor` | Child-scoped, assignment-based |
| Queue banner + session history + latest parent feedback | `therapist` | Enrollment-ownership based |
| Upcoming schedule + reschedule requests + enrollment status | `parent` | Ownership-checked |

### 1.4 Frontend

Warm brand system (clay / sage / gold / sand / plum / ocean) replacing the
cold slate palette, with a redesigned login page, inline SVG artwork and
Lucide icons. Progress was refactored into **Tujuan Terapi** (goals) and
**Riwayat per Sesi** (per-session history).

### 1.5 Data-integrity fixes in this release

- `GET /api/progress/:childId` now derives its history series from `sessions`
  and treats `progress_history` as legacy enrichment only. Legacy rows are
  queued per date and consumed in order — direct row-level joins to `sessions`
  are forbidden because they fan out history rows.
- Enrollment `sessions_done` is synchronised from actual linked
  `scheduled_sessions` on every mutation path (status change, direct session
  update, scheduling, goal update, reschedule approval).
- Evaluation sessions are additional checkpoints and are **excluded** from
  regular completion ratios.
- Session dates use the local calendar day (WIB), never a UTC conversion.
- Parent reschedule requests are restricted to sessions the requesting parent
  owns; therapist requests require assignment.

---

## 2. Migration ordering

Migrations are additive and backward compatible. A database that has not
applied v3/v4 keeps working — `server/src/features.js` detects the optional
structures and the API degrades gracefully.

### 2.1 Applying the migrations to an existing database

```bash
# 1. back up first
mysqldump -u root --single-transaction --routines tumbuhsehat > tumbuhsehat-$(date +%F).sql

# 2. apply the additive schemas (idempotent: ALTER ... ADD COLUMN is guarded)
mysql -u root tumbuhsehat < db/schema_v3.sql
mysql -u root tumbuhsehat < db/schema_v4.sql

# 3. apply the seed data (ON DUPLICATE KEY UPDATE converges existing rows)
mysql -u root tumbuhsehat < db/seed_v3.sql
mysql -u root tumbuhsehat < db/seed_v4.sql
```

### 2.2 Building a database from scratch

```bash
npm run setup     # once
npm run db:init   # DESTRUCTIVE: drops and recreates the target database
```

`npm run db:init` applies the full chain in this order:

```
schema.sql  →  schema_v2.sql  →  schema_v3.sql  →  schema_v4.sql
seed.sql    →  seed_v2.sql    →  seed_v3.sql    →  seed_v4.sql
   ↓
generateLegacySessions()      (30 legacy v2 sessions + progress_history)
   ↓
seedSessionHistory()          (17 rich v4 sessions + targets + parent reports)
   ↓
seedDemoRescheduleRequests()  (3 pending parent reschedule requests)
```

The legacy generator must run **after** `seed_v2.sql` (it derives the v2
history from the v2 `scheduled_sessions`) and **before** `seed_v4.sql` (running
it later would create generic sessions for the v4 children and block the rich
v4 history). The v4 history seeder must run **after** `seed_v4.sql` because it
depends on the completed enrollment-linked `scheduled_sessions`.

> **Warning:** `init-db.js` drops its target database. The target is resolved
> from `INIT_DB_NAME` → `DB_NAME` → `tumbuhsehat`, and the name is printed
> before anything is dropped. Never point it at a database you need to keep.

### 2.3 Verifying a fresh install

```bash
npm run db:init                          # with INIT_DB_NAME set to a scratch db
npm run db:verify                        # scratch vs live: counts + 11 integrity groups
npm run db:verify:content                # byte-level v4 content parity
```

Expected row counts for a complete install:

```
users 38 | children 20 | clinics 3 | therapy_programs 18 | therapy_packages 8
enrollments 8 | enrollment_goals 18 | doctor_schedules 12 | doctor_appointments 19
scheduled_sessions 96 | sessions 47 | session_targets 127 | progress_history 36
parent_reports 47 | reschedule_requests 3 | app_versions 5
```

CI runs this automatically on every push to `main` (job `fresh-install`).

---

## 3. Rollback

### 3.1 Rolling back the application code

```bash
git tag -l                      # list release tags
git checkout <previous-tag>    # e.g. v2.1.0
npm run setup
npm run build
```

### 3.2 Rolling back the release record

The application exposes a version rollback endpoint for the super admin:

```
POST /api/admin/versions/:id/rollback
```

It demotes the currently released version to `deprecated` and promotes the
requested one back to `released`. Only a `super_admin` may call it, and the
currently active version cannot be rolled back onto itself.

### 3.3 Rolling back the database

The v3/v4 schema changes are additive, so the application keeps running
against a pre-v3 database with graceful degradation. To roll the schema back:

```bash
mysql -u root tumbuhsehat < backup.sql   # restore the pre-release dump
```

There is no automated down-migration: the v3/v4 tables and columns are left in
place, and the application simply stops using them. Restoring the backup is
the supported path.

### 3.4 Version lifecycle rules

`app_versions.status` is an enum: `draft`, `released`, `deprecated`, `retired`.

**Exactly one version is `released` at any time.** As of v2.2.0:

| Version | Status |
|---|---|
| 1.0.0 | `deprecated` |
| 1.1.0 | `deprecated` |
| 2.0.0 | `deprecated` |
| 2.1.0 | `draft` |
| 2.2.0 | `released` |

---

## 4. Deployment notes

- `railpack.json` uses the `steps/commands` format required by Railway:
  install steps copy the source, then run `npm install`,
  `npm --prefix server install` and `npm --prefix web install --include=dev`.
- The Vite dev proxy is environment-aware: `VITE_API_PROXY` wins, then
  `RAILWAY_ENVIRONMENT` selects port `8080`, otherwise `PORT` with a local
  fallback to `3020`.
- API port `3020`, frontend dev port `5175`.
- Passwords are stored as SHA-256 hashes; the API returns a raw token that
  must be sent as `Authorization: Bearer <token>`.

---

## 5. Verification results for v2.2.0

| Check | Result |
|---|---|
| `npm run typecheck` | 0 errors |
| `npm run build` | exit 0, 573.66 kB bundle |
| `npm run scan:secrets` | no secrets detected |
| `npm run test:v4:all` | **151 passed, 0 failed** (workflow 59, isolation 28, reschedule 17, parent 47) |
| `npm run db:verify` | all 11 integrity groups pass on scratch and live |
| `npm run db:verify:content` | v4 content parity OK (17 sessions, 37 targets, 17 reports, 3 requests) |
| Cross-role smoke test | 29 passed, 0 failed |
| CDP role sweep | 7/7 roles, zero console errors |
