fix(message-templates): WABA-wide template status lookup + channel_id error key (EVO-1717)#142
Conversation
… error key (EVO-1717) Follow-up to the EVO-1232 review (PR #132). Addresses the verified findings: - [Medium] message_template_status_update no longer scopes the template lookup to the single `.first` channel of the WABA. A WABA can host multiple whatsapp_cloud channels and Meta's template id is per-WABA, so a template on a sibling channel silently dropped its APPROVED/REJECTED status. New find_template_for_waba resolves the template across all channels of the WABA. - [Low] WhatsApp Cloud channel validation errors move from :channel to :channel_id so the JSON/error key binds to the channel_id form field (6.3 AC). Specs: deterministic multi-channel-WABA regression (direct helper assertion + end-to-end), an update_columns-skips-validation case, and a reworked sync-job writeback spec that mirrors prod's find_or_initialize_by persistence instead of mutating the in-memory instance. The flat templates endpoint pre-req was already delivered by EVO-1716; no change needed there. Touches whatsapp_events_job.rb — coordinate merge order with 7.4a.
There was a problem hiding this comment.
Sorry @daniloleonecarneiro, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
dpaes
left a comment
There was a problem hiding this comment.
Reviewed — approved ✅
Follow-up to the EVO-1232 [6.3] review; both documented fixes verified against the real code at the head ref. CRM-community runs no RSpec in CI, so I traced the specs by hand rather than trusting the self-reported green.
- Medium — WABA multi-channel status webhook no-op: fixed.
find_template_for_waba(whatsapp_events_job.rb:138-159) resolves the template across everywhatsapp_cloudchannel sharing thewaba_id(provider_config ->> 'waba_id' OR business_account_id, thenMessageTemplate.where(channel_type: 'Channel::Whatsapp', channel_id: channel_ids).find_by("metadata ->> 'external_id' = ?")), andhandle_message_template_status_updateno longer scopes to the.firstchannel. The silentreturnfor a template living on a sibling channel is closed. The new "multi-channel WABA" spec group is a genuine regression guard (sibling-channel resolution, cross-WABA negative, blank-WABA guard, end-to-end perform). - Low — error key: fixed.
message_template.rb:236-240now uses:channel_idfor both validations; repo-wide grep shows no remainingerrors.add(:channel,/errors[:channel]reader, so the rename is complete and breaks nothing server-side.
Non-blocking (no change required here):
metadata ->> 'external_id'has no GIN/expression index, but thechannel_id IN (...)filter is indexed so the JSON scan is confined to the WABA's templates — same property as the pre-existing single-channel query, fine at community scale.- The
:channel→:channel_iderror-key rename is a client-facing contract change; the matching frontend field binding is tracked under 6.4 — worth confirming 6.4 ships so the user-facing benefit lands.
Merge-order note: this touches whatsapp_events_job.rb; the 7.4a work is documented as disjoint-case but merge-adjacent. Checked the open PRs — the only other one near that area (#124) touches a different file, so there's no live conflict.
Summary
Non-blocking follow-up to the EVO-1232 [6.3] review (PR #132, already merged). Addresses the findings verified against the merged code:
handle_message_template_status_updateresolved the template via the single.firstchannel returned byfind_channel_by_waba_id. Meta's template id (metadata['external_id']) is per-WABA, but a WABA can host multiplewhatsapp_cloudchannels — so a template living on a sibling channel had itsAPPROVED/REJECTEDstatus silently dropped. New privatefind_template_for_waba(waba_id, external_id)resolves the template across every channel of the WABA. Theupdate_columnswrite andrescueenvelope are unchanged. (1-phone-per-WABA deploys — the common community case — were unaffected.):channelto:channel_id, so the JSON error key / frontend field binding matches the 6.3 AC. Server-side blast radius verified empty (only the model + its spec read the key;InboxesControlleruses an independent string)./api/v1/message_templates,routes.rb:162) — verified, no code change.Test plan
spec/jobs/webhooks/whatsapp_events_job_spec.rb— newmulti-channel WABAgroup: a deterministic regression guard exercisingfind_template_for_wabadirectly (independent of which channel is.first), a cross-WABA negative, a blank-WABA-id guard, an end-to-endperform, and anupdate_columns-skips-validation case on a model-invalid record.spec/models/message_template_spec.rb— the three error assertions flipped toerrors[:channel_id].spec/jobs/sync_message_template_with_whatsapp_cloud_job_spec.rb— the writeback example reworked to mirror prod'sfind_or_initialize_by(channel:, name:, language:)persistence (persist + reload) instead of mutating the in-memory instance, which gave false confidence.Notas
whatsapp_events_job.rb— coordinate with 7.4a (disjointcasebranches, merge-conflict risk only, no logic interaction).:channel→:channel_idrename changes the error key any client could read; frontend handling is tracked under 6.4.find_channel_by_waba_idstill returns.firstfor channel resolution (other handlers act on a single channel by design) — only the template lookup is broadened.