|
4 | 4 | * See License-AGPL.txt in the project root for license information.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -import { User, WorkspaceInstance, WorkspaceTimeoutDuration } from "@gitpod/gitpod-protocol"; |
| 7 | +import { |
| 8 | + User, |
| 9 | + WorkspaceInstance, |
| 10 | + WorkspaceTimeoutDuration, |
| 11 | + WORKSPACE_TIMEOUT_DEFAULT_LONG, |
| 12 | +} from "@gitpod/gitpod-protocol"; |
| 13 | +import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; |
8 | 14 | import { inject, injectable } from "inversify";
|
9 |
| -import { EntitlementService } from "../../../src/billing/entitlement-service"; |
| 15 | +import { EntitlementService, MayStartWorkspaceResult } from "../../../src/billing/entitlement-service"; |
10 | 16 | import { Config } from "../../../src/config";
|
11 |
| -import { MayStartWorkspaceResult } from "../user/eligibility-service"; |
| 17 | +import { BillingModes } from "./billing-mode"; |
12 | 18 | import { EntitlementServiceChargebee } from "./entitlement-service-chargebee";
|
13 | 19 | import { EntitlementServiceLicense } from "./entitlement-service-license";
|
| 20 | +import { EntitlementServiceUBP } from "./entitlement-service-ubp"; |
14 | 21 |
|
15 | 22 | /**
|
16 | 23 | * The default implementation for the Enterprise Edition (EE). It decides based on config which ruleset to choose for each call.
|
| 24 | + * |
| 25 | + * As a last safety net for rolling this out, it swallows all errors and turns them into log statements. |
17 | 26 | */
|
18 | 27 | @injectable()
|
19 | 28 | export class EntitlementServiceImpl implements EntitlementService {
|
20 | 29 | @inject(Config) protected readonly config: Config;
|
21 |
| - @inject(EntitlementServiceChargebee) protected readonly etsChargebee: EntitlementServiceChargebee; |
22 |
| - @inject(EntitlementServiceLicense) protected readonly etsLicense: EntitlementServiceLicense; |
| 30 | + @inject(BillingModes) protected readonly billingModes: BillingModes; |
| 31 | + @inject(EntitlementServiceChargebee) protected readonly chargebee: EntitlementServiceChargebee; |
| 32 | + @inject(EntitlementServiceLicense) protected readonly license: EntitlementServiceLicense; |
| 33 | + @inject(EntitlementServiceUBP) protected readonly ubp: EntitlementServiceUBP; |
23 | 34 |
|
24 | 35 | async mayStartWorkspace(
|
25 | 36 | user: User,
|
26 |
| - date: Date, |
| 37 | + date: Date = new Date(), |
27 | 38 | runningInstances: Promise<WorkspaceInstance[]>,
|
28 | 39 | ): Promise<MayStartWorkspaceResult> {
|
29 |
| - if (!this.config.enablePayment) { |
30 |
| - return await this.etsLicense.mayStartWorkspace(user, date, runningInstances); |
| 40 | + try { |
| 41 | + const billingMode = await this.billingModes.getBillingModeForUser(user, date); |
| 42 | + switch (billingMode.mode) { |
| 43 | + case "none": |
| 44 | + return this.license.mayStartWorkspace(user, date, runningInstances); |
| 45 | + case "chargebee": |
| 46 | + return this.chargebee.mayStartWorkspace(user, date, runningInstances); |
| 47 | + case "usage-based": |
| 48 | + return this.ubp.mayStartWorkspace(user, date, runningInstances); |
| 49 | + } |
| 50 | + } catch (err) { |
| 51 | + log.error({ userId: user.id }, "EntitlementService error: mayStartWorkspace", err); |
| 52 | + return {}; |
31 | 53 | }
|
32 |
| - return await this.etsChargebee.mayStartWorkspace(user, date, runningInstances); |
33 | 54 | }
|
34 | 55 |
|
35 |
| - async maySetTimeout(user: User, date: Date): Promise<boolean> { |
36 |
| - if (!this.config.enablePayment) { |
37 |
| - return await this.etsLicense.maySetTimeout(user, date); |
| 56 | + async maySetTimeout(user: User, date: Date = new Date()): Promise<boolean> { |
| 57 | + try { |
| 58 | + const billingMode = await this.billingModes.getBillingModeForUser(user, date); |
| 59 | + switch (billingMode.mode) { |
| 60 | + case "none": |
| 61 | + return this.license.maySetTimeout(user, date); |
| 62 | + case "chargebee": |
| 63 | + return this.chargebee.maySetTimeout(user, date); |
| 64 | + case "usage-based": |
| 65 | + return this.ubp.maySetTimeout(user, date); |
| 66 | + } |
| 67 | + } catch (err) { |
| 68 | + log.error({ userId: user.id }, "EntitlementService error: maySetTimeout", err); |
| 69 | + return true; |
38 | 70 | }
|
39 |
| - return await this.etsChargebee.maySetTimeout(user, date); |
40 | 71 | }
|
41 | 72 |
|
42 |
| - async getDefaultWorkspaceTimeout(user: User, date: Date): Promise<WorkspaceTimeoutDuration> { |
43 |
| - if (!this.config.enablePayment) { |
44 |
| - return await this.etsLicense.getDefaultWorkspaceTimeout(user, date); |
| 73 | + async getDefaultWorkspaceTimeout(user: User, date: Date = new Date()): Promise<WorkspaceTimeoutDuration> { |
| 74 | + try { |
| 75 | + const billingMode = await this.billingModes.getBillingModeForUser(user, date); |
| 76 | + switch (billingMode.mode) { |
| 77 | + case "none": |
| 78 | + return this.license.getDefaultWorkspaceTimeout(user, date); |
| 79 | + case "chargebee": |
| 80 | + return this.chargebee.getDefaultWorkspaceTimeout(user, date); |
| 81 | + case "usage-based": |
| 82 | + return this.ubp.getDefaultWorkspaceTimeout(user, date); |
| 83 | + } |
| 84 | + } catch (err) { |
| 85 | + log.error({ userId: user.id }, "EntitlementService error: getDefaultWorkspaceTimeout", err); |
| 86 | + return WORKSPACE_TIMEOUT_DEFAULT_LONG; |
45 | 87 | }
|
46 |
| - return await this.etsChargebee.getDefaultWorkspaceTimeout(user, date); |
47 | 88 | }
|
48 | 89 |
|
49 |
| - async userGetsMoreResources(user: User): Promise<boolean> { |
50 |
| - if (!this.config.enablePayment) { |
51 |
| - return await this.etsLicense.userGetsMoreResources(user); |
| 90 | + async userGetsMoreResources(user: User, date: Date = new Date()): Promise<boolean> { |
| 91 | + try { |
| 92 | + const billingMode = await this.billingModes.getBillingModeForUser(user, date); |
| 93 | + switch (billingMode.mode) { |
| 94 | + case "none": |
| 95 | + return this.license.userGetsMoreResources(user); |
| 96 | + case "chargebee": |
| 97 | + return this.chargebee.userGetsMoreResources(user); |
| 98 | + case "usage-based": |
| 99 | + return this.ubp.userGetsMoreResources(user); |
| 100 | + } |
| 101 | + } catch (err) { |
| 102 | + log.error({ userId: user.id }, "EntitlementService error: userGetsMoreResources", err); |
| 103 | + return true; |
52 | 104 | }
|
53 |
| - return await this.etsChargebee.userGetsMoreResources(user); |
54 | 105 | }
|
55 | 106 | }
|
0 commit comments