Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db][payment][server] Implement TeamSubscription2.excludeFromMoreResources #10370

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
quantity: chargebeeSubscription.plan_quantity,
startDate: getStartDate(chargebeeSubscription),
endDate: chargebeeSubscription.cancelled_at ? getCancelledAt(chargebeeSubscription) : undefined,
excludeFromMoreResources: true,
});
await db2.storeEntry(ts2);
await this.service2.addAllTeamMemberSubscriptions(ts2);
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-db/src/team-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TeamDB {
searchTerm: string,
): Promise<{ total: number; rows: Team[] }>;
findTeamById(teamId: string): Promise<Team | undefined>;
findTeamByMembershipId(membershipId: string): Promise<Team | undefined>;
findMembersByTeam(teamId: string): Promise<TeamMemberInfo[]>;
findTeamMembership(userId: string, teamId: string): Promise<DBTeamMembership | undefined>;
findTeamsByUser(userId: string): Promise<Team[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class DBTeamSubscription2 implements TeamSubscription2 {
})
cancellationDate?: string;

@Column({
default: true,
})
excludeFromMoreResources: boolean;

// This column triggers the db-sync deletion mechanism. It's not intended for public consumption.
@Column()
deleted: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";
import { columnExists } from "./helper/helper";

const TABLE_NAME = "d_b_team_subscription2";
const COLUMN_NAME = "excludeFromMoreResources";

export class TS2MoreResources1653983151212 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME} ADD COLUMN ${COLUMN_NAME} tinyint(4) NOT NULL DEFAULT '1', ALGORITHM=INPLACE, LOCK=NONE `,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
9 changes: 9 additions & 0 deletions components/gitpod-db/src/typeorm/team-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ export class TeamDBImpl implements TeamDB {
return teamRepo.findOne({ id: teamId, deleted: false, markedDeleted: false });
}

public async findTeamByMembershipId(membershipId: string): Promise<Team | undefined> {
const membershipRepo = await this.getMembershipRepo();
const membership = await membershipRepo.findOne({ id: membershipId, deleted: false });
if (!membership) {
return;
}
return this.findTeamById(membership.teamId);
}

public async findMembersByTeam(teamId: string): Promise<TeamMemberInfo[]> {
const membershipRepo = await this.getMembershipRepo();
const userRepo = await this.getUserRepo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface TeamSubscription2 {
/** The Chargebee subscription id */
paymentReference: string;
cancellationDate?: string;
excludeFromMoreResources: boolean;
}

export namespace TeamSubscription2 {
Expand Down
15 changes: 14 additions & 1 deletion components/server/ee/src/user/eligibility-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { inject, injectable } from "inversify";
import { TeamSubscriptionDB, UserDB } from "@gitpod/gitpod-db/lib";
import { TeamDB, TeamSubscription2DB, TeamSubscriptionDB, UserDB } from "@gitpod/gitpod-db/lib";
import { TokenProvider } from "../../../src/user/token-provider";
import {
User,
Expand Down Expand Up @@ -50,11 +50,13 @@ export interface GitHubEducationPack {
export class EligibilityService {
@inject(Config) protected readonly config: Config;
@inject(UserDB) protected readonly userDb: UserDB;
@inject(TeamDB) protected readonly teamDb: TeamDB;
@inject(SubscriptionService) protected readonly subscriptionService: SubscriptionService;
@inject(EMailDomainService) protected readonly domainService: EMailDomainService;
@inject(TokenProvider) protected readonly tokenProvider: TokenProvider;
@inject(AccountStatementProvider) protected readonly accountStatementProvider: AccountStatementProvider;
@inject(TeamSubscriptionDB) protected readonly teamSubscriptionDb: TeamSubscriptionDB;
@inject(TeamSubscription2DB) protected readonly teamSubscription2Db: TeamSubscription2DB;

/**
* Whether the given user is recognized as a student within Gitpod
Expand Down Expand Up @@ -303,6 +305,17 @@ export class EligibilityService {
// some TeamSubscriptions are marked with 'excludeFromMoreResources' to convey that those are _not_ receiving more resources
const excludeFromMoreResources = await Promise.all(
relevantSubscriptions.map(async (s): Promise<boolean> => {
if (s.teamMembershipId) {
const team = await this.teamDb.findTeamByMembershipId(s.teamMembershipId);
if (!team) {
return true;
}
const ts2 = await this.teamSubscription2Db.findForTeam(team.id, new Date().toISOString());
if (!ts2) {
return true;
}
return ts2.excludeFromMoreResources;
}
if (!s.teamSubscriptionSlotId) {
return false;
}
Expand Down