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