From 304a0ec258ce1ae19bac5bd2ca51c24605fbacb8 Mon Sep 17 00:00:00 2001 From: John Yeo Date: Fri, 21 Nov 2025 19:08:46 +0000 Subject: [PATCH] fix: included usage for cusProducts with quantity > 1 --- .../getApiBalance/getApiBalance.ts | 6 +++ server/tests/_temp/temp1.test.ts | 46 ++++++++++--------- .../utils/cusEntUtils/convertCusEntUtils.ts | 5 +- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/server/src/internal/customers/cusUtils/apiCusUtils/getApiBalance/getApiBalance.ts b/server/src/internal/customers/cusUtils/apiCusUtils/getApiBalance/getApiBalance.ts index e525e3fd5..f74f221a0 100644 --- a/server/src/internal/customers/cusUtils/apiCusUtils/getApiBalance/getApiBalance.ts +++ b/server/src/internal/customers/cusUtils/apiCusUtils/getApiBalance/getApiBalance.ts @@ -190,6 +190,12 @@ export const getApiBalance = ({ ), ); + // console.log( + // "totalGrantedBalanceWithRollovers", + // totalGrantedBalanceWithRollovers, + // ); + // console.log("--------------------------------"); + const totalAdjustment = sumValues( cusEnts.map((cusEnt) => { const { adjustment } = getCusEntBalance({ diff --git a/server/tests/_temp/temp1.test.ts b/server/tests/_temp/temp1.test.ts index 1419305f1..6d6dc8f04 100644 --- a/server/tests/_temp/temp1.test.ts +++ b/server/tests/_temp/temp1.test.ts @@ -5,7 +5,11 @@ import ctx from "@tests/utils/testInitUtils/createTestContext.js"; import chalk from "chalk"; import { AutumnInt } from "@/external/autumn/autumnCli.js"; import { constructFeatureItem } from "@/utils/scriptUtils/constructItem.js"; -import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js"; +import { + constructProduct, + constructRawProduct, +} from "@/utils/scriptUtils/createTestProducts.js"; +import { constructPriceItem } from "../../src/internal/products/product-items/productItemUtils.js"; import { initCustomerV3 } from "../../src/utils/scriptUtils/testUtils/initCustomerV3.js"; import { initProductsV0 } from "../../src/utils/scriptUtils/testUtils/initProductsV0.js"; @@ -15,18 +19,24 @@ const pro = constructProduct({ isDefault: true, items: [ - // constructFeatureItem({ - // featureId: TestFeature.Action1, - // includedUsage: 5, - // }), constructFeatureItem({ featureId: TestFeature.Credits, - // includedUsage: 200, - unlimited: true, + includedUsage: 200, + // unlimited: true, + }), + ], +}); + +const oneOff = constructRawProduct({ + id: "one-off", + items: [ + constructPriceItem({ + price: 10, + interval: null, }), constructFeatureItem({ - featureId: TestFeature.Words, - unlimited: true, + featureId: TestFeature.Messages, + includedUsage: 100, }), ], }); @@ -46,7 +56,7 @@ describe(`${chalk.yellowBright("temp: Testing add ons")}`, () => { await initProductsV0({ ctx, - products: [pro], + products: [pro, oneOff], prefix: customerId, }); @@ -57,17 +67,9 @@ describe(`${chalk.yellowBright("temp: Testing add ons")}`, () => { }); test("should attach pro product", async () => { - const res = await Promise.all( - Array.from({ length: 7 }, () => - autumn.check({ - customer_id: customerId, - feature_id: TestFeature.Workflows, - send_event: true, - }), - ), - ); - - const allowedCount = res.filter((r) => r.allowed).length; - console.log("Allowed count", allowedCount); + await autumn.attach({ + customer_id: customerId, + product_id: oneOff.id, + }); }); }); diff --git a/shared/utils/cusEntUtils/convertCusEntUtils.ts b/shared/utils/cusEntUtils/convertCusEntUtils.ts index b856c1794..9c31aea76 100644 --- a/shared/utils/cusEntUtils/convertCusEntUtils.ts +++ b/shared/utils/cusEntUtils/convertCusEntUtils.ts @@ -121,7 +121,10 @@ export const cusEntToGrantedBalance = ({ const grantedBalance = cusEnt.entitlement.allowance || 0; - const total = new Decimal(grantedBalance).mul(entityCount).toNumber(); + const total = new Decimal(grantedBalance) + .mul(cusEnt.customer_product.quantity ?? 1) + .mul(entityCount) + .toNumber(); if (withRollovers && rollover) { return new Decimal(total)