diff --git a/server/src/internal/api/check/handleCheck.ts b/server/src/internal/api/check/handleCheck.ts index 36bbcfa43..800f64cc2 100644 --- a/server/src/internal/api/check/handleCheck.ts +++ b/server/src/internal/api/check/handleCheck.ts @@ -77,7 +77,24 @@ export const handleCheck = createRoute({ flag: null, }); - return c.json(fallbackResponse); + const featureToUse = ctx.features.find( + (feature) => feature.id === body.feature_id, + ); + + const transformedFallbackResponse = featureToUse + ? applyResponseVersionChanges({ + input: fallbackResponse, + targetVersion: ctx.apiVersion, + resource: AffectedResource.Check, + legacyData: { + noCusEnts: false, + featureToUse, + }, + ctx, + }) + : fallbackResponse; + + return c.json(transformedFallbackResponse); } let response: CheckResponseV3; diff --git a/server/tests/integration/balances/check/check-fallback.test.ts b/server/tests/integration/balances/check/check-fallback.test.ts index 6b73c5796..913d10790 100644 --- a/server/tests/integration/balances/check/check-fallback.test.ts +++ b/server/tests/integration/balances/check/check-fallback.test.ts @@ -64,3 +64,58 @@ test(`${chalk.yellowBright("check-fallback: /check returns allowed=true on retry CusService.getFull = originalGetFull; } }, 20000); + +test(`${chalk.yellowBright("check-fallback-legacy: /check fallback applies response version transforms")}`, async () => { + const messagesItem = items.monthlyMessages({ includedUsage: 1000 }); + const freeProd = products.base({ + id: "check-fallback-legacy-free", + items: [messagesItem], + }); + + const { customerId } = await initScenario({ + customerId: "check-fallback-legacy", + setup: [s.customer({ testClock: false }), s.products({ list: [freeProd] })], + actions: [s.attach({ productId: freeProd.id })], + }); + + const originalGetFull = CusService.getFull; + const app = createHonoApp(); + + try { + CusService.getFull = (async () => { + const error = new Error("simulated db outage") as Error & { + code: string; + }; + error.code = "CONNECT_TIMEOUT"; + throw error; + }) as typeof CusService.getFull; + + const response = await app.fetch( + new Request("http://localhost/v1/balances.check", { + method: "POST", + headers: { + Authorization: `Bearer ${process.env.UNIT_TEST_AUTUMN_SECRET_KEY || ""}`, + "Content-Type": "application/json", + "x-api-version": ApiVersion.V1_Beta.toString(), + "x-skip-cache": "true", + }, + body: JSON.stringify({ + customer_id: customerId, + feature_id: TestFeature.Messages, + }), + }), + ); + + expect(response.status).toBe(200); + expect(await response.json()).toEqual({ + allowed: false, + code: "feature_found", + customer_id: customerId, + feature_id: TestFeature.Messages, + entity_id: undefined, + required_balance: 1, + }); + } finally { + CusService.getFull = originalGetFull; + } +}, 20000);