4
4
* See License.enterprise.txt in the project root folder.
5
5
*/
6
6
7
- import { inject , injectable } from ' inversify' ;
7
+ import { inject , injectable } from " inversify" ;
8
8
9
- import { TeamSubscriptionDB } from ' @gitpod/gitpod-db/lib/team-subscription-db' ;
10
- import { log , LogContext } from ' @gitpod/gitpod-protocol/lib/util/logging' ;
11
- import { Plans } from ' @gitpod/gitpod-protocol/lib/plans' ;
12
- import { TeamSubscription } from ' @gitpod/gitpod-protocol/lib/team-subscription-protocol' ;
13
- import { getCancelledAt , getStartDate } from ' ./chargebee-subscription-helper' ;
14
- import { Chargebee as chargebee } from ' ./chargebee-types' ;
15
- import { EventHandler } from ' ./chargebee-event-handler' ;
9
+ import { TeamSubscriptionDB } from " @gitpod/gitpod-db/lib/team-subscription-db" ;
10
+ import { log , LogContext } from " @gitpod/gitpod-protocol/lib/util/logging" ;
11
+ import { Plans } from " @gitpod/gitpod-protocol/lib/plans" ;
12
+ import { TeamSubscription } from " @gitpod/gitpod-protocol/lib/team-subscription-protocol" ;
13
+ import { getCancelledAt , getStartDate } from " ./chargebee-subscription-helper" ;
14
+ import { Chargebee as chargebee } from " ./chargebee-types" ;
15
+ import { EventHandler } from " ./chargebee-event-handler" ;
16
16
import { TeamSubscriptionService } from "../accounting/team-subscription-service" ;
17
- import { Config } from ' ../config' ;
17
+ import { Config } from " ../config" ;
18
18
19
19
@injectable ( )
20
20
export class TeamSubscriptionHandler implements EventHandler < chargebee . SubscriptionEventV2 > {
@@ -23,7 +23,7 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
23
23
@inject ( TeamSubscriptionService ) protected readonly service : TeamSubscriptionService ;
24
24
25
25
canHandle ( event : chargebee . Event < any > ) : boolean {
26
- if ( event . event_type . startsWith ( ' subscription' ) ) {
26
+ if ( event . event_type . startsWith ( " subscription" ) ) {
27
27
const evt = event as chargebee . Event < chargebee . SubscriptionEventV2 > ;
28
28
const plan = Plans . getById ( evt . content . subscription . plan_id ) ;
29
29
if ( ! plan ) {
@@ -53,17 +53,23 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
53
53
return true ;
54
54
}
55
55
56
- async mapToTeamSubscription ( userId : string , eventType : chargebee . EventType , chargebeeSubscription : chargebee . Subscription ) {
56
+ async mapToTeamSubscription (
57
+ userId : string ,
58
+ eventType : chargebee . EventType ,
59
+ chargebeeSubscription : chargebee . Subscription ,
60
+ ) {
57
61
await this . db . transaction ( async ( db ) => {
58
62
const subs = await db . findTeamSubscriptions ( {
59
63
userId,
60
- paymentReference : chargebeeSubscription . id
64
+ paymentReference : chargebeeSubscription . id ,
61
65
} ) ;
62
66
if ( subs . length === 0 ) {
63
67
// Sanity check: If we try to create too many slots here we OOM, so we error instead.
64
68
const quantity = chargebeeSubscription . plan_quantity ;
65
69
if ( quantity > this . config . maxTeamSlotsOnCreation ) {
66
- throw new Error ( `(TS ${ chargebeeSubscription . id } ): nr of slots on creation (${ quantity } ) is higher than configured maximum (${ this . config . maxTeamSlotsOnCreation } ). Skipping creation!` ) ;
70
+ throw new Error (
71
+ `(TS ${ chargebeeSubscription . id } ): nr of slots on creation (${ quantity } ) is higher than configured maximum (${ this . config . maxTeamSlotsOnCreation } ). Skipping creation!` ,
72
+ ) ;
67
73
}
68
74
69
75
const ts = TeamSubscription . create ( {
@@ -78,12 +84,12 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
78
84
await db . storeTeamSubscriptionEntry ( ts ) ;
79
85
await this . service . addSlots ( ts , quantity ) ;
80
86
} else {
81
- const oldSubscription = subs . find ( s => s . paymentReference === chargebeeSubscription . id ) ;
87
+ const oldSubscription = subs . find ( ( s ) => s . paymentReference === chargebeeSubscription . id ) ;
82
88
if ( ! oldSubscription ) {
83
89
throw new Error ( `Cannot find TeamSubscription for paymentReference ${ chargebeeSubscription . id } !` ) ;
84
90
}
85
91
86
- if ( eventType === ' subscription_cancelled' ) {
92
+ if ( eventType === " subscription_cancelled" ) {
87
93
const cancelledAt = getCancelledAt ( chargebeeSubscription ) ;
88
94
oldSubscription . endDate = cancelledAt ;
89
95
await this . service . deactivateAllSlots ( oldSubscription , new Date ( cancelledAt ) ) ;
@@ -97,4 +103,4 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
97
103
protected userContext ( event : chargebee . Event < chargebee . SubscriptionEventV2 > ) : LogContext {
98
104
return { userId : event . content . subscription . customer_id } ;
99
105
}
100
- }
106
+ }
0 commit comments