|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the Gitpod Enterprise Source Code License, |
| 4 | + * See License.enterprise.txt in the project root folder. |
| 5 | + */ |
| 6 | + |
| 7 | +import { injectable, inject } from "inversify"; |
| 8 | +import { EntityManager, Repository } from "typeorm"; |
| 9 | + |
| 10 | +import { TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol"; |
| 11 | + |
| 12 | +import { TeamSubscription2DB } from "../team-subscription-2-db"; |
| 13 | +import { DBTeamSubscription2 } from "./entity/db-team-subscription-2"; |
| 14 | +import { TypeORM } from "./typeorm"; |
| 15 | + |
| 16 | +@injectable() |
| 17 | +export class TeamSubscription2DBImpl implements TeamSubscription2DB { |
| 18 | + @inject(TypeORM) protected readonly typeORM: TypeORM; |
| 19 | + |
| 20 | + async transaction<T>(code: (db: TeamSubscription2DB) => Promise<T>): Promise<T> { |
| 21 | + const manager = await this.getEntityManager(); |
| 22 | + return await manager.transaction(async (manager) => { |
| 23 | + return await code(new TransactionalTeamSubscription2DBImpl(manager)); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + protected async getEntityManager() { |
| 28 | + return (await this.typeORM.getConnection()).manager; |
| 29 | + } |
| 30 | + |
| 31 | + protected async getRepo(): Promise<Repository<DBTeamSubscription2>> { |
| 32 | + return (await this.getEntityManager()).getRepository(DBTeamSubscription2); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Team Subscriptions 2 |
| 37 | + */ |
| 38 | + |
| 39 | + async storeEntry(ts: TeamSubscription2): Promise<void> { |
| 40 | + const repo = await this.getRepo(); |
| 41 | + await repo.save(ts); |
| 42 | + } |
| 43 | + |
| 44 | + async findById(id: string): Promise<TeamSubscription2 | undefined> { |
| 45 | + const repo = await this.getRepo(); |
| 46 | + return repo.findOne(id); |
| 47 | + } |
| 48 | + |
| 49 | + async findByPaymentRef(teamId: string, paymentReference: string): Promise<TeamSubscription2 | undefined> { |
| 50 | + const repo = await this.getRepo(); |
| 51 | + return repo.findOne({ teamId, paymentReference }); |
| 52 | + } |
| 53 | + |
| 54 | + async findForTeam(teamId: string, date: string): Promise<TeamSubscription2 | undefined> { |
| 55 | + const repo = await this.getRepo(); |
| 56 | + const query = repo |
| 57 | + .createQueryBuilder("ts2") |
| 58 | + .where("ts2.teamId = :teamId", { teamId }) |
| 59 | + .andWhere("ts2.startDate <= :date", { date }) |
| 60 | + .andWhere('ts2.endDate = "" OR ts2.endDate > :date', { date }); |
| 61 | + return query.getOne(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export class TransactionalTeamSubscription2DBImpl extends TeamSubscription2DBImpl { |
| 66 | + constructor(protected readonly manager: EntityManager) { |
| 67 | + super(); |
| 68 | + } |
| 69 | + |
| 70 | + async getEntityManager(): Promise<EntityManager> { |
| 71 | + return this.manager; |
| 72 | + } |
| 73 | +} |
0 commit comments