Skip to content

Commit 645d6fc

Browse files
committed
[server] Treat "no CostCenter found" the same as "spending limit reached"
1 parent 0499dda commit 645d6fc

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

components/server/ee/src/billing/billing-service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ export class BillingService {
2929

3030
async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> {
3131
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
32-
const costCenter = !!attributionId && (await this.costCenterDB.findById(AttributionId.render(attributionId)));
32+
const costCenter = await this.costCenterDB.findById(AttributionId.render(attributionId));
3333
if (!costCenter) {
3434
const err = new Error("No CostCenter found");
3535
log.error({ userId: user.id }, err.message, err, { attributionId });
36-
throw err;
36+
// Technially we do not have any spending limit set, yet. But sending users down the "reached" path will fix this issues as well.
37+
return {
38+
reached: true,
39+
attributionId,
40+
};
3741
}
3842

3943
const allSessions = await this.listBilledUsage({

components/server/src/user/user-service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,12 @@ export class UserService {
203203
return subscription?.id;
204204
}
205205

206-
protected async validateUsageAttributionId(user: User, usageAttributionId: string): Promise<void> {
206+
protected async validateUsageAttributionId(user: User, usageAttributionId: string): Promise<AttributionId> {
207207
const attribution = AttributionId.parse(usageAttributionId);
208-
if (attribution?.kind === "team") {
208+
if (!attribution) {
209+
throw new ResponseError(ErrorCodes.INVALID_COST_CENTER, "The billing team id configured is invalid.");
210+
}
211+
if (attribution.kind === "team") {
209212
const team = await this.teamDB.findTeamById(attribution.teamId);
210213
if (!team) {
211214
throw new ResponseError(
@@ -228,6 +231,7 @@ export class UserService {
228231
);
229232
}
230233
}
234+
return attribution;
231235
}
232236

233237
protected async findSingleTeamWithUsageBasedBilling(user: User): Promise<Team | undefined> {
@@ -268,14 +272,14 @@ export class UserService {
268272
*
269273
* @param user
270274
* @param projectId
275+
* @returns The validated AttributionId
271276
*/
272-
async getWorkspaceUsageAttributionId(user: User, projectId?: string): Promise<AttributionId | undefined> {
277+
async getWorkspaceUsageAttributionId(user: User, projectId?: string): Promise<AttributionId> {
273278
// A. Billing-based attribution
274279
if (this.config.enablePayment) {
275280
if (user.usageAttributionId) {
276-
await this.validateUsageAttributionId(user, user.usageAttributionId);
277281
// Return the user's explicit attribution ID.
278-
return AttributionId.parse(user.usageAttributionId);
282+
return await this.validateUsageAttributionId(user, user.usageAttributionId);
279283
}
280284
const billingTeam = await this.findSingleTeamWithUsageBasedBilling(user);
281285
if (billingTeam) {

0 commit comments

Comments
 (0)