@@ -8,12 +8,12 @@ import { AccountingDB } from "@gitpod/gitpod-db/lib/accounting-db";
8
8
import { User } from "@gitpod/gitpod-protocol" ;
9
9
import { AccountEntry , Subscription } from "@gitpod/gitpod-protocol/lib/accounting-protocol" ;
10
10
import { inject , injectable } from "inversify" ;
11
- import { log } from ' @gitpod/gitpod-protocol/lib/util/logging' ;
11
+ import { log } from " @gitpod/gitpod-protocol/lib/util/logging" ;
12
12
import { Plan , Plans } from "@gitpod/gitpod-protocol/lib/plans" ;
13
13
import { orderByStartDateAscEndDateAsc } from "./accounting-util" ;
14
14
import { SubscriptionModel } from "./subscription-model" ;
15
15
16
- export type UserCreated = Pick < User , 'id' | ' creationDate' > ;
16
+ export type UserCreated = Pick < User , "id" | " creationDate" > ;
17
17
18
18
@injectable ( )
19
19
export class SubscriptionService {
@@ -24,12 +24,14 @@ export class SubscriptionService {
24
24
* @param user
25
25
* @returns All persisted subscriptions + the Free subscriptions that fill up the periods in between, sorted by startDate (ASC)
26
26
*/
27
- async getSubscriptionHistoryForUserInPeriod ( user : UserCreated , startDate : string , endDate : string ) : Promise < Subscription [ ] > {
27
+ async getSubscriptionHistoryForUserInPeriod (
28
+ user : UserCreated ,
29
+ startDate : string ,
30
+ endDate : string ,
31
+ ) : Promise < Subscription [ ] > {
28
32
const subscriptions = await this . accountingDB . findSubscriptionsForUserInPeriod ( user . id , startDate , endDate ) ;
29
33
const model = new SubscriptionModel ( user . id , subscriptions ) ;
30
- return model
31
- . mergedWithFreeSubscriptions ( user . creationDate )
32
- . sort ( orderByStartDateAscEndDateAsc ) ;
34
+ return model . mergedWithFreeSubscriptions ( user . creationDate ) . sort ( orderByStartDateAscEndDateAsc ) ;
33
35
}
34
36
35
37
/**
@@ -40,9 +42,7 @@ export class SubscriptionService {
40
42
async getNotYetCancelledSubscriptions ( user : UserCreated , date : string ) : Promise < Subscription [ ] > {
41
43
const subscriptions = await this . accountingDB . findNotYetCancelledSubscriptions ( user . id , date ) ;
42
44
const model = new SubscriptionModel ( user . id , subscriptions ) ;
43
- return model
44
- . mergedWithFreeSubscriptions ( user . creationDate )
45
- . sort ( orderByStartDateAscEndDateAsc ) ;
45
+ return model . mergedWithFreeSubscriptions ( user . creationDate ) . sort ( orderByStartDateAscEndDateAsc ) ;
46
46
}
47
47
48
48
/**
@@ -54,7 +54,7 @@ export class SubscriptionService {
54
54
throw new Error ( "unsubscribe only works for 'free' plans!" ) ;
55
55
}
56
56
57
- return this . accountingDB . transaction ( async db => {
57
+ return this . accountingDB . transaction ( async ( db ) => {
58
58
await this . doUnsubscribe ( db , userId , endDate , planId ) ;
59
59
} ) ;
60
60
}
@@ -66,21 +66,28 @@ export class SubscriptionService {
66
66
* @param startDate
67
67
* @param endDate
68
68
*/
69
- async subscribe ( userId : string , plan : Plan , paymentReference : string | undefined , startDate : string , endDate ?: string ) : Promise < Subscription > {
69
+ async subscribe (
70
+ userId : string ,
71
+ plan : Plan ,
72
+ paymentReference : string | undefined ,
73
+ startDate : string ,
74
+ endDate ?: string ,
75
+ ) : Promise < Subscription > {
70
76
if ( ! Plans . isFreePlan ( plan . chargebeeId ) ) {
71
77
throw new Error ( "subscribe only works for 'free' plans!" ) ;
72
78
}
73
79
74
- return this . accountingDB . transaction ( async db => {
80
+ return this . accountingDB . transaction ( async ( db ) => {
75
81
await this . doUnsubscribe ( db , userId , startDate , plan . chargebeeId ) ;
76
- const newSubscription = < Subscription > {
82
+ const newSubscription = < Subscription > {
77
83
userId,
78
84
amount : Plans . getHoursPerMonth ( plan ) ,
79
85
planId : plan . chargebeeId ,
80
86
paymentReference,
81
87
startDate,
82
- endDate } ;
83
- log . info ( { userId } , 'Creating subscription' , { subscription : newSubscription } ) ;
88
+ endDate,
89
+ } ;
90
+ log . info ( { userId } , "Creating subscription" , { subscription : newSubscription } ) ;
84
91
return db . newSubscription ( newSubscription ) ;
85
92
} ) ;
86
93
}
@@ -95,7 +102,9 @@ export class SubscriptionService {
95
102
96
103
// don't override but keep an existing, not-yet cancelled Prof. OSS subscription
97
104
const subs = await this . getNotYetCancelledSubscriptions ( user , now . toISOString ( ) ) ;
98
- const uncancelledOssSub = subs . find ( s => s . planId === Plans . FREE_OPEN_SOURCE . chargebeeId && ! s . cancellationDate ) ;
105
+ const uncancelledOssSub = subs . find (
106
+ ( s ) => s . planId === Plans . FREE_OPEN_SOURCE . chargebeeId && ! s . cancellationDate ,
107
+ ) ;
99
108
if ( uncancelledOssSub ) {
100
109
log . debug ( { userId : userId } , "already has professional OSS subscription" ) ;
101
110
return ;
@@ -107,10 +116,14 @@ export class SubscriptionService {
107
116
}
108
117
109
118
async addCredit ( userId : string , amount : number , date : string , expiryDate ?: string ) : Promise < AccountEntry > {
110
- const entry = < AccountEntry > {
111
- userId, amount, date, expiryDate, kind : 'credit'
119
+ const entry = < AccountEntry > {
120
+ userId,
121
+ amount,
122
+ date,
123
+ expiryDate,
124
+ kind : "credit" ,
112
125
} ;
113
- log . info ( { userId } , ' Adding credit' , { accountEntry : entry } ) ;
126
+ log . info ( { userId } , " Adding credit" , { accountEntry : entry } ) ;
114
127
return this . accountingDB . newAccountEntry ( entry ) ;
115
128
}
116
129
@@ -121,20 +134,23 @@ export class SubscriptionService {
121
134
*/
122
135
async hasActivePaidSubscription ( userId : string , date : Date ) : Promise < boolean > {
123
136
const subscriptions = await this . accountingDB . findActiveSubscriptionsForUser ( userId , date . toISOString ( ) ) ;
124
- return subscriptions
125
- . filter ( s => Subscription . isActive ( s , date . toISOString ( ) ) )
126
- . length > 0 ;
137
+ return subscriptions . filter ( ( s ) => Subscription . isActive ( s , date . toISOString ( ) ) ) . length > 0 ;
127
138
}
128
139
129
140
async store ( db : AccountingDB , model : SubscriptionModel ) {
130
141
const delta = model . getResult ( ) ;
131
142
await Promise . all ( [
132
- ...delta . updates . map ( s => db . storeSubscription ( s ) ) ,
133
- ...delta . inserts . map ( s => db . newSubscription ( s ) )
143
+ ...delta . updates . map ( ( s ) => db . storeSubscription ( s ) ) ,
144
+ ...delta . inserts . map ( ( s ) => db . newSubscription ( s ) ) ,
134
145
] ) ;
135
146
}
136
147
137
- private async doUnsubscribe ( db : AccountingDB , userId : string , endDate : string , planId : string ) : Promise < Subscription [ ] > {
148
+ private async doUnsubscribe (
149
+ db : AccountingDB ,
150
+ userId : string ,
151
+ endDate : string ,
152
+ planId : string ,
153
+ ) : Promise < Subscription [ ] > {
138
154
const subscriptions = await db . findAllSubscriptionsForUser ( userId ) ;
139
155
for ( let subscription of subscriptions ) {
140
156
if ( planId === subscription . planId ) {
@@ -144,7 +160,7 @@ export class SubscriptionService {
144
160
} else {
145
161
Subscription . cancelSubscription ( subscription , subscription . startDate ) ;
146
162
}
147
- log . info ( { userId } , ' Canceling subscription' , { subscription } ) ;
163
+ log . info ( { userId } , " Canceling subscription" , { subscription } ) ;
148
164
await db . storeSubscription ( subscription ) ;
149
165
}
150
166
}
0 commit comments