# CRM-002 Sales Command Center: deploy, promote, roll back

**Where it lives:** `admin.inboundgold.com/command/crm/`, a new folder beside Command Center v1 (`/command/`).

**Nothing is replaced:**
- No v1 file, admin portal file, admin database table, Airtable table, Zapier zap or n8n workflow is changed.
- No outreach and no spend.
- The only writes are verification events, corrections, undos and audit rows, all in the new `crm_*` tables.

**Gate:** install on **staging** only. Production (`mode => 'production'`, `entity_source => 'airtable'`) waits for CRM-001 acceptance and the mapping in SCHEMA-MAPPING.md §3.

## Files

| Upload from the zip | To |
|---|---|
| `command/crm/index.html`, `command/crm/api.php` | `admin.inboundgold.com/command/crm/` |
| `command/crm/lib/*` (includes `.htaccess` that blocks web access) | `admin.inboundgold.com/command/crm/lib/` |
| `command/crm/schema/*` (includes `.htaccess`) | `admin.inboundgold.com/command/crm/schema/` |

`docs/` and `tests/` are not uploaded.

## Staging install (owner, about 25 minutes, cPanel)

1. **Check the server.**
   - In cPanel → Terminal, run `php -v` (needs 8.1 or later) and `php -m | grep -i pdo_mysql`.
   - Note the free disk space shown in cPanel.
   - Record all three in Infrastructure Capability Inventory.
2. **Back up** the admin database: phpMyAdmin → Export → Quick → SQL.
3. **Create the tables:**
   - Upload the files.
   - Add the config block in step 4.
   - Run `php ~/admin.inboundgold.com/command/crm/lib/migrate.php`. It's safe to re-run.
   - Or import `schema/crm_mysql.sql` once in phpMyAdmin. Only do this if no `crm_*` tables exist yet.
4. **Add this block** to `admin.inboundgold.com/config.php`, just above the final `];`. Never paste secrets into a chat.

   ```php
   // Sales Command Center (CRM-002) at /command/crm/
   'crm' => [
     'mode' => 'staging',                 // 'production' only after CRM-001 acceptance
     'pdo'  => ['dsn' => 'mysql:host=localhost;dbname=ADMIN_DB_NAME;charset=utf8mb4', 'user' => 'ADMIN_DB_USER', 'pass' => 'ADMIN_DB_PASSWORD'],
     'users' => [                          // hashes: php -r 'echo password_hash("PASSWORD", PASSWORD_DEFAULT), PHP_EOL;'
       'alecia' => ['hash' => '$2y$...', 'role' => 'admin'],
       // 'name' => ['hash' => '$2y$...', 'role' => 'verifier'],   // can mark good/bad, undo own
       // 'name' => ['hash' => '$2y$...', 'role' => 'viewer'],     // read only
     ],
     'entity_source' => 'pdo',            // staging; 'airtable' after CRM-001
     'airtable_token' => '',              // optional now: read-only PAT (data.records:read, this base only) enables the Inbound leads tab
     'airtable_base'  => 'appKKKYKV482C7s1x',
     'airtable_map'   => [],              // SCHEMA-MAPPING.md §3, filled at CRM-001 acceptance
     'rework_hourly_rate_usd' => null,    // null = rework cost shown as Unknown
     'cache_seconds' => 30,
     'opening_inventory' => ['source' => 'COO reporting audit 2026-11-30 (reported, not system-verified)', 'items' => [
       ['label' => 'Airtable Leads records', 'value' => 0],
       ['label' => 'Apollo saved unique person IDs', 'value' => 810, 'live_key' => 'contacts'],
       ['label' => 'Apollo distinct organization IDs', 'value' => 654, 'live_key' => 'organizations'],
       ['label' => 'Apollo provider-verified emails', 'value' => 810],
       ['label' => 'Apollo US contacts', 'value' => 597],
     ]],
     'scenario' => ['qualified_target' => 33000, 'days' => 22, 'start_date' => null,   // set when the 22-day clock starts
       'leadcarrot' => ['cap_usd' => 212.50, 'raw' => 25000, 'assumed_yield' => 0.75, 'yield_verified' => false]],
     'integrations' => [                  // all off; set authorized/verified only after an owner-approved connection test
       'rosie' => ['authorized' => false, 'verified' => false], 'apollo' => ['authorized' => false, 'verified' => false],
       'leadcarrot' => ['authorized' => false, 'verified' => false], 'replyio' => ['authorized' => false, 'verified' => false],
       'stripe' => ['authorized' => false, 'verified' => false],
       'n8n' => ['authorized' => false, 'verified' => false, 'webhook_url' => '', 'secret' => '', 'send_test_events' => false],
     ],
   ],
   ```
