# v0.4.0 Multi-Tenant Subdomain Foundation

This version moves the app toward Option 2: one shared app codebase with separate tenant data for each subdomain.

## Included tenants

The default tenant map is in `config/tenants.json`:

- `pestdemo.recovermissedcall.com` -> tenant `pestdemo`
- `hvacdemo.recovermissedcall.com` -> tenant `hvacdemo`

Each tenant keeps its own data file:

- `data/tenants/pestdemo/db.json`
- `data/tenants/hvacdemo/db.json`

## What changed technically

- Added hostname-based tenant routing.
- Added isolated tenant data folders.
- Added automatic migration of the old legacy `data/db.json` into `data/tenants/pestdemo/db.json` if the new pest tenant data file does not exist yet.
- Added `config/tenants.json` for subdomain-to-tenant mapping.
- Added `scripts/migrate-to-tenants.js`.
- Added default HVAC demo business seed data.
- Updated health endpoint to show active tenant information.

## Critical deployment note

This code can serve both subdomains from the same app process if your hosting can point both hostnames to one Node app.

If cPanel/CloudLinux does not support multiple subdomains pointing to one Node app process, use the same codebase folder or symlinked app root for both Node app entries. You may still have to restart both Node app entries, but you only maintain one code folder.

## Recommended folder layout

```text
/home/recoverm/fallback-front-desk-core
  app.js
  src/
  public/
  config/tenants.json
  data/tenants/pestdemo/db.json
  data/tenants/hvacdemo/db.json
```

## Required .env additions

```env
DEFAULT_TENANT_ID=pestdemo
LEGACY_MIGRATION_TENANT_ID=pestdemo
TENANT_DATA_ROOT=/home/recoverm/fallback-front-desk-core/data/tenants
```

## Migration path from current pestdemo app

1. Back up current live app.
2. Back up current `.env`.
3. Back up current `data/db.json`.
4. Upload v0.4.0 to the shared core app folder.
5. Copy the old pest data file to:

```text
/home/recoverm/fallback-front-desk-core/data/tenants/pestdemo/db.json
```

6. Start/restart the app.
7. Visit:

```text
https://pestdemo.recovermissedcall.com/api/health
https://hvacdemo.recovermissedcall.com/api/health
```

Each should show a different `tenant.id`.

## Telnyx setup after this patch

Use tenant-specific tool URLs:

```text
Pest: https://pestdemo.recovermissedcall.com/api/ai-tools/submit-lead
HVAC: https://hvacdemo.recovermissedcall.com/api/ai-tools/submit-lead
```

The same app code will save the lead into the correct tenant based on the hostname.

## Important

Do not share one Telnyx Assistant between pest and HVAC unless its prompt is generic enough. For now, create one Telnyx Assistant per industry demo and use each tenant's generated prompt from System Settings.
