# PBPS MIS — Version 2

This folder holds the Version 2 rebuild of the PBPS Monitoring & Verification MIS.

```
V2/
├── README.md                                  ← you are here
├── docs/
│   ├── 01_V2_MASTER_PLAN.md                   ← the full rebuild plan (architecture, every module, rules, API, roadmap)
│   ├── 02_DATABASE_REDESIGN_AND_MIGRATION.md  ← new schema (DDL), zero-loss migration SQL, reconciliation
│   ├── 03_FRONTEND_PROMPT.md                  ← page-by-page prompt for building the new UI
│   ├── 04_DB_CHANGE_LOG.md                    ← every database change
│   ├── 05_DEPLOYMENT.md                       ← deployment guide (V2 replaces V1)
│   └── 06_PARALLEL_DEPLOYMENT.md              ← V2 beside V1 on monitoring.icosconsult.com, read-only, rebuilt from V1 every night
└── Source code/
    ├── backend/                               ← V2 API, worker, migration and checks (see its README)
    └── frontend/                              ← V2 web app, connected to the API (no mock data)
```

## Reading order

1. **01_V2_MASTER_PLAN.md** — read sections 1–4 first (what is wrong today, why pages are slow, what changes).
2. **02_DATABASE_REDESIGN_AND_MIGRATION.md** — the new database and the step-by-step migration that keeps every existing record.
3. **03_FRONTEND_PROMPT.md** — the page-by-page design the web app was built from. The backend is then built against the API contract in the master plan, Part G.

## Non-negotiable principle

> **No existing data is lost.** Every row of the current `pbps_mis` database is either mapped into the new schema (with its original ID preserved) or archived verbatim. The old database is never modified by the migration; it is kept read-only as the permanent source of truth for audit.

## Run it on this computer

1. Start MySQL with `start-mysql.cmd` (port 3306), not with the XAMPP button.
2. Open `start-v2.cmd`. It starts the API, the background worker and the web app, then opens http://localhost:8080.

Databases: `v2_prod_<date>` (V2, named in `backend/.env`) and `v2_prod_<date>_v1_archive` (every V1 table, unchanged). `convert:production` makes both and then runs `correct:low-attendance`, the corrections decided on 2026-12-01. V1's `pbps_mis` is only read.

**Demo accounts.** `DEMO_LOGIN=on` in `Source code/backend/.env` shows one button per role on the login page. They work only on this computer, and the server refuses to start with them in production. Create them again after a rebuild: `npm run demo:users`.

**Convert a newer production database.** Export it from the server, then in `Source code/backend`: `npm run convert:production -- <dump.sql> --uploads <folder>`. It imports into new databases, refuses a V1 it does not fully know, migrates, checks every value, and prints the lines for `.env`. Nothing in use is touched.

**Backups.** The worker makes one every day after `BACKUP_HOUR`, or as soon as it is started again. Each backup is restored into a throwaway database and counted before it is called good. Kept: every backup of the last 14 days and the newest of each month. `BACKUP_COPY_DIR` adds a second copy elsewhere. To restore: create an empty database, then `npm run backup:restore -- <folder> <database>` with a `BACKUP_DATABASE_URL` user that may write to it.

Emails stay in the outbox while `MAIL_DELIVERY=off`.

## Where the build differs from the documents

The code and its checks are the authority. These points were changed on purpose while building:

| Topic | Documents say | Built |
|---|---|---|
| Database access | Prisma | Direct SQL through mysql2 (no row-conversion cost) |
| Archive | Some V1 tables | Every V1 table, verified with checksums |
| Class end date | Shifted by the migration | Copied: V1 already shifted it when the actual start changed |
| Trainees who dropped out | Later statuses "not applicable" | "No" for every later indicator, marked `derived_dropout` |
| Completion KPI | Per-trainee 80 % gate (R-1, R-2) | Attendance is judged per trainee. Provider averages never change a result. One missing session day blocks every report of the class |
| Registration | Freeze and change requests | Closes 14 days after class start; nothing can change until M&E reopens it. No change requests |
| Attendance marks | Present, absent, excused | Present or absent only. One import file holds one day. Late approval is a setting (off) |
| Grievances | Reply, then resolve | The answer resolves the case. Providers never see who sent it |
| Verification | Call attempts in the system | One "unreachable" step. Samples come from the people who passed the report before |
| Providers' access | Own indicators and verification results | They read their own result of each indicator on the dashboard. Indicator management, targets and verification are closed to them, on screen and in the API |
| Reports | Dropouts have no later lines | Every report holds the whole class: dropouts and earlier "no" are added automatically and marked |
| Activities | Per class | Per provider or per training, as in V1; what was due before V2 is waived as "pilot" and can still be submitted |
| Provider indicators | Percent | Number of providers, target 5 (official protocol) |
| Roles | World Bank and EDI share one role | Separate roles, as in V1 |
| Birth dates | Ethiopian calendar | Removed. Ordinary dates (DD/MM/YYYY); what V1 recorded is kept as written |
| Frontend data | Mock server | Removed; the app uses only the real API |
