@@ -69,126 +69,133 @@ export class BillingModesImpl implements BillingModes {
69
69
return { mode : "none" } ;
70
70
}
71
71
72
- // Is Usage Based Billing enabled for this user or not?
73
- const teams = await this . teamDB . findTeamsByUser ( user . id ) ;
74
- const isUsageBasedBillingEnabled = await this . configCatClientFactory ( ) . getValueAsync (
75
- "isUsageBasedBillingEnabled" ,
76
- false ,
77
- {
78
- user,
79
- teams,
80
- } ,
81
- ) ;
82
-
83
- // 1. UBB enabled?
84
- if ( ! isUsageBasedBillingEnabled ) {
85
- // UBB is not enabled: definitely chargebee
86
- return { mode : "chargebee" } ;
87
- }
88
-
89
- // 2. Any personal subscriptions?
90
- // Chargebee takes precedence
91
- function isTeamSubscription ( s : Subscription ) : boolean {
92
- return ! ! Plans . getById ( s . planId ) ?. team ;
93
- }
94
- const cbSubscriptions = await this . subscriptionSvc . getActivePaidSubscription ( user . id , now ) ;
95
- const cbTeamSubscriptions = cbSubscriptions . filter ( ( s ) => isTeamSubscription ( s ) ) ;
96
- const cbPersonalSubscriptions = cbSubscriptions . filter (
97
- ( s ) => ! isTeamSubscription ( s ) && s . planId !== Plans . FREE_OPEN_SOURCE . chargebeeId ,
98
- ) ;
99
- let canUpgradeToUBB = false ;
100
- if ( cbPersonalSubscriptions . length > 0 ) {
101
- if ( cbPersonalSubscriptions . every ( ( s ) => Subscription . isCancelled ( s , now . toISOString ( ) ) ) ) {
102
- // The user has one or more paid subscriptions, but all of them have already been cancelled
103
- canUpgradeToUBB = true ;
104
- } else {
105
- // The user has at least one paid personal subscription
106
- return {
107
- mode : "chargebee" ,
108
- } ;
109
- }
110
- }
111
-
112
- // Stripe: Active personal subsciption?
113
- let hasUbbPersonal = false ;
114
- const customerId = await this . stripeSvc . findCustomerByUserId ( user . id ) ;
115
- if ( customerId ) {
116
- const subscriptionId = await this . stripeSvc . findUncancelledSubscriptionByCustomer ( customerId ) ;
117
- if ( subscriptionId ) {
118
- hasUbbPersonal = true ;
119
- }
120
- }
121
-
122
- // 3. Check team memberships/plans
123
- // UBB overrides wins if there is _any_. But if there is none, use the existing Chargebee subscription.
124
- const teamsModes = await Promise . all ( teams . map ( ( t ) => this . getBillingModeForTeam ( t , now ) ) ) ;
125
- const hasUbbPaidTeam = teamsModes . some ( ( tm ) => tm . mode === "usage-based" && ! ! tm . paid ) ;
126
- const hasCbTeam = teamsModes . some ( ( tm ) => tm . mode === "chargebee" ) ;
127
- const hasCbTeamSeat = cbTeamSubscriptions . length > 0 ;
128
-
129
- if ( hasUbbPaidTeam || hasUbbPersonal ) {
130
- // UBB is greedy: once a user has at least a paid team membership, they should benefit from it!
131
- const result : BillingMode = { mode : "usage-based" } ;
132
- if ( hasCbTeam ) {
133
- result . hasChargebeeTeamPlan = true ;
134
- }
135
- if ( hasCbTeamSeat ) {
136
- result . hasChargebeeTeamSubscription = true ;
137
- }
138
- return result ;
139
- }
140
- if ( hasCbTeam || hasCbTeamSeat || canUpgradeToUBB ) {
141
- // TODO(gpl): Q: How to test the free-tier, then? A: Make sure you have no CB seats anymore
142
- // For that we could add a new field here, which lists all seats that are "blocking" you, and display them in the UI somewhere.
143
- return { mode : "chargebee" , canUpgradeToUBB : true } ; // UBB is enabled, but no seat nor subscription yet.
144
- }
145
-
146
- // UBB free tier
72
+ // HACK
147
73
return { mode : "usage-based" } ;
74
+
75
+ // Is Usage Based Billing enabled for this user or not?
76
+ // const teams = await this.teamDB.findTeamsByUser(user.id);
77
+ // const isUsageBasedBillingEnabled = await this.configCatClientFactory().getValueAsync(
78
+ // "isUsageBasedBillingEnabled",
79
+ // false,
80
+ // {
81
+ // user,
82
+ // teams,
83
+ // },
84
+ // );
85
+
86
+ // // 1. UBB enabled?
87
+ // if (!isUsageBasedBillingEnabled) {
88
+ // // UBB is not enabled: definitely chargebee
89
+ // return { mode: "chargebee" };
90
+ // }
91
+
92
+ // // 2. Any personal subscriptions?
93
+ // // Chargebee takes precedence
94
+ // function isTeamSubscription(s: Subscription): boolean {
95
+ // return !!Plans.getById(s.planId)?.team;
96
+ // }
97
+ // const cbSubscriptions = await this.subscriptionSvc.getActivePaidSubscription(user.id, now);
98
+ // const cbTeamSubscriptions = cbSubscriptions.filter((s) => isTeamSubscription(s));
99
+ // const cbPersonalSubscriptions = cbSubscriptions.filter(
100
+ // (s) => !isTeamSubscription(s) && s.planId !== Plans.FREE_OPEN_SOURCE.chargebeeId,
101
+ // );
102
+ // let canUpgradeToUBB = false;
103
+ // if (cbPersonalSubscriptions.length > 0) {
104
+ // if (cbPersonalSubscriptions.every((s) => Subscription.isCancelled(s, now.toISOString()))) {
105
+ // // The user has one or more paid subscriptions, but all of them have already been cancelled
106
+ // canUpgradeToUBB = true;
107
+ // } else {
108
+ // // The user has at least one paid personal subscription
109
+ // return {
110
+ // mode: "chargebee",
111
+ // };
112
+ // }
113
+ // }
114
+
115
+ // // Stripe: Active personal subsciption?
116
+ // let hasUbbPersonal = false;
117
+ // const customerId = await this.stripeSvc.findCustomerByUserId(user.id);
118
+ // if (customerId) {
119
+ // const subscriptionId = await this.stripeSvc.findUncancelledSubscriptionByCustomer(customerId);
120
+ // if (subscriptionId) {
121
+ // hasUbbPersonal = true;
122
+ // }
123
+ // }
124
+
125
+ // // 3. Check team memberships/plans
126
+ // // UBB overrides wins if there is _any_. But if there is none, use the existing Chargebee subscription.
127
+ // const teamsModes = await Promise.all(teams.map((t) => this.getBillingModeForTeam(t, now)));
128
+ // const hasUbbPaidTeam = teamsModes.some((tm) => tm.mode === "usage-based" && !!tm.paid);
129
+ // const hasCbTeam = teamsModes.some((tm) => tm.mode === "chargebee");
130
+ // const hasCbTeamSeat = cbTeamSubscriptions.length > 0;
131
+
132
+ // if (hasUbbPaidTeam || hasUbbPersonal) {
133
+ // // UBB is greedy: once a user has at least a paid team membership, they should benefit from it!
134
+ // const result: BillingMode = { mode: "usage-based" };
135
+ // if (hasCbTeam) {
136
+ // result.hasChargebeeTeamPlan = true;
137
+ // }
138
+ // if (hasCbTeamSeat) {
139
+ // result.hasChargebeeTeamSubscription = true;
140
+ // }
141
+ // return result;
142
+ // }
143
+ // if (hasCbTeam || hasCbTeamSeat || canUpgradeToUBB) {
144
+ // // TODO(gpl): Q: How to test the free-tier, then? A: Make sure you have no CB seats anymore
145
+ // // For that we could add a new field here, which lists all seats that are "blocking" you, and display them in the UI somewhere.
146
+ // return { mode: "chargebee", canUpgradeToUBB: true }; // UBB is enabled, but no seat nor subscription yet.
147
+ // }
148
+
149
+ // // UBB free tier
150
+ // return { mode: "usage-based" };
148
151
}
149
152
150
153
async getBillingModeForTeam ( team : Team , _now : Date ) : Promise < BillingMode > {
151
154
if ( ! this . config . enablePayment ) {
152
155
// Payment is not enabled. E.g. Self-Hosted.
153
156
return { mode : "none" } ;
154
157
}
155
- const now = _now . toISOString ( ) ;
156
-
157
- // Is Usage Based Billing enabled for this team?
158
- const isUsageBasedBillingEnabled = await this . configCatClientFactory ( ) . getValueAsync (
159
- "isUsageBasedBillingEnabled" ,
160
- false ,
161
- {
162
- teamId : team . id ,
163
- teamName : team . name ,
164
- } ,
165
- ) ;
166
-
167
- // 1. UBB enabled?
168
- if ( ! isUsageBasedBillingEnabled ) {
169
- return { mode : "chargebee" } ;
170
- }
171
158
172
- // 2. Any Chargbee TeamSubscription2 (old Team Subscriptions are not relevant here, as they are not associated with a team)
173
- const teamSubscription = await this . teamSubscription2Db . findForTeam ( team . id , now ) ;
174
- if ( teamSubscription && TeamSubscription2 . isActive ( teamSubscription , now ) ) {
175
- if ( TeamSubscription2 . isCancelled ( teamSubscription , now ) ) {
176
- // The team has a paid subscription, but it's already cancelled, and UBB enabled
177
- return { mode : "chargebee" , canUpgradeToUBB : true } ;
178
- }
179
-
180
- return { mode : "chargebee" } ;
181
- }
159
+ // HACK
160
+ return { mode : "usage-based" } ;
182
161
183
- // 3. Now we're usage-based. We only have to figure out whether we have a plan yet or not.
184
- const result : BillingMode = { mode : "usage-based" } ;
185
- const customerId = await this . stripeSvc . findCustomerByTeamId ( team . id ) ;
186
- if ( customerId ) {
187
- const subscriptionId = await this . stripeSvc . findUncancelledSubscriptionByCustomer ( customerId ) ;
188
- if ( subscriptionId ) {
189
- result . paid = true ;
190
- }
191
- }
192
- return result ;
162
+ // const now = _now.toISOString();
163
+
164
+ // // Is Usage Based Billing enabled for this team?
165
+ // const isUsageBasedBillingEnabled = await this.configCatClientFactory().getValueAsync(
166
+ // "isUsageBasedBillingEnabled",
167
+ // false,
168
+ // {
169
+ // teamId: team.id,
170
+ // teamName: team.name,
171
+ // },
172
+ // );
173
+
174
+ // // 1. UBB enabled?
175
+ // if (!isUsageBasedBillingEnabled) {
176
+ // return { mode: "chargebee" };
177
+ // }
178
+
179
+ // // 2. Any Chargbee TeamSubscription2 (old Team Subscriptions are not relevant here, as they are not associated with a team)
180
+ // const teamSubscription = await this.teamSubscription2Db.findForTeam(team.id, now);
181
+ // if (teamSubscription && TeamSubscription2.isActive(teamSubscription, now)) {
182
+ // if (TeamSubscription2.isCancelled(teamSubscription, now)) {
183
+ // // The team has a paid subscription, but it's already cancelled, and UBB enabled
184
+ // return { mode: "chargebee", canUpgradeToUBB: true };
185
+ // }
186
+
187
+ // return { mode: "chargebee" };
188
+ // }
189
+
190
+ // // 3. Now we're usage-based. We only have to figure out whether we have a plan yet or not.
191
+ // const result: BillingMode = { mode: "usage-based" };
192
+ // const customerId = await this.stripeSvc.findCustomerByTeamId(team.id);
193
+ // if (customerId) {
194
+ // const subscriptionId = await this.stripeSvc.findUncancelledSubscriptionByCustomer(customerId);
195
+ // if (subscriptionId) {
196
+ // result.paid = true;
197
+ // }
198
+ // }
199
+ // return result;
193
200
}
194
201
}
0 commit comments