fix: interval upgrade / downgrade

This commit is contained in:
John Yeo
2026-04-03 23:46:27 +01:00
parent 9788c38381
commit 4f841fe229
2 changed files with 33 additions and 2 deletions

View File

@@ -192,7 +192,23 @@ describe("isProductUpgrade", () => {
expect(isProductUpgrade({ prices1, prices2 })).toBe(true);
});
test("$20/mo to $200/yr = downgrade ($20/mo > $16.67/mo)", () => {
test("$20/mo to $500/yr = upgrade (larger interval always upgrade, even if expensive)", () => {
const prices1 = [
createFixedPrice({
amount: 20,
interval: BillingInterval.Month,
}),
];
const prices2 = [
createFixedPrice({
amount: 500,
interval: BillingInterval.Year,
}),
];
expect(isProductUpgrade({ prices1, prices2 })).toBe(true);
});
test("$20/mo to $200/yr = upgrade (larger interval always upgrade)", () => {
const prices1 = [
createFixedPrice({
amount: 20,
@@ -205,7 +221,7 @@ describe("isProductUpgrade", () => {
interval: BillingInterval.Year,
}),
];
expect(isProductUpgrade({ prices1, prices2 })).toBe(false);
expect(isProductUpgrade({ prices1, prices2 })).toBe(true);
});
test("$5/week to $20/mo = upgrade ($20/mo <= $20/mo)", () => {

View File

@@ -2,6 +2,10 @@ import { Decimal } from "decimal.js";
import type { BillingInterval } from "../../models/productModels/intervals/billingInterval";
import type { Price } from "../../models/productModels/priceModels/priceModels";
import {
compareBillingIntervals,
getLargestInterval,
} from "../intervalUtils/priceIntervalUtils";
import { intervalToValue } from "../intervalUtils";
import { nullish } from "../utils";
import {
@@ -70,6 +74,17 @@ export const isProductUpgrade = ({
return true;
}
const billingInterval1 = getLargestInterval({ prices: prices1 });
const billingInterval2 = getLargestInterval({ prices: prices2 });
if (billingInterval1 && billingInterval2) {
const cmp = compareBillingIntervals({
configA: billingInterval1,
configB: billingInterval2,
});
if (cmp > 0) return true;
}
const total1 = getNormalizedTotal({ prices: prices1 });
const total2 = getNormalizedTotal({ prices: prices2 });