Skip to content

Commit 2fe2de9

Browse files
geroplroboquat
authored andcommitted
[server] Format commit
1 parent bd16387 commit 2fe2de9

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

components/ee/payment-endpoint/src/accounting/subscription-service.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { AccountingDB } from "@gitpod/gitpod-db/lib/accounting-db";
88
import { User } from "@gitpod/gitpod-protocol";
99
import { AccountEntry, Subscription } from "@gitpod/gitpod-protocol/lib/accounting-protocol";
1010
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";
1212
import { Plan, Plans } from "@gitpod/gitpod-protocol/lib/plans";
1313
import { orderByStartDateAscEndDateAsc } from "./accounting-util";
1414
import { SubscriptionModel } from "./subscription-model";
1515

16-
export type UserCreated = Pick<User, 'id' | 'creationDate'>;
16+
export type UserCreated = Pick<User, "id" | "creationDate">;
1717

1818
@injectable()
1919
export class SubscriptionService {
@@ -24,12 +24,14 @@ export class SubscriptionService {
2424
* @param user
2525
* @returns All persisted subscriptions + the Free subscriptions that fill up the periods in between, sorted by startDate (ASC)
2626
*/
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[]> {
2832
const subscriptions = await this.accountingDB.findSubscriptionsForUserInPeriod(user.id, startDate, endDate);
2933
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);
3335
}
3436

3537
/**
@@ -40,9 +42,7 @@ export class SubscriptionService {
4042
async getNotYetCancelledSubscriptions(user: UserCreated, date: string): Promise<Subscription[]> {
4143
const subscriptions = await this.accountingDB.findNotYetCancelledSubscriptions(user.id, date);
4244
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);
4646
}
4747

4848
/**
@@ -54,7 +54,7 @@ export class SubscriptionService {
5454
throw new Error("unsubscribe only works for 'free' plans!");
5555
}
5656

57-
return this.accountingDB.transaction(async db => {
57+
return this.accountingDB.transaction(async (db) => {
5858
await this.doUnsubscribe(db, userId, endDate, planId);
5959
});
6060
}
@@ -66,21 +66,28 @@ export class SubscriptionService {
6666
* @param startDate
6767
* @param endDate
6868
*/
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> {
7076
if (!Plans.isFreePlan(plan.chargebeeId)) {
7177
throw new Error("subscribe only works for 'free' plans!");
7278
}
7379

74-
return this.accountingDB.transaction(async db => {
80+
return this.accountingDB.transaction(async (db) => {
7581
await this.doUnsubscribe(db, userId, startDate, plan.chargebeeId);
76-
const newSubscription = <Subscription> {
82+
const newSubscription = <Subscription>{
7783
userId,
7884
amount: Plans.getHoursPerMonth(plan),
7985
planId: plan.chargebeeId,
8086
paymentReference,
8187
startDate,
82-
endDate };
83-
log.info({ userId }, 'Creating subscription', { subscription: newSubscription });
88+
endDate,
89+
};
90+
log.info({ userId }, "Creating subscription", { subscription: newSubscription });
8491
return db.newSubscription(newSubscription);
8592
});
8693
}
@@ -95,7 +102,9 @@ export class SubscriptionService {
95102

96103
// don't override but keep an existing, not-yet cancelled Prof. OSS subscription
97104
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+
);
99108
if (uncancelledOssSub) {
100109
log.debug({ userId: userId }, "already has professional OSS subscription");
101110
return;
@@ -107,10 +116,14 @@ export class SubscriptionService {
107116
}
108117

109118
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",
112125
};
113-
log.info({ userId }, 'Adding credit', { accountEntry: entry });
126+
log.info({ userId }, "Adding credit", { accountEntry: entry });
114127
return this.accountingDB.newAccountEntry(entry);
115128
}
116129

@@ -121,20 +134,23 @@ export class SubscriptionService {
121134
*/
122135
async hasActivePaidSubscription(userId: string, date: Date): Promise<boolean> {
123136
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;
127138
}
128139

129140
async store(db: AccountingDB, model: SubscriptionModel) {
130141
const delta = model.getResult();
131142
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)),
134145
]);
135146
}
136147

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[]> {
138154
const subscriptions = await db.findAllSubscriptionsForUser(userId);
139155
for (let subscription of subscriptions) {
140156
if (planId === subscription.planId) {
@@ -144,7 +160,7 @@ export class SubscriptionService {
144160
} else {
145161
Subscription.cancelSubscription(subscription, subscription.startDate);
146162
}
147-
log.info({ userId }, 'Canceling subscription', { subscription });
163+
log.info({ userId }, "Canceling subscription", { subscription });
148164
await db.storeSubscription(subscription);
149165
}
150166
}

0 commit comments

Comments
 (0)