CognxSafeTrack Claude Sonnet 4.6 commited on
Commit
bde43ec
·
1 Parent(s): 7e2166c

fix(meta-status): add on_behalf_of_business_info fallback for business verification

Browse files

When the `business` edge on WABA returns nothing (system user token scope or
test WABA restrictions), fall back to `on_behalf_of_business_info` + direct
business node query for verification_status. Covers verified businesses whose
WABA doesn't expose the business edge directly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

apps/api/src/services/organization.ts CHANGED
@@ -133,10 +133,12 @@ export async function fetchMetaStatus(organizationId: string, forceRefresh = fal
133
  logger.error({ err, organizationId }, '[META-STATUS] WABA fetch failed');
134
  }
135
 
136
- // Call 2 — Business verification (optional, may not be available on test WABAs)
137
  let businessId: string | undefined;
138
  let businessName: string | undefined;
139
  let businessVerified: boolean | undefined;
 
 
140
  try {
141
  const bizRes = await fetch(
142
  `https://graph.facebook.com/v19.0/${org.wabaId}?fields=business{id,name,verification_status}`,
@@ -148,8 +150,31 @@ export async function fetchMetaStatus(organizationId: string, forceRefresh = fal
148
  businessName = bizData.business.name;
149
  businessVerified = bizData.business.verification_status === 'verified';
150
  }
151
- } catch {
152
- // Business field not accessible — non-fatal (e.g. test WABAs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  }
154
 
155
  const result: MetaStatus = {
 
133
  logger.error({ err, organizationId }, '[META-STATUS] WABA fetch failed');
134
  }
135
 
136
+ // Call 2 — Business verification (optional, two attempts with different field paths)
137
  let businessId: string | undefined;
138
  let businessName: string | undefined;
139
  let businessVerified: boolean | undefined;
140
+
141
+ // Attempt A: business edge (works on production WABAs with business_management scope)
142
  try {
143
  const bizRes = await fetch(
144
  `https://graph.facebook.com/v19.0/${org.wabaId}?fields=business{id,name,verification_status}`,
 
150
  businessName = bizData.business.name;
151
  businessVerified = bizData.business.verification_status === 'verified';
152
  }
153
+ } catch { /* non-fatal */ }
154
+
155
+ // Attempt B: on_behalf_of_business_info fallback (broader token compatibility)
156
+ if (businessId === undefined) {
157
+ try {
158
+ const obRes = await fetch(
159
+ `https://graph.facebook.com/v19.0/${org.wabaId}?fields=on_behalf_of_business_info`,
160
+ { headers }
161
+ );
162
+ const obData = await obRes.json() as any;
163
+ if (!obData.error && obData.on_behalf_of_business_info?.id) {
164
+ businessId = obData.on_behalf_of_business_info.id;
165
+ businessName = obData.on_behalf_of_business_info.name;
166
+ // Fetch verification status directly from the business node
167
+ const vRes = await fetch(
168
+ `https://graph.facebook.com/v19.0/${businessId}?fields=name,verification_status`,
169
+ { headers }
170
+ );
171
+ const vData = await vRes.json() as any;
172
+ if (!vData.error) {
173
+ businessVerified = vData.verification_status === 'verified';
174
+ if (vData.name) businessName = vData.name;
175
+ }
176
+ }
177
+ } catch { /* non-fatal */ }
178
  }
179
 
180
  const result: MetaStatus = {