# crm.microcode.email Production Migration Scope

This document is the production gate for `crm.microcode.email` on `45.67.216.185`.

No deployment, migration, restart, file copy, deletion, permission change, or database write is approved until the scope below is confirmed immediately before release.

## Approved Scope

| Item | Value |
| --- | --- |
| Domain | `crm.microcode.email` |
| Server IP | `45.67.216.185` |
| cPanel account | `microcode` |
| cPanel owner | `root` |
| Application root | `/home/microcode/public_html/crm.microcode.email` |
| Document root | `/home/microcode/public_html/crm.microcode.email` |
| Public web root | `/home/microcode/public_html/crm.microcode.email/server/public` |
| Startup file | `/home/microcode/public_html/crm.microcode.email/app.js` |
| Runtime | Passenger Node.js |
| Node binary | `/opt/cpanel/ea-nodejs22/bin/node` |
| PHP version on vhost | `ea-php81` |
| Database | `/home/microcode/public_html/crm.microcode.email/server/db/office365.sqlite` |
| Database WAL | `/home/microcode/public_html/crm.microcode.email/server/db/office365.sqlite-wal` |
| Database SHM | `/home/microcode/public_html/crm.microcode.email/server/db/office365.sqlite-shm` |
| Logs | `/etc/apache2/logs/domlogs/crm.microcode.email*` |
| SSL certificate path | `/var/cpanel/ssl/apache_tls/crm.microcode.email/combined` |
| CRM-specific cron | None detected |
| CRM-specific queue worker | None detected |
| Git repository | None detected in production app root |

## Required Backups Before Production

Store backups outside any public web root, preferably under `/root/crm-microcode-email-backups/<timestamp>/`.

Required files:

- Application archive of `/home/microcode/public_html/crm.microcode.email`
- `.env` copy with restricted permissions
- SQLite backup of `server/db/office365.sqlite`
- WAL/SHM handling record, or a clean SQLite `.backup` created while app writes are paused
- Apache vhost/userdata record for `crm.microcode.email`
- Passenger config record for `crm.microcode.email`
- Cron record for user `microcode` and matching root/system entries
- Current production asset manifest under `server/public/assets`

## Data Mapping

| Existing table | Existing columns | New/retained table | New/retained columns | Rule | Rollback |
| --- | --- | --- | --- | --- | --- |
| `invoice_settings` | `payment_links` | `payment_gateways` | `name`, `payment_url`, `supported_currencies`, `is_default`, `is_active` | Seed the first valid legacy payment link as default gateway only when no gateways exist. Keep legacy links. | Archive inserted gateway; legacy links remain intact. |
| `invoices` | `payment_links` | `invoices` | `payment_gateway_id`, `payment_gateway_snapshot` | New unpaid invoices store selected gateway snapshot. Historical payment links remain unchanged. | Ignore nullable columns; render falls back to historical `payment_links`. |
| `invoices` | `payment_status`, `order_id`, `total_cents`, `currency` | `orders`, `finance_sales` | existing relationship fields | Link paid invoice to sales only when a safe order/sale relationship exists. Do not create duplicate sales. | Restore DB backup or remove rows recorded by migration mapping. |
| `orders` | `sales_agent_id`, `finance_payment_mode_id`, `status_updated_at` | `finance_sales` | `agent_id`, `payment_mode_id`, `sale_date`, `reserve_release_date` | Preserve historical sale date and update linked finance date only through approved app logic. | Restore DB backup if finance totals fail reconciliation. |
| `finance_sales` | amount/currency/reserve/commission columns | `finance_sales` | retained columns | Preserve values. Do not recalculate historical financial totals during migration. | Restore DB backup. |
| `email_logs` | all columns | `email_logs` | retained columns | Preserve invoice and lead email history. | Restore DB backup. |
| `customers`, `leads`, `orders` | IDs and relationship columns | retained tables | retained IDs | Preserve primary keys and relationships. | Restore DB backup. |

## Audit Command

Run against staging copy first:

```bash
npm run migration:audit -- --db /path/to/staging-office365.sqlite --output migration-audits
```

Run against production only as read-only inspection:

```bash
cd /home/microcode/public_html/crm.microcode.email
/opt/cpanel/ea-nodejs22/bin/npm run migration:audit -- --db server/db/office365.sqlite --output /root/crm-microcode-email-backups/<timestamp>/audit
```

## Stop Conditions

Stop release if any of these occur:

- Approved app path cannot be confirmed.
- Any resolved target path is outside `/home/microcode/public_html/crm.microcode.email`.
- Database backup is missing, empty, or unreadable.
- Migration audit fails.
- Financial counts or totals differ without documented explanation.
- Invoice paid/unpaid counts differ unexpectedly.
- Lead/customer/order counts differ unexpectedly.
- Existing paid invoices would generate duplicate sales.
- Any command would affect another cPanel account, domain, Apache vhost, PHP version, cron, or shared service.

