Skip to content

Commit db44072

Browse files
committed
Refactor: Build tier table
Each element of the array indicates the minimum amount of licenses that have to be purchased to get the corresponding unit price. When filtering this array, the first element of the filtered array is the tier to be applied.
1 parent 6ebe93b commit db44072

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
const priceTiers = [
2+
{ minAmount: 1, unitPrice: 299 },
3+
{ minAmount: 3, unitPrice: 239 },
4+
{ minAmount: 11, unitPrice: 219 },
5+
{ minAmount: 26, unitPrice: 199 },
6+
{ minAmount: 51, unitPrice: 149 },
7+
];
8+
19
export function tieredPricing(licenseAmount: number): number {
210
const licensePrice = calculateLicensePrice(licenseAmount);
311

412
return licensePrice * licenseAmount;
513
}
614

715
function calculateLicensePrice(licenseAmount: number): number {
8-
let licensePrice = 299;
9-
10-
if (licenseAmount >= 3) licensePrice = 239;
11-
if (licenseAmount >= 11) licensePrice = 219;
12-
if (licenseAmount >= 26) licensePrice = 199;
13-
if (licenseAmount >= 51) licensePrice = 149;
14-
15-
return licensePrice;
16+
const applicableTier = priceTiers
17+
.filter((tier) => licenseAmount >= tier.minAmount)
18+
.pop();
19+
return applicableTier?.unitPrice || 299;
1620
}

0 commit comments

Comments
 (0)