()
| 172 | * Returns the fetched response or null on error |
| 173 | */ |
| 174 | export async function fetchAndStorePassesEligibility(): Promise<ReferralEligibilityResponse | null> { |
| 175 | // Return existing promise if fetch is already in progress |
| 176 | if (fetchInProgress) { |
| 177 | logForDebugging('Passes: Reusing in-flight eligibility fetch') |
| 178 | return fetchInProgress |
| 179 | } |
| 180 | |
| 181 | const orgId = getOauthAccountInfo()?.organizationUuid |
| 182 | |
| 183 | if (!orgId) { |
| 184 | return null |
| 185 | } |
| 186 | |
| 187 | // Store the promise to share with concurrent calls |
| 188 | fetchInProgress = (async () => { |
| 189 | try { |
| 190 | const response = await fetchReferralEligibility() |
| 191 | |
| 192 | const cacheEntry = { |
| 193 | ...response, |
| 194 | timestamp: Date.now(), |
| 195 | } |
| 196 | |
| 197 | saveGlobalConfig(current => ({ |
| 198 | ...current, |
| 199 | passesEligibilityCache: { |
| 200 | ...current.passesEligibilityCache, |
| 201 | [orgId]: cacheEntry, |
| 202 | }, |
| 203 | })) |
| 204 | |
| 205 | logForDebugging( |
| 206 | `Passes eligibility cached for org ${orgId}: ${response.eligible}`, |
| 207 | ) |
| 208 | |
| 209 | return response |
| 210 | } catch (error) { |
| 211 | logForDebugging('Failed to fetch and cache passes eligibility') |
| 212 | logError(error as Error) |
| 213 | return null |
| 214 | } finally { |
| 215 | // Clear the promise when done |
| 216 | fetchInProgress = null |
| 217 | } |
| 218 | })() |
| 219 | |
| 220 | return fetchInProgress |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get cached passes eligibility data or fetch if needed |
no test coverage detected