diff --git a/components/usage-api/typescript/src/usage/v1/sugar.ts b/components/usage-api/typescript/src/usage/v1/sugar.ts index fcd2a294dd89d7..4392a9740f2bed 100644 --- a/components/usage-api/typescript/src/usage/v1/sugar.ts +++ b/components/usage-api/typescript/src/usage/v1/sugar.ts @@ -9,7 +9,15 @@ import { BillingServiceClient } from "./billing_grpc_pb"; import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing"; import * as opentracing from "opentracing"; import { Metadata } from "@grpc/grpc-js"; -import { CostCenter as ProtocolCostCenter, GetCostCenterRequest, GetCostCenterResponse, ListUsageRequest, ListUsageResponse, SetCostCenterRequest, SetCostCenterResponse } from "./usage_pb"; +import { + CostCenter as ProtocolCostCenter, + GetCostCenterRequest, + GetCostCenterResponse, + ListUsageRequest, + ListUsageResponse, + SetCostCenterRequest, + SetCostCenterResponse, +} from "./usage_pb"; import { GetUpcomingInvoiceRequest, GetUpcomingInvoiceResponse, @@ -22,7 +30,7 @@ import { createClientCallMetricsInterceptor, IClientCallMetrics } from "@gitpod/ import * as grpc from "@grpc/grpc-js"; import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb"; import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution"; -import { CostCenter } from "@gitpod/gitpod-protocol"; +import { CostCenter, BillingStrategy } from "@gitpod/gitpod-protocol"; export const UsageServiceClientProvider = Symbol("UsageServiceClientProvider"); export const BillingServiceClientProvider = Symbol("BillingServiceClientProvider"); @@ -148,10 +156,7 @@ export class PromisifiedUsageServiceClient { ); } - public async listUsage( - _ctx: TraceContext, - request: ListUsageRequest, - ): Promise { + public async listUsage(_ctx: TraceContext, request: ListUsageRequest): Promise { const ctx = TraceContext.childContext(`/usage-service/listUsage`, _ctx); try { const response = await new Promise((resolve, reject) => { @@ -176,42 +181,49 @@ export class PromisifiedUsageServiceClient { } } - public async getCostCenter( - attributionID: AttributionId, - ): Promise { + public async getCostCenter(attributionID: AttributionId): Promise { const request = new GetCostCenterRequest(); - request.setAttributionId(AttributionId.render(attributionID)) + request.setAttributionId(AttributionId.render(attributionID)); const response = await new Promise((resolve, reject) => { - this.client.getCostCenter( - request, - (err: grpc.ServiceError | null, response: GetCostCenterResponse) => { - if (err) { - reject(err); - return; - } - resolve(response); - }, - ); + this.client.getCostCenter(request, (err: grpc.ServiceError | null, response: GetCostCenterResponse) => { + if (err) { + reject(err); + return; + } + resolve(response); + }); }); if (!response.hasCostCenter()) { return undefined; } - return { - id: AttributionId.parse(response.getCostCenter()!.getAttributionId()), + + const attrId = AttributionId.parse(response.getCostCenter()!.getAttributionId()); + if (!attrId) { + return undefined; + } + + let billingStrategy: BillingStrategy = "other"; + if ( + response.getCostCenter()!.getBillingStrategy() === + ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_STRIPE + ) { + billingStrategy = "stripe"; + } + + return { + id: attrId, spendingLimit: response.getCostCenter()!.getSpendingLimit(), - billingStrategy: response.getCostCenter()!.getBillingStrategy() || 'other' + billingStrategy: billingStrategy, }; } - public async setCostCenter( - costCenter: CostCenter, - ): Promise { + public async setCostCenter(costCenter: CostCenter): Promise { const request = new SetCostCenterRequest(); const cc = new ProtocolCostCenter(); cc.setAttributionId(AttributionId.render(costCenter.id)); let billingStrategy = ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_OTHER; - if (costCenter.billingStrategy == 'stripe') { + if (costCenter.billingStrategy == "stripe") { billingStrategy = ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_STRIPE; } cc.setBillingStrategy(billingStrategy); @@ -219,16 +231,13 @@ export class PromisifiedUsageServiceClient { request.setCostCenter(cc); await new Promise((resolve, reject) => { - this.client.setCostCenter( - request, - (err: grpc.ServiceError | null, response: SetCostCenterResponse) => { - if (err) { - reject(err); - return; - } - resolve(response); - }, - ); + this.client.setCostCenter(request, (err: grpc.ServiceError | null, response: SetCostCenterResponse) => { + if (err) { + reject(err); + return; + } + resolve(response); + }); }); }