4
4
* See License.enterprise.txt in the project root folder.
5
5
*/
6
6
7
- import { inject , injectable } from ' inversify' ;
8
-
9
- import { SubscriptionService } from ' ../accounting/subscription-service' ;
10
- import { AccountingDB } from ' @gitpod/gitpod-db/lib/accounting-db' ;
11
- import { log , LogContext } from ' @gitpod/gitpod-protocol/lib/util/logging' ;
12
- import { SubscriptionMapperFactory } from ' ./subscription-mapper' ;
13
- import { Plans } from ' @gitpod/gitpod-protocol/lib/plans' ;
14
- import { Chargebee as chargebee } from ' ./chargebee-types' ;
15
- import { EventHandler } from ' ./chargebee-event-handler' ;
16
- import { UpgradeHelper } from ' ./upgrade-helper' ;
7
+ import { inject , injectable } from " inversify" ;
8
+
9
+ import { SubscriptionService } from " ../accounting/subscription-service" ;
10
+ import { AccountingDB } from " @gitpod/gitpod-db/lib/accounting-db" ;
11
+ import { log , LogContext } from " @gitpod/gitpod-protocol/lib/util/logging" ;
12
+ import { SubscriptionMapper } from " ./subscription-mapper" ;
13
+ import { Plans } from " @gitpod/gitpod-protocol/lib/plans" ;
14
+ import { Chargebee as chargebee } from " ./chargebee-types" ;
15
+ import { EventHandler } from " ./chargebee-event-handler" ;
16
+ import { UpgradeHelper } from " ./upgrade-helper" ;
17
17
import { formatDate } from "@gitpod/gitpod-protocol/lib/util/date-time" ;
18
- import { getUpdatedAt } from ' ./chargebee-subscription-helper' ;
19
- import { UserPaidSubscription } from ' @gitpod/gitpod-protocol/lib/accounting-protocol' ;
20
- import { DBSubscriptionAdditionalData } from ' @gitpod/gitpod-db/lib/typeorm/entity/db-subscription' ;
18
+ import { getUpdatedAt } from " ./chargebee-subscription-helper" ;
19
+ import { UserPaidSubscription } from " @gitpod/gitpod-protocol/lib/accounting-protocol" ;
20
+ import { DBSubscriptionAdditionalData } from " @gitpod/gitpod-db/lib/typeorm/entity/db-subscription" ;
21
21
22
22
@injectable ( )
23
23
export class SubscriptionHandler implements EventHandler < chargebee . SubscriptionEventV2 > {
24
24
@inject ( SubscriptionService ) protected readonly subscriptionService : SubscriptionService ;
25
25
@inject ( AccountingDB ) protected readonly db : AccountingDB ;
26
- @inject ( SubscriptionMapperFactory ) protected readonly mapperFactory : SubscriptionMapperFactory ;
26
+ @inject ( SubscriptionMapper ) protected readonly mapper : SubscriptionMapper ;
27
27
@inject ( UpgradeHelper ) protected readonly upgradeHelper : UpgradeHelper ;
28
28
29
29
canHandle ( event : chargebee . Event < any > ) : boolean {
30
- if ( event . event_type . startsWith ( ' subscription' ) ) {
30
+ if ( event . event_type . startsWith ( " subscription" ) ) {
31
31
const evt = event as chargebee . Event < chargebee . SubscriptionEventV2 > ;
32
32
const plan = Plans . getById ( evt . content . subscription . plan_id ) ;
33
33
return ! ! plan && ! plan . team ;
@@ -44,16 +44,16 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
44
44
log . debug ( logContext , `Start SubscriptionHandler.handleSingleEvent` , { eventType } ) ;
45
45
try {
46
46
if ( ! event . content . subscription ) {
47
- log . error ( logContext , ' Ignoring event, because it does not contain a subscription' , event ) ;
47
+ log . error ( logContext , " Ignoring event, because it does not contain a subscription" , event ) ;
48
48
} else {
49
49
try {
50
50
await this . storeAdditionalData ( event . content . subscription , event . content . invoice ) ;
51
- } catch ( err ) {
52
- log . error ( logContext , ' Failed to store additional subscription data' , event ) ;
51
+ } catch ( err ) {
52
+ log . error ( logContext , " Failed to store additional subscription data" , event ) ;
53
53
}
54
54
}
55
55
56
- if ( event . event_type === ' subscription_changed' ) {
56
+ if ( event . event_type === " subscription_changed" ) {
57
57
await this . checkAndChargeForUpgrade ( userId , chargebeeSubscription ) ;
58
58
}
59
59
@@ -70,11 +70,12 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
70
70
const paymentReference = subscription . id ;
71
71
const coupons = subscription . coupons ;
72
72
const mrr = subscription . mrr || 0 ;
73
- const nextBilling = subscription . next_billing_at && new Date ( subscription . next_billing_at * 1000 ) . toISOString ( ) || '' ;
74
- let lastInvoice = '' ;
73
+ const nextBilling =
74
+ ( subscription . next_billing_at && new Date ( subscription . next_billing_at * 1000 ) . toISOString ( ) ) || "" ;
75
+ let lastInvoice = "" ;
75
76
let lastInvoiceAmount : number = 0 ;
76
77
if ( invoice ) {
77
- lastInvoice = invoice . date && new Date ( invoice . date * 1000 ) . toISOString ( ) || '' ;
78
+ lastInvoice = ( invoice . date && new Date ( invoice . date * 1000 ) . toISOString ( ) ) || "" ;
78
79
lastInvoiceAmount = invoice . total || 0 ;
79
80
}
80
81
@@ -84,7 +85,7 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
84
85
mrr,
85
86
lastInvoice,
86
87
lastInvoiceAmount,
87
- nextBilling
88
+ nextBilling,
88
89
} ;
89
90
await this . db . storeSubscriptionAdditionalData ( data ) ;
90
91
}
@@ -100,18 +101,28 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
100
101
protected async checkAndChargeForUpgrade ( userId : string , chargebeeSubscription : chargebee . Subscription ) {
101
102
const gitpodSubscriptions = await this . db . findSubscriptionForUserByPaymentRef ( userId , chargebeeSubscription . id ) ;
102
103
if ( gitpodSubscriptions . length === 0 ) {
103
- throw new Error ( `Expected existing Gitpod subscription for PaymentRef ${ chargebeeSubscription . id } and user ${ userId } , found none.` ) ;
104
+ throw new Error (
105
+ `Expected existing Gitpod subscription for PaymentRef ${ chargebeeSubscription . id } and user ${ userId } , found none.` ,
106
+ ) ;
104
107
}
105
- const currentGitpodSubscription = gitpodSubscriptions [ 0 ] ; // Ordered by startDate DESC
108
+ const currentGitpodSubscription = gitpodSubscriptions [ 0 ] ; // Ordered by startDate DESC
106
109
const oldPlan = Plans . getById ( currentGitpodSubscription . planId ) ! ;
107
110
const newPlan = Plans . getById ( chargebeeSubscription . plan_id ) ! ;
108
111
109
112
if ( newPlan . pricePerMonth > oldPlan . pricePerMonth ) {
110
113
// Upgrade: Charge for it!
111
- const diffInCents = ( newPlan . pricePerMonth * 100 ) - ( oldPlan . pricePerMonth * 100 ) ;
114
+ const diffInCents = newPlan . pricePerMonth * 100 - oldPlan . pricePerMonth * 100 ;
112
115
const upgradeTimestamp = getUpdatedAt ( chargebeeSubscription ) ;
113
- const description = `Difference on Upgrade from '${ oldPlan . name } ' to '${ newPlan . name } ' (${ formatDate ( upgradeTimestamp ) } )` ;
114
- await this . upgradeHelper . chargeForUpgrade ( userId , chargebeeSubscription . id , diffInCents , description , upgradeTimestamp ) ;
116
+ const description = `Difference on Upgrade from '${ oldPlan . name } ' to '${ newPlan . name } ' (${ formatDate (
117
+ upgradeTimestamp ,
118
+ ) } )`;
119
+ await this . upgradeHelper . chargeForUpgrade (
120
+ userId ,
121
+ chargebeeSubscription . id ,
122
+ diffInCents ,
123
+ description ,
124
+ upgradeTimestamp ,
125
+ ) ;
115
126
}
116
127
}
117
128
@@ -123,14 +134,13 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
123
134
protected async mapToGitpodSubscription ( userId : string , event : chargebee . Event < chargebee . SubscriptionEventV2 > ) {
124
135
await this . db . transaction ( async ( db ) => {
125
136
const subscriptions = await db . findAllSubscriptionsForUser ( userId ) ;
126
- const userPaidSubscriptions = subscriptions . filter ( s => UserPaidSubscription . is ( s ) ) ;
137
+ const userPaidSubscriptions = subscriptions . filter ( ( s ) => UserPaidSubscription . is ( s ) ) ;
127
138
128
- const mapper = this . mapperFactory . newMapper ( ) ;
129
- const delta = mapper . map ( userPaidSubscriptions , event ) . getResult ( ) ;
139
+ const delta = this . mapper . map ( userPaidSubscriptions , event ) . getResult ( ) ;
130
140
131
141
await Promise . all ( [
132
- ...delta . updates . map ( s => db . storeSubscription ( s ) ) ,
133
- ...delta . inserts . map ( s => db . newSubscription ( s ) )
142
+ ...delta . updates . map ( ( s ) => db . storeSubscription ( s ) ) ,
143
+ ...delta . inserts . map ( ( s ) => db . newSubscription ( s ) ) ,
134
144
] ) ;
135
145
} ) ;
136
146
}
@@ -142,4 +152,4 @@ export class SubscriptionHandler implements EventHandler<chargebee.SubscriptionE
142
152
// 3. Apply all events to handleSingleEvent
143
153
// 4. Compare result with the one from the API
144
154
}
145
- }
155
+ }
0 commit comments