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;
// console.log("Event:", evt);
// console.log("Data:", data);
switch (type) {
case WebhookEventType.CustomerProductsUpdated:

View File

@@ -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<ApiEntityV1, EntityLegacyData>({
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,
},
});

View File

@@ -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,

View File

@@ -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,
// });
});
});