Skip to content

Commit 10a4efd

Browse files
committed
[db][payment][server] Implement TeamSubscription2.excludeFromMoreResources
1 parent 23c7726 commit 10a4efd

File tree

7 files changed

+54
-1
lines changed

7 files changed

+54
-1
lines changed

components/ee/payment-endpoint/src/chargebee/team-subscription-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
128128
quantity: chargebeeSubscription.plan_quantity,
129129
startDate: getStartDate(chargebeeSubscription),
130130
endDate: chargebeeSubscription.cancelled_at ? getCancelledAt(chargebeeSubscription) : undefined,
131+
excludeFromMoreResources: true,
131132
});
132133
await db2.storeEntry(ts2);
133134
await this.service2.addAllTeamMemberSubscriptions(ts2);

components/gitpod-db/src/team-db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface TeamDB {
1717
searchTerm: string,
1818
): Promise<{ total: number; rows: Team[] }>;
1919
findTeamById(teamId: string): Promise<Team | undefined>;
20+
findTeamByMembershipId(membershipId: string): Promise<Team | undefined>;
2021
findMembersByTeam(teamId: string): Promise<TeamMemberInfo[]>;
2122
findTeamMembership(userId: string, teamId: string): Promise<DBTeamMembership | undefined>;
2223
findTeamsByUser(userId: string): Promise<Team[]>;

components/gitpod-db/src/typeorm/entity/db-team-subscription-2.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class DBTeamSubscription2 implements TeamSubscription2 {
4646
})
4747
cancellationDate?: string;
4848

49+
@Column({
50+
default: true,
51+
})
52+
excludeFromMoreResources: boolean;
53+
4954
// This column triggers the db-sync deletion mechanism. It's not intended for public consumption.
5055
@Column()
5156
deleted: boolean;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
import { columnExists } from "./helper/helper";
9+
10+
const TABLE_NAME = "d_b_team_subscription2";
11+
const COLUMN_NAME = "excludeFromMoreResources";
12+
13+
export class TS2MoreResources1653983151212 implements MigrationInterface {
14+
public async up(queryRunner: QueryRunner): Promise<void> {
15+
if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) {
16+
await queryRunner.query(
17+
`ALTER TABLE ${TABLE_NAME} ADD COLUMN ${COLUMN_NAME} tinyint(4) NOT NULL DEFAULT '1', ALGORITHM=INPLACE, LOCK=NONE `,
18+
);
19+
}
20+
}
21+
22+
public async down(queryRunner: QueryRunner): Promise<void> {}
23+
}

components/gitpod-db/src/typeorm/team-db-impl.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ export class TeamDBImpl implements TeamDB {
6464
return teamRepo.findOne({ id: teamId, deleted: false, markedDeleted: false });
6565
}
6666

67+
public async findTeamByMembershipId(membershipId: string): Promise<Team | undefined> {
68+
const membershipRepo = await this.getMembershipRepo();
69+
const membership = await membershipRepo.findOne({ id: membershipId, deleted: false });
70+
if (!membership) {
71+
return;
72+
}
73+
return this.findTeamById(membership.teamId);
74+
}
75+
6776
public async findMembersByTeam(teamId: string): Promise<TeamMemberInfo[]> {
6877
const membershipRepo = await this.getMembershipRepo();
6978
const userRepo = await this.getUserRepo();

components/gitpod-protocol/src/team-subscription-protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface TeamSubscription2 {
4343
/** The Chargebee subscription id */
4444
paymentReference: string;
4545
cancellationDate?: string;
46+
excludeFromMoreResources: boolean;
4647
}
4748

4849
export namespace TeamSubscription2 {

components/server/ee/src/user/eligibility-service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { inject, injectable } from "inversify";
8-
import { TeamSubscriptionDB, UserDB } from "@gitpod/gitpod-db/lib";
8+
import { TeamDB, TeamSubscription2DB, TeamSubscriptionDB, UserDB } from "@gitpod/gitpod-db/lib";
99
import { TokenProvider } from "../../../src/user/token-provider";
1010
import {
1111
User,
@@ -50,11 +50,13 @@ export interface GitHubEducationPack {
5050
export class EligibilityService {
5151
@inject(Config) protected readonly config: Config;
5252
@inject(UserDB) protected readonly userDb: UserDB;
53+
@inject(TeamDB) protected readonly teamDb: TeamDB;
5354
@inject(SubscriptionService) protected readonly subscriptionService: SubscriptionService;
5455
@inject(EMailDomainService) protected readonly domainService: EMailDomainService;
5556
@inject(TokenProvider) protected readonly tokenProvider: TokenProvider;
5657
@inject(AccountStatementProvider) protected readonly accountStatementProvider: AccountStatementProvider;
5758
@inject(TeamSubscriptionDB) protected readonly teamSubscriptionDb: TeamSubscriptionDB;
59+
@inject(TeamSubscription2DB) protected readonly teamSubscription2Db: TeamSubscription2DB;
5860

5961
/**
6062
* Whether the given user is recognized as a student within Gitpod
@@ -303,6 +305,17 @@ export class EligibilityService {
303305
// some TeamSubscriptions are marked with 'excludeFromMoreResources' to convey that those are _not_ receiving more resources
304306
const excludeFromMoreResources = await Promise.all(
305307
relevantSubscriptions.map(async (s): Promise<boolean> => {
308+
if (s.teamMembershipId) {
309+
const team = await this.teamDb.findTeamByMembershipId(s.teamMembershipId);
310+
if (!team) {
311+
return true;
312+
}
313+
const ts2 = await this.teamSubscription2Db.findForTeam(team.id, new Date().toISOString());
314+
if (!ts2) {
315+
return true;
316+
}
317+
return ts2.excludeFromMoreResources;
318+
}
306319
if (!s.teamSubscriptionSlotId) {
307320
return false;
308321
}

0 commit comments

Comments
 (0)