5. **Optional synthetic data on staging.** On a staging database only, run the fixture loader from the zip: `php tests/seed_ui.php` points at SQLite. For MySQL, ask Claude for a loader that targets the staging DSN. Synthetic rows carry `is_test = 1` and are excluded in production mode.
6. **Smoke test** (about 5 minutes):
   - Open `/command/crm/` and sign in as admin. The badge reads **STAGING**.
   - Open `/command/crm/api.php?a=overview` in a private window. It must return **401 Sign in first**.
   - Open `/command/crm/lib/store.php` and `/command/crm/schema/crm_mysql.sql`. Both must be **403 Forbidden**. That proves `.htaccess` is active.
   - Open a contact and click ✓ on one field. You should see "Saved." The Audit tab shows a `verify` row.
   - Click Undo. The Audit tab shows an `undo` row.
   - `/command/` (v1) still loads unchanged.

## Promote to production (after CRM-001 acceptance)

1. Omni supplies the table and field IDs, which go into `airtable_map`.
2. Set the Airtable token: a read-only PAT scoped to this base.
3. Set `entity_source => 'airtable'` and `mode => 'production'`.
4. Reload and check:
   - The badge reads **PRODUCTION**.
   - Contacts and organizations match the Airtable counts.
   - No "synthetic" pills appear anywhere.
5. Integrations stay off until each one is authorized and a test is recorded:
   - **n8n** additionally needs a cron entry for `lib/outbox_send.php`. It sends HMAC-signed, server-side, and posts only to an `https://*.n8n.cloud/` address.
   - **Rosie / Apollo / Lead Carrot / Reply.io / Stripe** imports are **not built** in this increment. Each needs verified permission, an export entitlement and its storage path first, per the COO audit.

## Rollback (any time, about 5 minutes)

| Level | Action | Effect |
|---|---|---|
| 1. Disable | Remove the `'crm' => [...]` block from config.php | Page shows "not configured". Data is kept. |
| 2. Remove UI | Delete the `command/crm/` folder | v1 and the admin portal are unaffected (tested) |
| 3. Remove data | phpMyAdmin → Export tables `crm_*` (backup), then run `schema/crm_rollback.sql` | Drops only the 11 `crm_*` tables. Tested on MariaDB 10.11: a pre-existing admin table and its row survived. |
| Undo a single verdict | Undo button, or an admin undoes anyone's | New audit event. Nothing is deleted. |

Nothing outside `crm_*`, `command/crm/` and the `crm` config key is created, so rollback can't break the live site.

## Security notes

**Secrets**
- DB password, Airtable token and n8n secret live only in config.php. The browser never receives them.

**Sessions and requests**
- Session cookie `igcrm` is HttpOnly, SameSite=Strict, scoped to `/command/crm/`, and Secure on HTTPS.
- Every write needs the session CSRF token header.

**Login protection**
- Login is limited to 10 failures per username per 15 minutes, counted server-side from the audit log.
- For defence in depth, also put `/command/crm/` behind cPanel Directory Privacy.

**Roles and audit**
- Roles are enforced server-side. The UI hiding buttons is a convenience, not the control.
- Every login, verify, correct, undo, replay and denial is written to `crm_audit_log`, with the payload SHA-256 and a hashed IP.

**Known limitation**
- The report cache (`cache_seconds`) writes report JSON, including contact names, to the server temp folder for up to 30 seconds. Set it to 0 to disable.
