fix: adding entity to webhooks response

This commit is contained in:
John Yeo
2025-12-11 20:27:35 +00:00
parent d79c063fa5
commit 194c7022a6
4 changed files with 44 additions and 36 deletions

View File

@@ -54,7 +54,6 @@ autumnWebhookRouter.post(
const { type, data } = evt; const { type, data } = evt;
// console.log("Event:", evt); // console.log("Event:", evt);
// console.log("Data:", data);
switch (type) { switch (type) {
case WebhookEventType.CustomerProductsUpdated: case WebhookEventType.CustomerProductsUpdated:

View File

@@ -1,8 +1,10 @@
import { import {
AffectedResource, AffectedResource,
type ApiCustomer, type ApiCustomer,
type ApiEntityV1,
type ApiPlan, type ApiPlan,
ApiVersion, ApiVersion,
ApiVersionClass,
type AppEnv, type AppEnv,
type AuthType, type AuthType,
addToExpand, addToExpand,
@@ -10,6 +12,7 @@ import {
CusExpand, CusExpand,
type CustomerLegacyData, type CustomerLegacyData,
cusProductToProduct, cusProductToProduct,
type EntityLegacyData,
type FullCusProduct, type FullCusProduct,
type FullProduct, type FullProduct,
type Organization, type Organization,
@@ -23,6 +26,7 @@ import { FeatureService } from "@/internal/features/FeatureService.js";
import { JobName } from "@/queue/JobName.js"; import { JobName } from "@/queue/JobName.js";
import { addTaskToQueue } from "@/queue/queueUtils.js"; import { addTaskToQueue } from "@/queue/queueUtils.js";
import { getApiCustomerBase } from "../../customers/cusUtils/apiCusUtils/getApiCustomerBase"; import { getApiCustomerBase } from "../../customers/cusUtils/apiCusUtils/getApiCustomerBase";
import { getApiEntityBase } from "../../entities/entityUtils/apiEntityUtils/getApiEntityBase";
import { getPlanResponse } from "../../products/productUtils/productResponseUtils/getPlanResponse"; import { getPlanResponse } from "../../products/productUtils/productResponseUtils/getPlanResponse";
interface ActionDetails { interface ActionDetails {
@@ -124,8 +128,10 @@ export const handleProductsUpdated = async ({
env, env,
}); });
ctx.apiVersion = new ApiVersionClass(ApiVersion.V1_2);
if (ctx.apiVersion.lte(ApiVersion.V1_2)) { if (ctx.apiVersion.lte(ApiVersion.V1_2)) {
addToExpand({ ctx = addToExpand({
ctx, ctx,
add: [ add: [
CusExpand.BalancesFeature, CusExpand.BalancesFeature,
@@ -166,21 +172,27 @@ export const handleProductsUpdated = async ({
ctx, ctx,
}); });
// console.log(`API version: ${ctx.apiVersion.value}`); let entity: unknown | undefined;
// console.log(`Versioned customer:`, versionedCustomer); if (fullCus.entity) {
// console.log(`Versioned plan:`, versionedPlan); const { apiEntity, legacyData } = await getApiEntityBase({
ctx,
entity: fullCus.entity,
fullCus,
});
// let entityRes = null; entity = applyResponseVersionChanges<ApiEntityV1, EntityLegacyData>({
// if (notNullish(customer?.entity)) { input: apiEntity,
// entityRes = await getSingleEntityResponse({ targetVersion: ctx.apiVersion,
// ctx, resource: AffectedResource.Entity,
// entityId: customer.entity!.id, legacyData,
// fullCus: customer, ctx,
// entity: customer.entity!, });
// }); }
// }
// 2. Send Svix event // 2. Send Svix event
ctx.logger.info(
`sending customer.products.updated webhook, customer ID: ${data.customerId}, entity ID: ${fullCus.entity?.id || "none"}`,
);
await sendSvixEvent({ await sendSvixEvent({
org, org,
env, env,
@@ -188,6 +200,7 @@ export const handleProductsUpdated = async ({
data: { data: {
scenario, scenario,
customer: versionedCustomer, customer: versionedCustomer,
entity,
updated_product: versionedPlan, updated_product: versionedPlan,
}, },
}); });

View File

@@ -1,5 +1,5 @@
import { beforeAll, describe, test } from "bun:test"; import { beforeAll, describe } from "bun:test";
import { ApiVersion, BillingInterval, FreeTrialDuration } from "@autumn/shared"; import { ApiVersion, BillingInterval } from "@autumn/shared";
import { TestFeature } from "@tests/setup/v2Features.js"; import { TestFeature } from "@tests/setup/v2Features.js";
import { advanceTestClock } from "@tests/utils/stripeUtils"; import { advanceTestClock } from "@tests/utils/stripeUtils";
import ctx from "@tests/utils/testInitUtils/createTestContext.js"; import ctx from "@tests/utils/testInitUtils/createTestContext.js";
@@ -7,11 +7,7 @@ import chalk from "chalk";
import { addWeeks } from "date-fns"; import { addWeeks } from "date-fns";
import { AutumnInt } from "@/external/autumn/autumnCli.js"; import { AutumnInt } from "@/external/autumn/autumnCli.js";
import { constructPriceItem } from "@/internal/products/product-items/productItemUtils"; import { constructPriceItem } from "@/internal/products/product-items/productItemUtils";
import { import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js";
constructArrearItem,
constructFeatureItem,
constructPrepaidItem,
} from "@/utils/scriptUtils/constructItem.js";
import { import {
constructProduct, constructProduct,
constructRawProduct, constructRawProduct,

View File

@@ -68,22 +68,22 @@ describe(`${chalk.yellowBright("temp1: Testing pro product")}`, () => {
await autumn.entities.create(customerId, [entity]); await autumn.entities.create(customerId, [entity]);
await autumn.attach({ // await autumn.attach({
customer_id: customerId, // customer_id: customerId,
product_id: pro.id, // product_id: pro.id,
entity_id: entity.id, // entity_id: entity.id,
}); // });
await autumn.attach({ // await autumn.attach({
customer_id: customerId, // customer_id: customerId,
product_id: free.id, // product_id: free.id,
entity_id: entity.id, // entity_id: entity.id,
}); // });
await autumn.attach({ // await autumn.attach({
customer_id: customerId, // customer_id: customerId,
product_id: premium.id, // product_id: premium.id,
entity_id: entity.id, // entity_id: entity.id,
}); // });
}); });
}); });