fix: included usage for cusProducts with quantity > 1

This commit is contained in:
John Yeo
2025-11-21 19:08:46 +00:00
parent d5c1d69f76
commit 304a0ec258
3 changed files with 34 additions and 23 deletions

View File

@@ -190,6 +190,12 @@ export const getApiBalance = ({
),
);
// console.log(
// "totalGrantedBalanceWithRollovers",
// totalGrantedBalanceWithRollovers,
// );
// console.log("--------------------------------");
const totalAdjustment = sumValues(
cusEnts.map((cusEnt) => {
const { adjustment } = getCusEntBalance({

View File

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

View File

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