@@ -2146,39 +2146,80 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2146
2146
}
2147
2147
}
2148
2148
2149
- async getUsageLimitForTeam ( ctx : TraceContext , teamId : string ) : Promise < number | undefined > {
2150
- const user = this . checkAndBlockUser ( "getUsageLimitForTeam" ) ;
2151
- const team = await this . guardTeamOperation ( teamId , "get" ) ;
2152
- await this . ensureStripeApiIsAllowed ( { team } ) ;
2149
+ async getUsageLimit ( ctx : TraceContext , attributionId : string ) : Promise < number | undefined > {
2150
+ const attrId = AttributionId . parse ( attributionId ) ;
2151
+ if ( attrId === undefined ) {
2152
+ log . error ( `Invalid attribution id: ${ attributionId } ` ) ;
2153
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attibution id: ${ attributionId } ` ) ;
2154
+ }
2153
2155
2154
- const attributionId : AttributionId = { kind : "team" , teamId } ;
2155
- await this . guardCostCenterAccess ( ctx , user . id , attributionId , "get" ) ;
2156
+ const user = this . checkAndBlockUser ( "getUsageLimit" ) ;
2157
+ switch ( attrId . kind ) {
2158
+ case "team" :
2159
+ const team = await this . guardTeamOperation ( attrId . teamId , "get" ) ;
2160
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2161
+ break ;
2162
+ case "user" :
2163
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2164
+ break ;
2165
+ }
2166
+ await this . guardCostCenterAccess ( ctx , user . id , attrId , "get" ) ;
2156
2167
2157
- const costCenter = await this . usageServiceClientProvider . getDefault ( ) . getCostCenter ( attributionId ) ;
2168
+ const costCenter = await this . usageServiceClientProvider . getDefault ( ) . getCostCenter ( attrId ) ;
2158
2169
if ( costCenter ) {
2159
2170
return costCenter . spendingLimit ;
2160
2171
}
2161
2172
return undefined ;
2162
2173
}
2163
2174
2164
- async setUsageLimitForTeam ( ctx : TraceContext , teamId : string , usageLimit : number ) : Promise < void > {
2165
- const user = this . checkAndBlockUser ( "setUsageLimitForTeam" ) ;
2166
- const team = await this . guardTeamOperation ( teamId , "update" ) ;
2167
- await this . ensureStripeApiIsAllowed ( { team } ) ;
2175
+ async setUsageLimit ( ctx : TraceContext , attributionId : string , usageLimit : number ) : Promise < void > {
2176
+ const attrId = AttributionId . parse ( attributionId ) ;
2177
+ if ( attrId === undefined ) {
2178
+ log . error ( `Invalid attribution id: ${ attributionId } ` ) ;
2179
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attibution id: ${ attributionId } ` ) ;
2180
+ }
2168
2181
if ( typeof usageLimit !== "number" || usageLimit < 0 ) {
2169
- throw new ResponseError ( ErrorCodes . BAD_REQUEST , "Unexpected `usageLimit` value." ) ;
2182
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Unexpected usageLimit value: ${ usageLimit } ` ) ;
2183
+ }
2184
+
2185
+ const user = this . checkAndBlockUser ( "setUsageLimit" ) ;
2186
+ switch ( attrId . kind ) {
2187
+ case "team" :
2188
+ const team = await this . guardTeamOperation ( attrId . teamId , "update" ) ;
2189
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2190
+ break ;
2191
+ case "user" :
2192
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2193
+ break ;
2194
+ }
2195
+ await this . guardCostCenterAccess ( ctx , user . id , attrId , "update" ) ;
2196
+
2197
+ const costCenter = await this . usageServiceClientProvider . getDefault ( ) . getCostCenter ( attrId ) ;
2198
+ if ( costCenter ?. billingStrategy !== "stripe" ) {
2199
+ throw new ResponseError (
2200
+ ErrorCodes . BAD_REQUEST ,
2201
+ `Setting a usage limit is not valid for non-Stripe billing strategies` ,
2202
+ ) ;
2170
2203
}
2171
- const attributionId : AttributionId = { kind : "team" , teamId } ;
2172
- await this . guardCostCenterAccess ( ctx , user . id , attributionId , "update" ) ;
2173
2204
2174
- const costCenter = await this . usageServiceClientProvider . getDefault ( ) . getCostCenter ( attributionId ) ;
2175
2205
await this . usageServiceClientProvider . getDefault ( ) . setCostCenter ( {
2176
- id : attributionId ,
2206
+ id : attrId ,
2177
2207
spendingLimit : usageLimit ,
2178
2208
billingStrategy : costCenter ?. billingStrategy || "other" ,
2179
2209
} ) ;
2180
2210
}
2181
2211
2212
+ async getUsageLimitForTeam ( ctx : TraceContext , teamId : string ) : Promise < number | undefined > {
2213
+ const attributionId : AttributionId = { kind : "team" , teamId : teamId } ;
2214
+
2215
+ return this . getUsageLimit ( ctx , AttributionId . render ( attributionId ) ) ;
2216
+ }
2217
+
2218
+ async setUsageLimitForTeam ( ctx : TraceContext , teamId : string , usageLimit : number ) : Promise < void > {
2219
+ const attributionId : AttributionId = { kind : "team" , teamId : teamId } ;
2220
+ return this . setUsageLimit ( ctx , AttributionId . render ( attributionId ) , usageLimit ) ;
2221
+ }
2222
+
2182
2223
async getNotifications ( ctx : TraceContext ) : Promise < string [ ] > {
2183
2224
const result = await super . getNotifications ( ctx ) ;
2184
2225
const user = this . checkAndBlockUser ( "getNotifications" ) ;
0 commit comments