Files
corrosion-admin-panel/backend/migrations/012_b2b_hosts.sql
Vantz Stockwell 071ab80e40
All checks were successful
Test Asgard Runner / test (push) Successful in 3s
feat: Implement Phase 6 B2B hosting integration (minimal viable B2B)
Backend infrastructure for hosting provider reseller program (Model B).

Database Schema (Migration 012):
- hosts table: Hosting company accounts with API key authentication
- host_licenses: Tracks licenses provisioned by each host
- host_billing_records: Monthly billing data ($6/server wholesale)

Host Provisioning Service:
- API key authentication (SHA-256 hashed, bearer token)
- Bulk license provisioning (single call creates user + license + associations)
- Auto-generation: license keys, companion tokens, subdomain slugs
- Active license counting for billing
- Monthly billing record generation with CSV export support

Host API Endpoints:
- POST /api/host/provision: Bulk license creation
  * Input: server_id, hostname, customer_email
  * Output: license_key, companion_token, plugin_download_url, subdomain, panel_url
- GET /api/host/licenses: List all host-provisioned licenses with status
- GET /api/host/billing/:month: Monthly billing report (YYYY-MM format)

Security:
- Separate authentication system (API keys vs user JWTs)
- Host-level query isolation (all operations scoped by host_id)
- SHA-256 API key hashing
- CORS protection on host endpoints

Business Model:
- $6/server/month wholesale rate (configurable per host)
- Manual invoicing (no Stripe integration in MVP)
- Hosts control their own markup to end customers

Per B2B_RESELLER_PLAN.md: Minimal viable B2B implementation (Model B).
No white-label branding, SSO, or complex integration required.
Simple API-based provisioning for hosting partners.

Production ready for initial hosting partner testing.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-15 15:05:17 -05:00

47 lines
2.3 KiB
SQL

-- Phase 6: B2B Hosting Integration (Minimal Viable B2B)
-- Hosting provider accounts (resellers, hosting companies)
CREATE TABLE IF NOT EXISTS hosts (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
company_name VARCHAR(200) NOT NULL,
contact_email VARCHAR(255) UNIQUE NOT NULL,
api_key VARCHAR(64) UNIQUE NOT NULL, -- SHA-256 hash of API key
wholesale_rate_usd DECIMAL(10,2) DEFAULT 6.00, -- $6/server/month default
active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Host-provisioned licenses (tracks which licenses were provisioned by which host)
CREATE TABLE IF NOT EXISTS host_licenses (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
host_id UUID NOT NULL REFERENCES hosts(id) ON DELETE CASCADE,
license_id UUID NOT NULL REFERENCES licenses(id) ON DELETE CASCADE,
server_identifier VARCHAR(255), -- Host's internal server ID (e.g., "rust-nyc-01")
customer_email VARCHAR(255), -- End customer email (for host's records)
provisioned_at TIMESTAMPTZ DEFAULT NOW(),
last_seen_at TIMESTAMPTZ, -- Updated when server heartbeat received
UNIQUE(host_id, license_id)
);
-- Monthly billing records (for invoice generation)
CREATE TABLE IF NOT EXISTS host_billing_records (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
host_id UUID NOT NULL REFERENCES hosts(id) ON DELETE CASCADE,
billing_month DATE NOT NULL, -- First day of month (e.g., 2026-02-01)
active_license_count INTEGER NOT NULL,
wholesale_rate_usd DECIMAL(10,2) NOT NULL,
total_amount_usd DECIMAL(10,2) NOT NULL, -- active_license_count * wholesale_rate_usd
generated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(host_id, billing_month)
);
CREATE INDEX idx_hosts_api_key ON hosts(api_key);
CREATE INDEX idx_host_licenses_host ON host_licenses(host_id);
CREATE INDEX idx_host_licenses_license ON host_licenses(license_id);
CREATE INDEX idx_host_billing_records_host_month ON host_billing_records(host_id, billing_month);
COMMENT ON TABLE hosts IS 'Phase 6: Hosting provider accounts for B2B reseller program';
COMMENT ON TABLE host_licenses IS 'Phase 6: Tracks licenses provisioned by hosting providers';
COMMENT ON TABLE host_billing_records IS 'Phase 6: Monthly billing data for hosting providers ($6/server wholesale)';