-- ============================================================ -- whatsapp_config: track Meta Cloud API registration state -- -- Why this exists: -- Saving a row to whatsapp_config does NOT make a phone number -- actually receive webhook events from Meta. Two extra Cloud API -- calls are required: -- -- POST /{phone_number_id}/register — subscribes the number -- with a 2FA PIN, makes -- it routable to OUR app -- POST /{waba_id}/subscribed_apps — subscribes the WABA -- (one-time per app, but -- idempotent so we can -- call on every save) -- -- Until those two complete successfully, Meta routes inbound -- events to whichever app last registered the number (often the -- one that did Embedded Signup originally). Symptom: a second -- wacrm user adds a second number under the same WABA, the UI -- reports "Connected" because metadata verification succeeds, -- but Meta's activity log shows zero events for that number. -- -- These columns let the UI distinguish "credentials saved" from -- "actually live" and let users retry registration without -- re-entering everything. -- -- Backfill: every column is nullable. Existing rows survive with -- NULL values; the UI shows them as "registration status unknown — -- click Verify Registration" and the diagnostic endpoint fills the -- timestamps on the next probe. -- -- Idempotent — safe to re-run. -- ============================================================ ALTER TABLE whatsapp_config ADD COLUMN IF NOT EXISTS registered_at TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS subscribed_apps_at TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS last_registration_error TEXT; -- Index supports the "find all numbers awaiting registration" -- query a future admin dashboard might want; cheap to maintain. CREATE INDEX IF NOT EXISTS idx_whatsapp_config_registered_at ON whatsapp_config (registered_at) WHERE registered_at IS NULL;