diff --git a/package.json b/package.json index 063bcd29f..df9652477 100644 --- a/package.json +++ b/package.json @@ -155,6 +155,7 @@ "scroll-into-view": "^1.16.2", "semver": "^7.5.4", "sharp": "^0.32.1", + "stripe": "^14.17.0", "ts-key-enum": "^2.0.12", "tslib": "^2.3.0", "tsup": "^8.0.1", diff --git a/packages/twenty-server/src/core/billing/billing.module.ts b/packages/twenty-server/src/core/billing/billing.module.ts new file mode 100644 index 000000000..cfeae8fb9 --- /dev/null +++ b/packages/twenty-server/src/core/billing/billing.module.ts @@ -0,0 +1,6 @@ +import { Module } from '@nestjs/common'; + +@Module({ + imports: [], +}) +export class BillingModule {} diff --git a/packages/twenty-server/src/core/billing/entities/billing-subscription-item.entity.ts b/packages/twenty-server/src/core/billing/entities/billing-subscription-item.entity.ts new file mode 100644 index 000000000..b2ca09ce8 --- /dev/null +++ b/packages/twenty-server/src/core/billing/entities/billing-subscription-item.entity.ts @@ -0,0 +1,46 @@ +import { + Column, + CreateDateColumn, + Entity, + ManyToOne, + PrimaryGeneratedColumn, + UpdateDateColumn, +} from 'typeorm'; + +import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity'; + +@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.billingSubscriptionItems, + { + onDelete: 'CASCADE', + }, + ) + billingSubscription: BillingSubscription; + + @Column({ nullable: false }) + stripeProductId: string; + + @Column({ nullable: false }) + stripePriceId: string; + + @Column({ nullable: false }) + quantity: number; +} diff --git a/packages/twenty-server/src/core/billing/entities/billing-subscription.entity.ts b/packages/twenty-server/src/core/billing/entities/billing-subscription.entity.ts new file mode 100644 index 000000000..704c23854 --- /dev/null +++ b/packages/twenty-server/src/core/billing/entities/billing-subscription.entity.ts @@ -0,0 +1,53 @@ +import { + Column, + CreateDateColumn, + Entity, + JoinColumn, + OneToMany, + OneToOne, + PrimaryGeneratedColumn, + UpdateDateColumn, +} from 'typeorm'; +import Stripe from 'stripe'; + +import { Workspace } from 'src/core/workspace/workspace.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', + }) + @JoinColumn() + workspace: Workspace; + + @Column({ nullable: false, type: 'uuid' }) + workspaceId: string; + + @Column({ unique: true, nullable: false }) + stripeCustomerId: string; + + @Column({ unique: true, nullable: false }) + stripeSubscriptionId: string; + + @Column({ nullable: false }) + status: Stripe.Subscription.Status; + + @OneToMany( + () => BillingSubscriptionItem, + (billingSubscriptionItem) => billingSubscriptionItem.billingSubscription, + ) + billingSubscriptionItems: BillingSubscriptionItem[]; +} diff --git a/packages/twenty-server/src/core/core.module.ts b/packages/twenty-server/src/core/core.module.ts index cab93d790..9e6959110 100644 --- a/packages/twenty-server/src/core/core.module.ts +++ b/packages/twenty-server/src/core/core.module.ts @@ -8,6 +8,7 @@ import { ApiRestModule } from 'src/core/api-rest/api-rest.module'; import { FeatureFlagModule } from 'src/core/feature-flag/feature-flag.module'; import { OpenApiModule } from 'src/core/open-api/open-api.module'; import { TimelineMessagingModule } from 'src/core/messaging/timeline-messaging.module'; +import { BillingModule } from 'src/core/billing/billing.module'; import { AnalyticsModule } from './analytics/analytics.module'; import { FileModule } from './file/file.module'; @@ -15,25 +16,26 @@ import { ClientConfigModule } from './client-config/client-config.module'; @Module({ imports: [ - AuthModule, - WorkspaceModule, - UserModule, - RefreshTokenModule, AnalyticsModule, - FileModule, - ClientConfigModule, ApiRestModule, - OpenApiModule, + AuthModule, + BillingModule, + ClientConfigModule, FeatureFlagModule, + FileModule, + OpenApiModule, + RefreshTokenModule, TimelineMessagingModule, + UserModule, + WorkspaceModule, ], exports: [ - AuthModule, - WorkspaceModule, - UserModule, AnalyticsModule, + AuthModule, FeatureFlagModule, TimelineMessagingModule, + UserModule, + WorkspaceModule, ], }) export class CoreModule {} diff --git a/packages/twenty-server/src/core/feature-flag/feature-flag.entity.ts b/packages/twenty-server/src/core/feature-flag/feature-flag.entity.ts index ea3528000..bfd042483 100644 --- a/packages/twenty-server/src/core/feature-flag/feature-flag.entity.ts +++ b/packages/twenty-server/src/core/feature-flag/feature-flag.entity.ts @@ -18,6 +18,7 @@ export enum FeatureFlagKeys { IsCalendarEnabled = 'IS_CALENDAR_ENABLED', IsMessagingEnabled = 'IS_MESSAGING_ENABLED', IsNewRecordBoardEnabled = 'IS_NEW_RECORD_BOARD_ENABLED', + IsSelfBillingEnabled = 'IS_SELF_BILLING_ENABLED', IsWorkspaceCleanable = 'IS_WORKSPACE_CLEANABLE', } diff --git a/packages/twenty-server/src/core/workspace/workspace.entity.ts b/packages/twenty-server/src/core/workspace/workspace.entity.ts index 7ae201a9b..2ea41f013 100644 --- a/packages/twenty-server/src/core/workspace/workspace.entity.ts +++ b/packages/twenty-server/src/core/workspace/workspace.entity.ts @@ -6,12 +6,14 @@ import { CreateDateColumn, Entity, OneToMany, + OneToOne, PrimaryGeneratedColumn, UpdateDateColumn, } from 'typeorm'; import { User } from 'src/core/user/user.entity'; import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity'; +import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity'; @Entity({ name: 'workspace', schema: 'core' }) @ObjectType('Workspace') @@ -65,4 +67,10 @@ export class Workspace { @Field() activationStatus: 'active' | 'inactive'; + + @OneToOne( + () => BillingSubscription, + (billingSubscription) => billingSubscription.workspace, + ) + billingSubscription: BillingSubscription; } diff --git a/packages/twenty-server/src/database/typeorm/core/migrations/1708535112230-addBillingCoreTables.ts b/packages/twenty-server/src/database/typeorm/core/migrations/1708535112230-addBillingCoreTables.ts new file mode 100644 index 000000000..c35dd9768 --- /dev/null +++ b/packages/twenty-server/src/database/typeorm/core/migrations/1708535112230-addBillingCoreTables.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddBillingCoreTables1708535112230 implements MigrationInterface { + name = 'AddBillingCoreTables1708535112230' + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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"`); + } + +} diff --git a/packages/twenty-server/src/database/typeorm/typeorm.service.ts b/packages/twenty-server/src/database/typeorm/typeorm.service.ts index 08597e988..837bacef6 100644 --- a/packages/twenty-server/src/database/typeorm/typeorm.service.ts +++ b/packages/twenty-server/src/database/typeorm/typeorm.service.ts @@ -8,6 +8,8 @@ import { User } from 'src/core/user/user.entity'; 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 { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subscription-item.entity'; @Injectable() export class TypeORMService implements OnModuleInit, OnModuleDestroy { @@ -21,7 +23,14 @@ export class TypeORMService implements OnModuleInit, OnModuleDestroy { type: 'postgres', logging: false, schema: 'core', - entities: [User, Workspace, RefreshToken, FeatureFlagEntity], + entities: [ + User, + Workspace, + RefreshToken, + FeatureFlagEntity, + BillingSubscription, + BillingSubscriptionItem, + ], }); } diff --git a/yarn.lock b/yarn.lock index f10fa0a77..11f6746a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15797,6 +15797,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:>=8.1.0": + version: 20.11.19 + resolution: "@types/node@npm:20.11.19" + dependencies: + undici-types: "npm:~5.26.4" + checksum: f451ef0a1d78f29c57bad7b77e49ebec945f2a6d0d7a89851d7e185ee9fe7ad94d651c0dfbcb7858c9fa791310c8b40a881e2260f56bd3c1b7e7ae92723373ae + languageName: node + linkType: hard + "@types/node@npm:^10.1.0": version: 10.17.60 resolution: "@types/node@npm:10.17.60" @@ -42787,6 +42796,16 @@ __metadata: languageName: node linkType: hard +"stripe@npm:^14.17.0": + version: 14.17.0 + resolution: "stripe@npm:14.17.0" + dependencies: + "@types/node": "npm:>=8.1.0" + qs: "npm:^6.11.0" + checksum: ec783c4b125ad6c2f8181d3aa07b7d6a7126a588310ace8d9189269014ce84ba3e98d43464bc557bfcefcc05d7c5aebc551dd4e19c32316165c76944898a719a + languageName: node + linkType: hard + "strnum@npm:^1.0.5": version: 1.0.5 resolution: "strnum@npm:1.0.5" @@ -44394,6 +44413,7 @@ __metadata: storybook: "npm:^7.6.3" storybook-addon-cookie: "npm:^3.2.0" storybook-addon-pseudo-states: "npm:^2.1.2" + stripe: "npm:^14.17.0" supertest: "npm:^6.1.3" ts-jest: "npm:^29.1.1" ts-key-enum: "npm:^2.0.12"