Skip to content

Commit f59978f

Browse files
committed
fix(knowledge): key the usage gate cache by billing period source
1 parent 9d6e7e7 commit f59978f

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎apps/sim/lib/billing/core/usage-gate-cache.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('checkIngestionUsageLimits', () => {
8585
expect(mockCheck).toHaveBeenCalledTimes(2)
8686
})
8787

88-
it('separates answers by actor, period, payer and plan', async () => {
88+
it('separates answers by actor, period, period source, payer and plan', async () => {
8989
await checkIngestionUsageLimits(ATTRIBUTION)
9090
await checkIngestionUsageLimits({ ...ATTRIBUTION, actorUserId: 'member-2' })
9191
await checkIngestionUsageLimits({
@@ -97,12 +97,16 @@ describe('checkIngestionUsageLimits', () => {
9797
billedAccountUserId: 'owner-2',
9898
billingEntity: { type: 'user', id: 'owner-2' },
9999
})
100+
await checkIngestionUsageLimits({
101+
...ATTRIBUTION,
102+
billingPeriod: { ...ATTRIBUTION.billingPeriod, source: 'reporting' },
103+
})
100104
await checkIngestionUsageLimits({ ...ATTRIBUTION, payerSubscription: SUBSCRIPTION })
101105
await checkIngestionUsageLimits({
102106
...ATTRIBUTION,
103107
payerSubscription: { ...SUBSCRIPTION, plan: 'enterprise' },
104108
})
105-
expect(mockCheck).toHaveBeenCalledTimes(6)
109+
expect(mockCheck).toHaveBeenCalledTimes(7)
106110
})
107111

108112
it('does not cache a failed read', async () => {

‎apps/sim/lib/billing/core/usage-gate-cache.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const gateCache = new LRUCache<string, AttributedUsageLimitsResult>({
2828
})
2929

3030
/**
31-
* The gate depends on who pays, for which period, under which plan, and which
32-
* member acts: the payer pool, its limit and the per-member cap are all part of
33-
* the answer. The workspace is not, so every workspace of one payer shares an
34-
* entry.
31+
* The gate depends on who pays, for which period (and how that period was
32+
* derived), under which plan, and which member acts: the payer pool, its limit
33+
* and the per-member cap are all part of the answer. The workspace is not, so
34+
* every workspace of one payer shares an entry.
3535
*/
3636
function gateKey(attribution: BillingAttributionSnapshot): string {
3737
const subscription = attribution.payerSubscription
@@ -40,6 +40,7 @@ function gateKey(attribution: BillingAttributionSnapshot): string {
4040
attribution.billingEntity.id,
4141
attribution.billingPeriod.start,
4242
attribution.billingPeriod.end,
43+
attribution.billingPeriod.source ?? '',
4344
attribution.billedAccountUserId,
4445
attribution.actorUserId,
4546
subscription?.id ?? '',

0 commit comments

Comments
 (0)