iachat/app/controllers/api/v1/accounts
Muhsin Keloth 66cfef9298
feat: Add WhatsApp health monitoring and self-service registration completion (#12556)
Fixes
https://linear.app/chatwoot/issue/CW-5692/whatsapp-es-numbers-stuck-in-pending-due-to-premature-registration


###  Problem  
Multiple customers reported that their WhatsApp numbers remain stuck in
**Pending** in WhatsApp Manager even after successful onboarding.

- Our system triggers a **registration call**
(`/<PHONE_NUMBER_ID>/register`) as soon as the number is OTP verified.
- In many cases, Meta hasn’t finished **display name
review/provisioning**, so the call fails with:

  ```
  code: 100, error_subcode: 2388001
  error_user_title: "Cannot Create Certificate"
error_user_msg: "Your display name could not be processed. Please edit
your display name and try again."
  ```

- This leaves the number stuck in Pending, no messaging can start until
we manually retry registration.
- Some customers have reported being stuck in this state for **7+
days**.

###  Root cause  
- We only check `code_verification_status = VERIFIED` before attempting
registration.
- We **don’t wait** for display name provisioning (`name_status` /
`platform_type`) to be complete.
- As a result, registration fails prematurely and the number never
transitions out of Pending.

### Solution  

#### 1. Health Status Monitoring  
- Build a backend service to fetch **real-time health data** from Graph
API:
  - `code_verification_status`  
  - `name_status` / `display_name_status`  
  - `platform_type`  
  - `throughput.level`  
  - `messaging_limit_tier`  
  - `quality_rating`  
- Expose health data via API
(`/api/v1/accounts/:account_id/inboxes/:id/health`).
- Display this in the UI as an **Account Health tab** with clear badges
and direct links to WhatsApp Manager.

#### 2. Smarter Registration Logic  
- Update `WebhookSetupService` to include a **dual-condition check**:  
  - Register if:  
    1. Phone is **not verified**, OR  
2. Phone is **verified but provisioning incomplete** (`platform_type =
NOT_APPLICABLE`, `throughput.level = NOT_APPLICABLE`).
- Skip registration if number is already provisioned.  
- Retry registration automatically when stuck.  
- Provide a UI banner with complete registration button so customers can
retry without manual support.

### Screenshot
<img width="2292" height="1344" alt="CleanShot 2025-09-30 at 16 01
03@2x"
src="https://github.com/user-attachments/assets/1c417d2a-b11c-475e-b092-3c5671ee59a7"
/>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
2025-10-02 11:25:48 +05:30
..
actions feat: Reverse the contact merge (#8057) 2023-10-10 08:20:48 +05:30
assignment_policies feat: Add assignment policies controllers with jbuilder views (#12199) 2025-08-18 19:15:21 -07:00
channels fix: Manage Twilio SMS channel via inbox API (#11457) 2025-05-15 12:28:54 +05:30
contacts feat: Add support for search_conversations in copilot (#11520) 2025-05-20 19:22:17 -07:00
conversations feat: API Endpoints to update message status (#11387) 2025-04-29 15:33:11 -07:00
google refactor: use state-based authentication (#11690) 2025-06-18 17:39:06 +05:30
inboxes feat: Add assignment policies controllers with jbuilder views (#12199) 2025-08-18 19:15:21 -07:00
instagram refactor: use state-based authentication (#11690) 2025-06-18 17:39:06 +05:30
integrations feat: Whatsapp embedded signup (#11612) 2025-07-14 21:37:06 -07:00
microsoft refactor: use state-based authentication (#11690) 2025-06-18 17:39:06 +05:30
notion feat: Whatsapp embedded signup (#11612) 2025-07-14 21:37:06 -07:00
twitter fix: Twitter inbox creation error (#1783) 2021-02-16 19:35:10 +05:30
whatsapp chore: Enable flexible whatsapp onboarding (Manual + Embedded Signup) options (#12344) 2025-09-15 19:59:56 +05:30
agent_bots_controller.rb feat: Ability to reset api_access_token (#11565) 2025-05-29 14:42:13 +05:30
agents_controller.rb fix: Removing repetitive name parameter in AgentsController (#11259) 2025-04-09 08:53:13 +05:30
articles_controller.rb feat: Update swagger to openapi 3.0.4, update request payloads with examples (#11374) 2025-05-22 14:26:44 +07:00
assignable_agents_controller.rb chore: Add Assignable Agents API (#4722) 2022-05-23 19:24:07 +05:30
assignment_policies_controller.rb feat: Add assignment policies controllers with jbuilder views (#12199) 2025-08-18 19:15:21 -07:00
automation_rules_controller.rb feat: common attachment endpoint follow-up changes (#7826) 2023-09-01 15:18:48 +07:00
base_controller.rb Feat: authenticate direct upload (#4160) 2022-03-16 13:54:18 +05:30
bulk_actions_controller.rb feat: Add support for bulk snooze until (#9360) 2024-05-08 08:55:31 +05:30
callbacks_controller.rb fix: Fetch all facebook pages during inbox creation (#11956) 2025-07-17 18:32:12 +05:30
campaigns_controller.rb feat: WhatsApp campaigns (#11910) 2025-07-16 09:04:02 +05:30
canned_responses_controller.rb fix: order for canned response (#6400) 2023-02-06 12:53:48 -08:00
categories_controller.rb feat: Adds the ability to set an emoji for help center category (#8111) 2023-10-20 13:52:30 +05:30
contact_inboxes_controller.rb chore: Add an API to find the contacts using contact inbox sourceId (#8012) 2023-09-28 15:26:28 -07:00
contacts_controller.rb perf: Contact optimisation fixes (#12016) 2025-09-25 18:59:55 +05:30
conversations_controller.rb feat: Add conversation delete feature (#11677) 2025-06-05 15:53:17 -05:00
csat_survey_responses_controller.rb feat: more CSAT filters (#7038) 2023-05-18 22:50:46 +05:30
custom_attribute_definitions_controller.rb feat: Support Regex validation for custom attributes (#7856) 2024-01-23 18:01:57 +04:00
custom_filters_controller.rb fix: Allow resource access without filter type in custom_filters API (#11445) 2025-05-08 20:12:05 -07:00
dashboard_apps_controller.rb feat: Allow users to create dashboard apps to give agents more context (#4761) 2022-06-01 11:13:10 +05:30
inbox_members_controller.rb feat: invalidate cache after inbox members or team members update (#10869) 2025-02-20 21:28:38 -08:00
inboxes_controller.rb feat: Add WhatsApp health monitoring and self-service registration completion (#12556) 2025-10-02 11:25:48 +05:30
labels_controller.rb chore: Use "destroy!" instead of "destroy" when not checking the return value (#4259) 2022-03-24 13:28:25 +05:30
macros_controller.rb feat: common attachment endpoint follow-up changes (#7826) 2023-09-01 15:18:48 +07:00
notification_settings_controller.rb Chore: Code Cleanup in API controllers (#932) 2020-06-07 13:58:05 +05:30
notifications_controller.rb feat: Update last_snoozed_at after the un-snooze notification (#8943) 2024-02-20 18:55:39 +05:30
oauth_authorization_controller.rb refactor: use state-based authentication (#11690) 2025-06-18 17:39:06 +05:30
portals_controller.rb feat: Allow detaching help center widget (#12459) 2025-09-17 22:27:50 +05:30
search_controller.rb feat: allow searching articles in omnisearch (#11558) 2025-05-28 13:50:50 +05:30
team_members_controller.rb feat: invalidate cache after inbox members or team members update (#10869) 2025-02-20 21:28:38 -08:00
teams_controller.rb chore: Use "destroy!" instead of "destroy" when not checking the return value (#4259) 2022-03-24 13:28:25 +05:30
upload_controller.rb feat: Extend upload API end point to support external images (#10062) 2024-09-05 10:42:54 +05:30
webhooks_controller.rb feat: Add event subscription option to webhooks (#4540) 2022-04-25 17:44:42 +05:30
working_hours_controller.rb feat: Business hours 2020-11-01 00:14:33 +05:30