Skip to content

Commit 99bed1d

Browse files
committed
feat(tiered_pricing): 🔵 add Tier concept
1 parent dff1af3 commit 99bed1d

File tree

2 files changed

+44
-55
lines changed

2 files changed

+44
-55
lines changed
Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,26 @@
11
import NumberOfSubscriptionsNotAllowed from "./NumberOfSubscriptionsNotAllowed";
2+
import Tier from "./Tier";
23

34
export default class GraduatedTieredPricing {
45
priceFor(subscriptions: number): number {
56
if (subscriptions < 1) {
67
throw new NumberOfSubscriptionsNotAllowed();
78
}
89

9-
const step1Price = 299;
10-
const step2Price = 239;
11-
const step3Price = 219;
12-
const step4Price = 199;
13-
const step5Price = 149;
10+
const tiers = [
11+
new Tier(1, 2, 299),
12+
new Tier(3, 10, 239),
13+
new Tier(11, 25, 219),
14+
new Tier(26, 50, 199),
15+
new Tier(51, Number.MAX_SAFE_INTEGER, 149),
16+
];
1417

15-
const step1Boundary = 2;
16-
const step2Boundary = 10;
17-
const step3Boundary = 25;
18-
const step4Boundary = 50;
19-
20-
const numberOfSubscriptionsInStep1 = 2;
21-
const numberOfSubscriptionsInStep2 = step2Boundary - step1Boundary; // 8
22-
const numberOfSubscriptionsInStep3 = step3Boundary - step2Boundary; // 15
23-
const numberOfSubscriptionsInStep4 = step4Boundary - step3Boundary; // 25
24-
25-
if (subscriptions <= step1Boundary) {
26-
return subscriptions * step1Price;
27-
}
28-
if (subscriptions <= step2Boundary) {
29-
return (
30-
numberOfSubscriptionsInStep1 * step1Price +
31-
(subscriptions - numberOfSubscriptionsInStep1) * step2Price
32-
);
33-
}
34-
if (subscriptions <= step3Boundary) {
35-
return (
36-
numberOfSubscriptionsInStep1 * step1Price +
37-
numberOfSubscriptionsInStep2 * step2Price +
38-
(subscriptions -
39-
(numberOfSubscriptionsInStep1 + numberOfSubscriptionsInStep2)) *
40-
step3Price
41-
);
42-
}
43-
if (subscriptions <= step4Boundary) {
44-
return (
45-
numberOfSubscriptionsInStep1 * step1Price +
46-
numberOfSubscriptionsInStep2 * step2Price +
47-
numberOfSubscriptionsInStep3 * step3Price +
48-
(subscriptions -
49-
(numberOfSubscriptionsInStep1 +
50-
numberOfSubscriptionsInStep2 +
51-
numberOfSubscriptionsInStep3)) *
52-
step4Price
53-
);
54-
}
5518
return (
56-
numberOfSubscriptionsInStep1 * step1Price +
57-
numberOfSubscriptionsInStep2 * step2Price +
58-
numberOfSubscriptionsInStep3 * step3Price +
59-
numberOfSubscriptionsInStep4 * step4Price +
60-
(subscriptions -
61-
(numberOfSubscriptionsInStep1 +
62-
numberOfSubscriptionsInStep2 +
63-
numberOfSubscriptionsInStep3 +
64-
numberOfSubscriptionsInStep4)) *
65-
step5Price
19+
tiers[0].totalFor(subscriptions) +
20+
tiers[1].totalFor(subscriptions) +
21+
tiers[2].totalFor(subscriptions) +
22+
tiers[3].totalFor(subscriptions) +
23+
tiers[4].totalFor(subscriptions)
6624
);
6725
}
6826
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default class Tier {
2+
constructor(
3+
private readonly from: number,
4+
private readonly to: number,
5+
private readonly price: number
6+
) {}
7+
8+
private total(): number {
9+
return this.size() * this.price;
10+
}
11+
12+
size(): number {
13+
return this.to - this.from + 1;
14+
}
15+
16+
covers(subscriptions: number): boolean {
17+
return subscriptions >= this.to;
18+
}
19+
20+
totalFor(subscriptions: number): number {
21+
if (this.covers(subscriptions)) {
22+
return this.total();
23+
}
24+
25+
if (subscriptions < this.from) {
26+
return 0;
27+
}
28+
29+
return (subscriptions - this.from + 1) * this.price;
30+
}
31+
}

0 commit comments

Comments
 (0)