Skip to content

Commit

Permalink
Rename subscriptionProduct table
Browse files Browse the repository at this point in the history
  • Loading branch information
martmull committed Feb 21, 2024
1 parent 2177ba9 commit 26ac7f1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import {
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';

import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity';

@Entity({ name: 'billingProduct', schema: 'core' })
export class BillingProduct {
@Entity({ name: 'billingSubscriptionItem', schema: 'core' })
export class BillingSubscriptionItem {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ nullable: true })
deletedAt?: Date;

@CreateDateColumn({ type: 'timestamp with time zone' })
createdAt: Date;

@UpdateDateColumn({ type: 'timestamp with time zone' })
updatedAt: Date;

@Column({ nullable: false })
billingSubscriptionId: string;

@ManyToOne(
() => BillingSubscription,
(billingSubscription) => billingSubscription.billingProducts,
(billingSubscription) => billingSubscription.billingSubscriptionItems,
{
onDelete: 'CASCADE',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
OneToMany,
OneToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import Stripe from 'stripe';

import { Workspace } from 'src/core/workspace/workspace.entity';
import { BillingProduct } from 'src/core/billing/entities/billing-product.entity';
import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subscription-item.entity';

@Entity({ name: 'billingSubscription', schema: 'core' })
export class BillingSubscription {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ nullable: true })
deletedAt?: Date;

@CreateDateColumn({ type: 'timestamp with time zone' })
createdAt: Date;

@UpdateDateColumn({ type: 'timestamp with time zone' })
updatedAt: Date;

@OneToOne(() => Workspace, (workspace) => workspace.billingSubscription, {
onDelete: 'CASCADE',
})
Expand All @@ -35,8 +46,8 @@ export class BillingSubscription {
status: Stripe.Subscription.Status;

@OneToMany(
() => BillingProduct,
(billingProduct) => billingProduct.billingSubscription,
() => BillingSubscriptionItem,
(billingSubscriptionItem) => billingSubscriptionItem.billingSubscription,
)
billingProducts: BillingProduct[];
billingSubscriptionItems: BillingSubscriptionItem[];
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddBillingCoreTables1708535112230 implements MigrationInterface {
name = 'AddBillingCoreTables1708535112230'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "core"."billingSubscriptionItem" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "deletedAt" TIMESTAMP, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "billingSubscriptionId" uuid NOT NULL, "stripeProductId" character varying NOT NULL, "stripePriceId" character varying NOT NULL, "quantity" integer NOT NULL, CONSTRAINT "PK_0287b2d9fca488edcbf748281fc" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "core"."billingSubscription" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "deletedAt" TIMESTAMP, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "workspaceId" uuid NOT NULL, "stripeCustomerId" character varying NOT NULL, "stripeSubscriptionId" character varying NOT NULL, "status" character varying NOT NULL, CONSTRAINT "UQ_9120b7586c3471463480b58d20a" UNIQUE ("stripeCustomerId"), CONSTRAINT "UQ_1a858c28c7766d429cbd25f05e8" UNIQUE ("stripeSubscriptionId"), CONSTRAINT "REL_4abfb70314c18da69e1bee1954" UNIQUE ("workspaceId"), CONSTRAINT "PK_6e9c72c32d91640b8087cb53666" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "core"."billingSubscriptionItem" ADD CONSTRAINT "FK_a602e7c9da619b8290232f6eeab" FOREIGN KEY ("billingSubscriptionId") REFERENCES "core"."billingSubscription"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "core"."billingSubscription" ADD CONSTRAINT "FK_4abfb70314c18da69e1bee1954d" FOREIGN KEY ("workspaceId") REFERENCES "core"."workspace"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "core"."billingSubscription" DROP CONSTRAINT "FK_4abfb70314c18da69e1bee1954d"`);
await queryRunner.query(`ALTER TABLE "core"."billingSubscriptionItem" DROP CONSTRAINT "FK_a602e7c9da619b8290232f6eeab"`);
await queryRunner.query(`DROP TABLE "core"."billingSubscription"`);
await queryRunner.query(`DROP TABLE "core"."billingSubscriptionItem"`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Workspace } from 'src/core/workspace/workspace.entity';
import { RefreshToken } from 'src/core/refresh-token/refresh-token.entity';
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity';
import { BillingProduct } from 'src/core/billing/entities/billing-product.entity';
import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subscription-item.entity';

@Injectable()
export class TypeORMService implements OnModuleInit, OnModuleDestroy {
Expand All @@ -29,7 +29,7 @@ export class TypeORMService implements OnModuleInit, OnModuleDestroy {
RefreshToken,
FeatureFlagEntity,
BillingSubscription,
BillingProduct,
BillingSubscriptionItem,
],
});
}
Expand Down

0 comments on commit 26ac7f1

Please sign in to comment.