From 194c7022a6a36774f8e453749e8a3c50525ddd6b Mon Sep 17 00:00:00 2001 From: John Yeo Date: Thu, 11 Dec 2025 20:27:35 +0000 Subject: [PATCH] fix: adding entity to webhooks response --- .../external/autumn/autumnWebhookRouter.ts | 1 - .../handlers/handleProductsUpdated.ts | 39 ++++++++++++------- server/tests/_temp/temp.test.ts | 10 ++--- server/tests/_temp/temp1.test.ts | 30 +++++++------- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/server/src/external/autumn/autumnWebhookRouter.ts b/server/src/external/autumn/autumnWebhookRouter.ts index f234a640b..1aee9ea3c 100644 --- a/server/src/external/autumn/autumnWebhookRouter.ts +++ b/server/src/external/autumn/autumnWebhookRouter.ts @@ -54,7 +54,6 @@ autumnWebhookRouter.post( const { type, data } = evt; // console.log("Event:", evt); - // console.log("Data:", data); switch (type) { case WebhookEventType.CustomerProductsUpdated: diff --git a/server/src/internal/analytics/handlers/handleProductsUpdated.ts b/server/src/internal/analytics/handlers/handleProductsUpdated.ts index 918f317e6..63aacb624 100644 --- a/server/src/internal/analytics/handlers/handleProductsUpdated.ts +++ b/server/src/internal/analytics/handlers/handleProductsUpdated.ts @@ -1,8 +1,10 @@ import { AffectedResource, type ApiCustomer, + type ApiEntityV1, type ApiPlan, ApiVersion, + ApiVersionClass, type AppEnv, type AuthType, addToExpand, @@ -10,6 +12,7 @@ import { CusExpand, type CustomerLegacyData, cusProductToProduct, + type EntityLegacyData, type FullCusProduct, type FullProduct, type Organization, @@ -23,6 +26,7 @@ import { FeatureService } from "@/internal/features/FeatureService.js"; import { JobName } from "@/queue/JobName.js"; import { addTaskToQueue } from "@/queue/queueUtils.js"; import { getApiCustomerBase } from "../../customers/cusUtils/apiCusUtils/getApiCustomerBase"; +import { getApiEntityBase } from "../../entities/entityUtils/apiEntityUtils/getApiEntityBase"; import { getPlanResponse } from "../../products/productUtils/productResponseUtils/getPlanResponse"; interface ActionDetails { @@ -124,8 +128,10 @@ export const handleProductsUpdated = async ({ env, }); + ctx.apiVersion = new ApiVersionClass(ApiVersion.V1_2); + if (ctx.apiVersion.lte(ApiVersion.V1_2)) { - addToExpand({ + ctx = addToExpand({ ctx, add: [ CusExpand.BalancesFeature, @@ -166,21 +172,27 @@ export const handleProductsUpdated = async ({ ctx, }); - // console.log(`API version: ${ctx.apiVersion.value}`); - // console.log(`Versioned customer:`, versionedCustomer); - // console.log(`Versioned plan:`, versionedPlan); + let entity: unknown | undefined; + if (fullCus.entity) { + const { apiEntity, legacyData } = await getApiEntityBase({ + ctx, + entity: fullCus.entity, + fullCus, + }); - // let entityRes = null; - // if (notNullish(customer?.entity)) { - // entityRes = await getSingleEntityResponse({ - // ctx, - // entityId: customer.entity!.id, - // fullCus: customer, - // entity: customer.entity!, - // }); - // } + entity = applyResponseVersionChanges({ + input: apiEntity, + targetVersion: ctx.apiVersion, + resource: AffectedResource.Entity, + legacyData, + ctx, + }); + } // 2. Send Svix event + ctx.logger.info( + `sending customer.products.updated webhook, customer ID: ${data.customerId}, entity ID: ${fullCus.entity?.id || "none"}`, + ); await sendSvixEvent({ org, env, @@ -188,6 +200,7 @@ export const handleProductsUpdated = async ({ data: { scenario, customer: versionedCustomer, + entity, updated_product: versionedPlan, }, }); diff --git a/server/tests/_temp/temp.test.ts b/server/tests/_temp/temp.test.ts index bc087c8af..728b70de8 100644 --- a/server/tests/_temp/temp.test.ts +++ b/server/tests/_temp/temp.test.ts @@ -1,5 +1,5 @@ -import { beforeAll, describe, test } from "bun:test"; -import { ApiVersion, BillingInterval, FreeTrialDuration } from "@autumn/shared"; +import { beforeAll, describe } from "bun:test"; +import { ApiVersion, BillingInterval } from "@autumn/shared"; import { TestFeature } from "@tests/setup/v2Features.js"; import { advanceTestClock } from "@tests/utils/stripeUtils"; import ctx from "@tests/utils/testInitUtils/createTestContext.js"; @@ -7,11 +7,7 @@ import chalk from "chalk"; import { addWeeks } from "date-fns"; import { AutumnInt } from "@/external/autumn/autumnCli.js"; import { constructPriceItem } from "@/internal/products/product-items/productItemUtils"; -import { - constructArrearItem, - constructFeatureItem, - constructPrepaidItem, -} from "@/utils/scriptUtils/constructItem.js"; +import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js"; import { constructProduct, constructRawProduct, diff --git a/server/tests/_temp/temp1.test.ts b/server/tests/_temp/temp1.test.ts index 5aff33c9b..bca02eaab 100644 --- a/server/tests/_temp/temp1.test.ts +++ b/server/tests/_temp/temp1.test.ts @@ -68,22 +68,22 @@ describe(`${chalk.yellowBright("temp1: Testing pro product")}`, () => { await autumn.entities.create(customerId, [entity]); - await autumn.attach({ - customer_id: customerId, - product_id: pro.id, - entity_id: entity.id, - }); + // await autumn.attach({ + // customer_id: customerId, + // product_id: pro.id, + // entity_id: entity.id, + // }); - await autumn.attach({ - customer_id: customerId, - product_id: free.id, - entity_id: entity.id, - }); + // await autumn.attach({ + // customer_id: customerId, + // product_id: free.id, + // entity_id: entity.id, + // }); - await autumn.attach({ - customer_id: customerId, - product_id: premium.id, - entity_id: entity.id, - }); + // await autumn.attach({ + // customer_id: customerId, + // product_id: premium.id, + // entity_id: entity.id, + // }); }); });