@@ -2146,35 +2146,64 @@ 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 . render ( { kind : "team" , teamId } ) ;
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
+ }
2155
2166
await this . guardCostCenterAccess ( ctx , user . id , attributionId , "get" ) ;
2156
-
2157
2167
const costCenter = await this . costCenterDB . findById ( attributionId ) ;
2158
2168
if ( costCenter ) {
2159
2169
return costCenter . spendingLimit ;
2160
2170
}
2161
2171
return undefined ;
2162
2172
}
2163
2173
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 } ) ;
2174
+ async setUsageLimit ( ctx : TraceContext , attributionId : string , usageLimit : number ) : Promise < void > {
2175
+ const attrId = AttributionId . parse ( attributionId ) ;
2176
+ if ( attrId === undefined ) {
2177
+ log . error ( `Invalid attribution id: ${ attributionId } ` ) ;
2178
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attibution id: ${ attributionId } ` ) ;
2179
+ }
2168
2180
if ( typeof usageLimit !== "number" || usageLimit < 0 ) {
2169
- throw new ResponseError ( ErrorCodes . BAD_REQUEST , " Unexpected ` usageLimit` value." ) ;
2181
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , ` Unexpected usageLimit value: ${ usageLimit } ` ) ;
2170
2182
}
2171
- const attributionId = AttributionId . render ( { kind : "team" , teamId } ) ;
2172
- await this . guardCostCenterAccess ( ctx , user . id , attributionId , "update" ) ;
2173
2183
2174
- await this . costCenterDB . storeEntry ( {
2175
- id : AttributionId . render ( { kind : "team" , teamId } ) ,
2176
- spendingLimit : usageLimit ,
2177
- } ) ;
2184
+ const user = this . checkAndBlockUser ( "setUsageLimit" ) ;
2185
+ switch ( attrId . kind ) {
2186
+ case "team" :
2187
+ const team = await this . guardTeamOperation ( attrId . teamId , "get" ) ;
2188
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2189
+ break ;
2190
+ case "user" :
2191
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2192
+ break ;
2193
+ }
2194
+ await this . guardCostCenterAccess ( ctx , user . id , attributionId , "get" ) ;
2195
+ await this . costCenterDB . storeEntry ( { id : attributionId , spendingLimit : usageLimit } ) ;
2196
+ }
2197
+
2198
+ async getUsageLimitForTeam ( ctx : TraceContext , teamId : string ) : Promise < number | undefined > {
2199
+ const attributionId : AttributionId = { kind : "team" , teamId : teamId } ;
2200
+
2201
+ return this . getUsageLimit ( ctx , AttributionId . render ( attributionId ) ) ;
2202
+ }
2203
+
2204
+ async setUsageLimitForTeam ( ctx : TraceContext , teamId : string , usageLimit : number ) : Promise < void > {
2205
+ const attributionId : AttributionId = { kind : "team" , teamId : teamId } ;
2206
+ return this . setUsageLimit ( ctx , AttributionId . render ( attributionId ) , usageLimit ) ;
2178
2207
}
2179
2208
2180
2209
async getNotifications ( ctx : TraceContext ) : Promise < string [ ] > {
0 commit comments