@@ -47,49 +47,44 @@ export interface EntitlementService {
47
47
mayStartWorkspace (
48
48
user : User ,
49
49
organizationId : string ,
50
- date : Date ,
51
50
runningInstances : Promise < WorkspaceInstance [ ] > ,
52
51
) : Promise < MayStartWorkspaceResult > ;
53
52
54
53
/**
55
54
* A user may set the workspace timeout if they have a professional subscription
56
- * @param user
57
- * @param date The date for which we want to know whether the user is allowed to set a timeout (depends on active subscription)
55
+ * @param userId
56
+ * @param organizationId
58
57
*/
59
- maySetTimeout ( user : User , date : Date ) : Promise < boolean > ;
58
+ maySetTimeout ( userId : string , organizationId ?: string ) : Promise < boolean > ;
60
59
61
60
/**
62
61
* Returns the default workspace timeout for the given user at a given point in time
63
- * @param user
64
- * @param date The date for which we want to know the default workspace timeout (depends on active subscription)
62
+ * @param userId
63
+ * @param organizationId
65
64
*/
66
- getDefaultWorkspaceTimeout ( user : User , date : Date ) : Promise < WorkspaceTimeoutDuration > ;
65
+ getDefaultWorkspaceTimeout ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > ;
67
66
68
67
/**
69
68
* Returns the default workspace lifetime for the given user at a given point in time
70
- * @param user
71
- * @param date The date for which we want to know the default workspace timeout (depends on active subscription)
72
- */
73
- getDefaultWorkspaceLifetime ( user : User , date : Date ) : Promise < WorkspaceTimeoutDuration > ;
74
-
75
- /**
76
- * Returns true if the user ought to land on a workspace cluster that provides more resources
77
- * compared to the default case.
69
+ * @param userId
70
+ * @param organizationId
78
71
*/
79
- userGetsMoreResources ( user : User ) : Promise < boolean > ;
72
+ getDefaultWorkspaceLifetime ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > ;
80
73
81
74
/**
82
75
* Returns true if network connections should be limited
83
- * @param user
76
+ * @param userId
77
+ * @param organizationId
84
78
*/
85
- limitNetworkConnections ( user : User , date : Date ) : Promise < boolean > ;
79
+ limitNetworkConnections ( userId : string , organizationId : string ) : Promise < boolean > ;
86
80
87
81
/**
88
- * Returns BillingTier of this particular user
82
+ * Returns BillingTier of this organization
89
83
*
90
- * @param user
84
+ * @param userId
85
+ * @param organizationId
91
86
*/
92
- getBillingTier ( user : User ) : Promise < BillingTier > ;
87
+ getBillingTier ( userId : string , organizationId : string ) : Promise < BillingTier > ;
93
88
}
94
89
95
90
/**
@@ -107,7 +102,6 @@ export class EntitlementServiceImpl implements EntitlementService {
107
102
async mayStartWorkspace (
108
103
user : User ,
109
104
organizationId : string ,
110
- date : Date = new Date ( ) ,
111
105
runningInstances : Promise < WorkspaceInstance [ ] > ,
112
106
) : Promise < MayStartWorkspaceResult > {
113
107
try {
@@ -117,13 +111,13 @@ export class EntitlementServiceImpl implements EntitlementService {
117
111
needsVerification : true ,
118
112
} ;
119
113
}
120
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
114
+ const billingMode = await this . billingModes . getBillingMode ( user . id , organizationId ) ;
121
115
switch ( billingMode . mode ) {
122
116
case "none" :
123
117
// if payment is not enabled users can start as many parallel workspaces as they want
124
118
return { } ;
125
119
case "usage-based" :
126
- return this . ubp . mayStartWorkspace ( user , organizationId , date , runningInstances ) ;
120
+ return this . ubp . mayStartWorkspace ( user , organizationId , runningInstances ) ;
127
121
default :
128
122
throw new Error ( "Unsupported billing mode: " + ( billingMode as any ) . mode ) ; // safety net
129
123
}
@@ -133,104 +127,87 @@ export class EntitlementServiceImpl implements EntitlementService {
133
127
}
134
128
}
135
129
136
- async maySetTimeout ( user : User , date : Date = new Date ( ) ) : Promise < boolean > {
130
+ async maySetTimeout ( userId : string , organizationId ?: string ) : Promise < boolean > {
137
131
try {
138
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
132
+ const billingMode = await this . billingModes . getBillingModeForUser ( ) ;
139
133
switch ( billingMode . mode ) {
140
134
case "none" :
141
135
// when payment is disabled users can do everything
142
136
return true ;
143
137
case "usage-based" :
144
- return this . ubp . maySetTimeout ( user , date ) ;
138
+ return this . ubp . maySetTimeout ( userId , organizationId ) ;
145
139
}
146
140
} catch ( err ) {
147
- log . error ( { userId : user . id } , "EntitlementService error: maySetTimeout" , err ) ;
141
+ log . error ( { userId } , "EntitlementService error: maySetTimeout" , err ) ;
148
142
return true ;
149
143
}
150
144
}
151
145
152
- async getDefaultWorkspaceTimeout ( user : User , date : Date = new Date ( ) ) : Promise < WorkspaceTimeoutDuration > {
146
+ async getDefaultWorkspaceTimeout ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > {
153
147
try {
154
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
148
+ const billingMode = await this . billingModes . getBillingMode ( userId , organizationId ) ;
155
149
switch ( billingMode . mode ) {
156
150
case "none" :
157
151
return WORKSPACE_TIMEOUT_DEFAULT_LONG ;
158
152
case "usage-based" :
159
- return this . ubp . getDefaultWorkspaceTimeout ( user , date ) ;
153
+ return this . ubp . getDefaultWorkspaceTimeout ( userId , organizationId ) ;
160
154
}
161
155
} catch ( err ) {
162
- log . error ( { userId : user . id } , "EntitlementService error: getDefaultWorkspaceTimeout" , err ) ;
156
+ log . error ( { userId } , "EntitlementService error: getDefaultWorkspaceTimeout" , err ) ;
163
157
return WORKSPACE_TIMEOUT_DEFAULT_LONG ;
164
158
}
165
159
}
166
160
167
- async getDefaultWorkspaceLifetime ( user : User , date : Date = new Date ( ) ) : Promise < WorkspaceTimeoutDuration > {
161
+ async getDefaultWorkspaceLifetime ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > {
168
162
try {
169
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
163
+ const billingMode = await this . billingModes . getBillingMode ( userId , organizationId ) ;
170
164
switch ( billingMode . mode ) {
171
165
case "none" :
172
166
return WORKSPACE_LIFETIME_LONG ;
173
167
case "usage-based" :
174
- return this . ubp . getDefaultWorkspaceLifetime ( user , date ) ;
168
+ return this . ubp . getDefaultWorkspaceLifetime ( userId , organizationId ) ;
175
169
}
176
170
} catch ( err ) {
177
- log . error ( { userId : user . id } , "EntitlementService error: getDefaultWorkspaceLifetime" , err ) ;
171
+ log . error ( { userId } , "EntitlementService error: getDefaultWorkspaceLifetime" , err ) ;
178
172
return WORKSPACE_LIFETIME_LONG ;
179
173
}
180
174
}
181
175
182
- async userGetsMoreResources ( user : User , date : Date = new Date ( ) ) : Promise < boolean > {
183
- try {
184
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
185
- switch ( billingMode . mode ) {
186
- case "none" :
187
- // TODO(gpl) Not sure this makes sense, but it's the way it was before
188
- return false ;
189
- case "usage-based" :
190
- return this . ubp . userGetsMoreResources ( user ) ;
191
- }
192
- } catch ( err ) {
193
- log . error ( { userId : user . id } , "EntitlementService error: userGetsMoreResources" , err ) ;
194
- return true ;
195
- }
196
- }
197
-
198
176
/**
199
177
* Returns true if network connections should be limited
200
178
* @param user
201
179
*/
202
- async limitNetworkConnections ( user : User , date : Date ) : Promise < boolean > {
180
+ async limitNetworkConnections ( userId : string , organizationId : string ) : Promise < boolean > {
203
181
try {
204
- const billingMode = await this . billingModes . getBillingModeForUser ( user , date ) ;
182
+ const billingMode = await this . billingModes . getBillingMode ( userId , organizationId ) ;
205
183
switch ( billingMode . mode ) {
206
184
case "none" :
207
185
return false ;
208
186
case "usage-based" :
209
- return this . ubp . limitNetworkConnections ( user , date ) ;
187
+ return this . ubp . limitNetworkConnections ( userId , organizationId ) ;
210
188
}
211
189
} catch ( err ) {
212
- log . error ( { userId : user . id } , "EntitlementService error: limitNetworkConnections" , err ) ;
190
+ log . error ( { userId } , "EntitlementService error: limitNetworkConnections" , err ) ;
213
191
return false ;
214
192
}
215
193
}
216
194
217
195
/**
218
196
* Returns true if network connections should be limited
219
- * @param user
197
+ * @param userId
198
+ * @param organizationId
220
199
*/
221
- async getBillingTier ( user : User ) : Promise < BillingTier > {
200
+ async getBillingTier ( userId : string , organizationId : string ) : Promise < BillingTier > {
222
201
try {
223
- const now = new Date ( ) ;
224
- const billingMode = await this . billingModes . getBillingModeForUser ( user , now ) ;
202
+ const billingMode = await this . billingModes . getBillingMode ( userId , organizationId ) ;
225
203
switch ( billingMode . mode ) {
226
204
case "none" :
227
- // TODO(gpl) Is this true? Cross-check this whole interface with Self-Hosted before next release!
228
205
return "paid" ;
229
206
case "usage-based" :
230
- return this . ubp . getBillingTier ( user ) ;
207
+ return billingMode . paid ? "paid" : "free" ;
231
208
}
232
209
} catch ( err ) {
233
- log . error ( { userId : user . id } , "EntitlementService error: getBillingTier" , err ) ;
210
+ log . error ( { userId } , "EntitlementService error: getBillingTier" , err ) ;
234
211
return "paid" ;
235
212
}
236
213
}
0 commit comments