-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: free tiers should be removed from volume based if they exists
- Loading branch information
Showing
3 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@crystallize/js-api-client", | ||
"license": "MIT", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"author": "Crystallize <[email protected]> (https://crystallize.com)", | ||
"contributors": [ | ||
"Sébastien Morel <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
import { Prices, Tier } from '../types/pricing'; | ||
|
||
export function pricesForUsageOnTier(usage: number, tiers: Tier[], tierType: 'volume' | 'graduated'): Prices { | ||
const sortedTiers = [...tiers].sort((a: Tier, b: Tier) => a.threshold - b.threshold); | ||
const sortedTiers = tiers.sort((a: Tier, b: Tier) => a.threshold - b.threshold); | ||
// let's add the implicit tiers id it does not exists | ||
if (sortedTiers[0].threshold > 0) { | ||
sortedTiers.unshift({ threshold: 0, price: 0, currency: tiers[0].currency }); | ||
} | ||
|
||
if (tierType === 'volume') { | ||
return volumeBasedPriceFor(usage, sortedTiers); | ||
return volumeBasedPriceFor(Math.max(0, usage), sortedTiers); | ||
} | ||
return graduatedBasedPriceFor(usage, sortedTiers); | ||
} | ||
|
||
function volumeBasedPriceFor(usage: number, tiers: Tier[]): Prices { | ||
const freeUsage = tiers.reduce((memo: number, tier: Tier, tierIndex) => { | ||
if (tier.price === 0) { | ||
return tiers[tierIndex + 1]?.threshold || 0; | ||
} | ||
return memo; | ||
}, 0); | ||
const forCalculationUsage = Math.max(0, usage - freeUsage); | ||
const tiersLength = tiers.length; | ||
|
||
for (let i = tiersLength - 1; i >= 0; i--) { | ||
const tier: Tier = tiers[i]; | ||
if (usage < tier.threshold && i > 0) { | ||
continue; | ||
} | ||
// manage also an inexistent tier (threshold = 0) | ||
return { [tier.currency]: (usage >= tier.threshold ? tier.price : 0) * usage }; | ||
return { [tier.currency]: (usage >= tier.threshold ? tier.price || 0 : 0) * forCalculationUsage }; | ||
} | ||
return { USD: 0.0 }; | ||
} | ||
|
||
function graduatedBasedPriceFor(usage: number, tiers: Tier[]): Prices { | ||
let rest = usage; | ||
|
||
// manage also an inexistent tier (threshold = 0) | ||
if (tiers[0].threshold > 0) { | ||
rest = Math.max(0, rest - (tiers[0].threshold - 1)); | ||
} | ||
|
||
const splitUsage: Array<Tier & { usage: number }> = tiers.map((tier: Tier, tierIndex: number) => { | ||
const limit = tiers[tierIndex + 1]?.threshold || Infinity; | ||
const tierUsage = rest > limit ? limit : rest; | ||
const currentThreshold = tier.threshold; | ||
const nextThreshold = tiers[tierIndex + 1]?.threshold; | ||
const maxTierUsage = nextThreshold ? nextThreshold - currentThreshold : Infinity; | ||
const tierUsage = rest <= maxTierUsage ? rest : maxTierUsage; | ||
rest -= tierUsage; | ||
return { | ||
...tier, | ||
usage: tierUsage, | ||
}; | ||
}); | ||
|
||
return splitUsage.reduce((memo: Prices, tier: Tier & { usage: number }) => { | ||
return { | ||
...memo, | ||
[tier.currency]: (memo[tier.currency] || 0.0) + tier.usage * tier.price, | ||
[tier.currency]: (memo[tier.currency] || 0.0) + tier.usage * (tier.price || 0), | ||
}; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters