railyard/apps/rago/crates/rago-db/migrations/0035_integrations_oauth_sso.sql
caelum0x b93dae1507 feat(rago): progressive billing, 3DS actions, per-org numbering, OAuth/SSO per org, NetSuite sync
Billing: weighted_sum/custom aggregations, dynamic charge totals, Lago-style
per-org invoice sequences, usage thresholds + progressive invoices, Stripe
requires_action with confirm links, pay-in-advance charges, invoice-then-settle
wallet credits, payment receipts, draft deletion, quote->order flow, and the
previously never-emitted webhooks.

Integrations: per-org OAuth authorize/callback with state + PKCE, NetSuite
RESTlet (OAuth1 TBA) + Xero direct sync with item mappings, HubSpot/Salesforce
CRM sync, integration items cache, per-org SSO IdP credentials, console
integrations page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 01:25:23 +03:00

26 lines
1.3 KiB
SQL

-- Integration OAuth connect + per-organization SSO.
--
-- `sso_states` becomes the single-use state store for every server-driven
-- OAuth redirect: SSO logins (`purpose = 'sso_login'`) and integration
-- connections (`purpose = 'integration_oauth'`). The state is bound to the
-- organization (and integration) it was minted for, so a callback can never be
-- replayed into another tenant.
ALTER TABLE sso_states ADD COLUMN IF NOT EXISTS purpose varchar NOT NULL DEFAULT 'sso_login';
ALTER TABLE sso_states ADD COLUMN IF NOT EXISTS organization_id uuid;
ALTER TABLE sso_states ADD COLUMN IF NOT EXISTS integration_id uuid;
-- The fetched provider catalog (items, accounts, subsidiaries, tax items) is
-- upserted per integration + type + external id.
CREATE UNIQUE INDEX IF NOT EXISTS idx_integration_items_unique
ON integration_items (integration_id, item_type, external_id);
-- Auth (SSO) integrations are looked up by the email domain they serve.
CREATE INDEX IF NOT EXISTS idx_integration_connections_auth_domain
ON integration_connections (lower(settings->>'domain'))
WHERE integration_type = 'auth' AND deleted_at IS NULL;
-- Resource-mapping lookups by integration (sync status on invoices, credit
-- notes and customers).
CREATE INDEX IF NOT EXISTS idx_integration_customers_integration
ON integration_customers (integration_id, customer_id);