66
77import { TeamDB } from "@gitpod/gitpod-db/lib" ;
88import {
9- BillingTier ,
10- Team ,
11- User ,
129 WorkspaceInstance ,
1310 WorkspaceTimeoutDuration ,
1411 WORKSPACE_TIMEOUT_DEFAULT_LONG ,
1512 WORKSPACE_TIMEOUT_DEFAULT_SHORT ,
1613 WORKSPACE_LIFETIME_LONG ,
1714 WORKSPACE_LIFETIME_SHORT ,
15+ User ,
16+ BillingTier ,
17+ Team ,
1818} from "@gitpod/gitpod-protocol" ;
1919import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution" ;
2020import { inject , injectable } from "inversify" ;
@@ -38,11 +38,10 @@ export class EntitlementServiceUBP implements EntitlementService {
3838 async mayStartWorkspace (
3939 user : User ,
4040 organizationId : string ,
41- date : Date ,
4241 runningInstances : Promise < WorkspaceInstance [ ] > ,
4342 ) : Promise < MayStartWorkspaceResult > {
4443 const hasHitParallelWorkspaceLimit = async ( ) : Promise < HitParallelWorkspaceLimit | undefined > => {
45- const max = await this . getMaxParallelWorkspaces ( user , date ) ;
44+ const max = await this . getMaxParallelWorkspaces ( user . id , organizationId ) ;
4645 const current = ( await runningInstances ) . filter ( ( i ) => i . status . phase !== "preparing" ) . length ;
4746 if ( current >= max ) {
4847 return {
@@ -54,7 +53,7 @@ export class EntitlementServiceUBP implements EntitlementService {
5453 }
5554 } ;
5655 const [ usageLimitReachedOnCostCenter , hitParallelWorkspaceLimit ] = await Promise . all ( [
57- this . checkUsageLimitReached ( user , organizationId , date ) ,
56+ this . checkUsageLimitReached ( user . id , organizationId ) ,
5857 hasHitParallelWorkspaceLimit ( ) ,
5958 ] ) ;
6059 return {
@@ -63,69 +62,63 @@ export class EntitlementServiceUBP implements EntitlementService {
6362 } ;
6463 }
6564
66- private async checkUsageLimitReached (
67- user : User ,
68- organizationId : string ,
69- date : Date ,
70- ) : Promise < AttributionId | undefined > {
71- const result = await this . usageService . checkUsageLimitReached ( user . id , organizationId ) ;
65+ private async checkUsageLimitReached ( userId : string , organizationId : string ) : Promise < AttributionId | undefined > {
66+ const result = await this . usageService . checkUsageLimitReached ( userId , organizationId ) ;
7267 if ( result . reached ) {
7368 return result . attributionId ;
7469 }
7570 return undefined ;
7671 }
7772
78- private async getMaxParallelWorkspaces ( user : User , date : Date ) : Promise < number > {
79- if ( await this . hasPaidSubscription ( user , date ) ) {
73+ private async getMaxParallelWorkspaces ( userId : string , organizationId : string ) : Promise < number > {
74+ if ( await this . hasPaidSubscription ( userId , organizationId ) ) {
8075 return MAX_PARALLEL_WORKSPACES_PAID ;
8176 } else {
8277 return MAX_PARALLEL_WORKSPACES_FREE ;
8378 }
8479 }
8580
86- async maySetTimeout ( user : User , date : Date ) : Promise < boolean > {
87- return this . hasPaidSubscription ( user , date ) ;
81+ async maySetTimeout ( userId : string , organizationId ?: string ) : Promise < boolean > {
82+ return this . hasPaidSubscription ( userId , organizationId ) ;
8883 }
8984
90- async getDefaultWorkspaceTimeout ( user : User , date : Date ) : Promise < WorkspaceTimeoutDuration > {
91- if ( await this . hasPaidSubscription ( user , date ) ) {
85+ async getDefaultWorkspaceTimeout ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > {
86+ if ( await this . hasPaidSubscription ( userId , organizationId ) ) {
9287 return WORKSPACE_TIMEOUT_DEFAULT_LONG ;
9388 } else {
9489 return WORKSPACE_TIMEOUT_DEFAULT_SHORT ;
9590 }
9691 }
9792
98- async getDefaultWorkspaceLifetime ( user : User , date : Date ) : Promise < WorkspaceTimeoutDuration > {
99- if ( await this . hasPaidSubscription ( user , date ) ) {
93+ async getDefaultWorkspaceLifetime ( userId : string , organizationId : string ) : Promise < WorkspaceTimeoutDuration > {
94+ if ( await this . hasPaidSubscription ( userId , organizationId ) ) {
10095 return WORKSPACE_LIFETIME_LONG ;
10196 } else {
10297 return WORKSPACE_LIFETIME_SHORT ;
10398 }
10499 }
105100
106- /**
107- * DEPRECATED: With usage-based billing, users can choose exactly how many resources they want to get.
108- * Thus, we no longer need to "force" extra resources via the `userGetsMoreResources` mechanism.
109- */
110- async userGetsMoreResources ( user : User , date : Date = new Date ( ) ) : Promise < boolean > {
111- return false ;
112- }
113-
114101 /**
115102 * Returns true if network connections should be limited
116103 * @param user
117104 */
118- async limitNetworkConnections ( user : User , date : Date ) : Promise < boolean > {
105+ async limitNetworkConnections ( userId : string , organizationId : string ) : Promise < boolean > {
119106 // gpl: Because with the current payment handling (pay-after-use) having a "paid" plan is not a good enough classifier for trushworthyness atm.
120107 // We're looking into improving this, but for the meantime we limit network connections for everybody to reduce the impact of abuse.
121108 return true ;
122109 }
123110
124- private async hasPaidSubscription ( user : User , date : Date ) : Promise < boolean > {
111+ private async hasPaidSubscription ( userId : string , organizationId ?: string ) : Promise < boolean > {
112+ if ( organizationId ) {
113+ // This is the "stricter", more correct version: We only allow privileges on the Organization that is paying for it
114+ const { billingStrategy } = await this . usageService . getCostCenter ( userId , organizationId ) ;
115+ return billingStrategy === CostCenter_BillingStrategy . BILLING_STRATEGY_STRIPE ;
116+ }
117+ // This is the old behavior, stemming from our transition to PAYF, where our API did-/doesn't pass organizationId, yet
125118 // Member of paid team?
126- const teams = await this . teamDB . findTeamsByUser ( user . id ) ;
119+ const teams = await this . teamDB . findTeamsByUser ( userId ) ;
127120 const isTeamSubscribedPromises = teams . map ( async ( team : Team ) => {
128- const { billingStrategy } = await this . usageService . getCostCenter ( user . id , team . id ) ;
121+ const { billingStrategy } = await this . usageService . getCostCenter ( userId , team . id ) ;
129122 return billingStrategy === CostCenter_BillingStrategy . BILLING_STRATEGY_STRIPE ;
130123 } ) ;
131124 // Return the first truthy promise, or false if all the promises were falsy.
@@ -147,8 +140,8 @@ export class EntitlementServiceUBP implements EntitlementService {
147140 } ) ;
148141 }
149142
150- async getBillingTier ( user : User ) : Promise < BillingTier > {
151- const hasPaidPlan = await this . hasPaidSubscription ( user , new Date ( ) ) ;
143+ async getBillingTier ( userId : string , organizationId : string ) : Promise < BillingTier > {
144+ const hasPaidPlan = await this . hasPaidSubscription ( userId , organizationId ) ;
152145 return hasPaidPlan ? "paid" : "free" ;
153146 }
154147}
0 commit comments