-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathapiBalanceUtils.ts
More file actions
102 lines (89 loc) · 1.97 KB
/
apiBalanceUtils.ts
File metadata and controls
102 lines (89 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
type ApiBalanceBreakdownV1,
type ApiBalanceV1,
type ApiFeatureV1,
cusEntsToPlanId,
cusEntsToRollovers,
type FullCusEntWithFullCusProduct,
} from "@autumn/shared";
export const getBooleanApiBalance = ({
cusEnts,
apiFeature,
}: {
cusEnts: FullCusEntWithFullCusProduct[];
apiFeature?: ApiFeatureV1;
}): ApiBalanceV1 => {
const feature = cusEnts[0].entitlement.feature;
const planId = cusEntsToPlanId({ cusEnts });
const id = cusEnts[0].external_id ?? cusEnts[0].id;
return {
object: "balance",
feature: apiFeature,
feature_id: feature.id,
unlimited: false,
granted: 0,
remaining: 0,
usage: 0,
overage_allowed: false,
max_purchase: null,
next_reset_at: null,
breakdown: [
{
object: "balance_breakdown",
id,
plan_id: planId,
included_grant: 0,
prepaid_grant: 0,
remaining: 0,
usage: 0,
unlimited: false,
reset: null,
expires_at: null,
price: null,
overage: 0,
} satisfies ApiBalanceBreakdownV1,
],
rollovers: undefined,
} satisfies ApiBalanceV1;
};
export const getUnlimitedApiBalance = ({
apiFeature,
cusEnts,
}: {
apiFeature?: ApiFeatureV1;
cusEnts: FullCusEntWithFullCusProduct[];
}): ApiBalanceV1 => {
const feature = cusEnts[0].entitlement.feature;
const planId = cusEntsToPlanId({ cusEnts });
const id = cusEnts[0].external_id ?? cusEnts[0].id;
const entityId = undefined; // Unlimited features don't have entity context
return {
object: "balance",
feature: apiFeature,
feature_id: feature.id,
unlimited: true,
granted: 0,
remaining: 0,
usage: 0,
next_reset_at: null,
max_purchase: null,
overage_allowed: false,
breakdown: [
{
object: "balance_breakdown",
id,
plan_id: planId,
included_grant: 0,
prepaid_grant: 0,
remaining: 0,
usage: 0,
unlimited: true,
reset: null,
expires_at: null,
price: null,
overage: 0,
} satisfies ApiBalanceBreakdownV1,
],
rollovers: cusEntsToRollovers({ cusEnts, entityId }),
};
};