feat: billing

This commit is contained in:
David Nguyen
2025-05-19 12:38:50 +10:00
parent 7abfc9e271
commit 2805478e0d
221 changed files with 8436 additions and 5847 deletions

View File

@ -0,0 +1,69 @@
/*
Warnings:
- You are about to drop the column `customerId` on the `User` table. All the data in the column will be lost.
- You are about to drop the `TeamPending` table. If the table is not empty, all the data it contains will be lost.
- A unique constraint covering the columns `[organisationClaimId]` on the table `Organisation` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[organisationId]` on the table `Subscription` will be added. If there are existing duplicate values, this will fail.
- Added the required column `organisationClaimId` to the `Organisation` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "Subscription" DROP CONSTRAINT "Subscription_organisationId_fkey";
-- DropForeignKey
ALTER TABLE "TeamPending" DROP CONSTRAINT "TeamPending_ownerUserId_fkey";
-- DropIndex
DROP INDEX "User_customerId_key";
-- AlterTable
ALTER TABLE "Organisation" ADD COLUMN "organisationClaimId" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "Subscription" ADD COLUMN "customerId" TEXT;
-- AlterTable
ALTER TABLE "User" DROP COLUMN "customerId";
-- DropTable
DROP TABLE "TeamPending";
-- CreateTable
CREATE TABLE "SubscriptionClaim" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"name" TEXT NOT NULL,
"locked" BOOLEAN NOT NULL DEFAULT false,
"teamCount" INTEGER NOT NULL,
"memberCount" INTEGER NOT NULL,
"flags" JSONB NOT NULL,
CONSTRAINT "SubscriptionClaim_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "OrganisationClaim" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"originalSubscriptionClaimId" TEXT,
"teamCount" INTEGER NOT NULL,
"memberCount" INTEGER NOT NULL,
"flags" JSONB NOT NULL,
CONSTRAINT "OrganisationClaim_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Organisation_organisationClaimId_key" ON "Organisation"("organisationClaimId");
-- CreateIndex
CREATE UNIQUE INDEX "Subscription_organisationId_key" ON "Subscription"("organisationId");
-- AddForeignKey
ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_organisationId_fkey" FOREIGN KEY ("organisationId") REFERENCES "Organisation"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Organisation" ADD CONSTRAINT "Organisation_organisationClaimId_fkey" FOREIGN KEY ("organisationClaimId") REFERENCES "OrganisationClaim"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,9 @@
-- Insert default claims
INSERT INTO "SubscriptionClaim" (id, name, "teamCount", "memberCount", locked, flags, "createdAt", "updatedAt")
VALUES
('free', 'Free', 1, 1, true, '{}', NOW(), NOW()),
('individual', 'Individual', 1, 1, true, '{"unlimitedDocuments": true}', NOW(), NOW()),
('pro', 'Teams', 1, 5, true, '{"memberStripeSync": true, "unlimitedDocuments": true, "embedSigning": true}', NOW(), NOW()),
('platform', 'Platform', 1, 0, true, '{"unlimitedDocuments": true, "embedAuthoring": false, "embedAuthoringWhiteLabel": true, "embedSigning": false, "embedSigningWhiteLabel": true}', NOW(), NOW()),
('enterprise', 'Enterprise', 0, 0, true, '{"unlimitedDocuments": true, "embedAuthoring": true, "embedAuthoringWhiteLabel": true, "embedSigning": true, "embedSigningWhiteLabel": true, "cfr21": true}', NOW(), NOW()),
('early-adopter', 'Early Adopter', 0, 0, true, '{"unlimitedDocuments": true, "embedAuthoring": false, "embedAuthoringWhiteLabel": true, "embedSigning": false, "embedSigningWhiteLabel": true}', NOW(), NOW());

View File

@ -1,3 +1,4 @@
// Todo: orgs remember to add custom migration to apply default claims
generator kysely {
provider = "prisma-kysely"
}
@ -39,7 +40,6 @@ enum Role {
model User {
id Int @id @default(autoincrement())
name String?
customerId String? @unique
email String @unique
emailVerified DateTime?
password String? // Todo: (RR7) Remove after RR7 migration.
@ -60,8 +60,6 @@ model User {
ownedOrganisations Organisation[]
organisationMember OrganisationMember[]
ownedPendingTeams TeamPending[]
twoFactorSecret String?
twoFactorEnabled Boolean @default(false)
twoFactorBackupCodes String?
@ -255,12 +253,44 @@ model Subscription {
updatedAt DateTime @updatedAt
cancelAtPeriodEnd Boolean @default(false)
organisationId String
organisation Organisation @relation(fields: [organisationId], references: [id])
customerId String? // Todo: orgs
organisationId String @unique
organisation Organisation @relation(fields: [organisationId], references: [id], onDelete: Cascade)
@@index([organisationId])
}
/// @zod.import(["import { ZClaimFlagsSchema } from '@documenso/lib/types/subscription';"])
model SubscriptionClaim {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
locked Boolean @default(false)
teamCount Int
memberCount Int
flags Json /// [ClaimFlags] @zod.custom.use(ZClaimFlagsSchema)
}
/// @zod.import(["import { ZClaimFlagsSchema } from '@documenso/lib/types/subscription';"])
model OrganisationClaim {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
originalSubscriptionClaimId String?
organisation Organisation?
teamCount Int
memberCount Int
flags Json /// [ClaimFlags] @zod.custom.use(ZClaimFlagsSchema)
}
model Account {
id String @id @default(cuid())
userId Int
@ -550,8 +580,11 @@ model Organisation {
url String @unique // todo: constrain
avatarImageId String?
customerId String? @unique
subscriptions Subscription[]
customerId String? @unique // Todo: orgs
subscription Subscription?
organisationClaimId String @unique
organisationClaim OrganisationClaim @relation(fields: [organisationClaimId], references: [id], onDelete: Cascade)
members OrganisationMember[]
invites OrganisationMemberInvite[]
@ -683,7 +716,7 @@ model OrganisationGlobalSettings {
brandingLogo String @default("")
brandingUrl String @default("")
brandingCompanyDetails String @default("")
brandingHidePoweredBy Boolean @default(false)
brandingHidePoweredBy Boolean @default(false) // Todo: orgs this doesn't seem to be used?
}
model TeamGlobalSettings {
@ -732,17 +765,6 @@ model Team {
teamGlobalSettings TeamGlobalSettings @relation(fields: [teamGlobalSettingsId], references: [id], onDelete: Cascade)
}
model TeamPending {
id Int @id @default(autoincrement())
name String
url String @unique
createdAt DateTime @default(now())
customerId String @unique
ownerUserId Int
owner User @relation(fields: [ownerUserId], references: [id], onDelete: Cascade)
}
model TeamEmail {
teamId Int @id @unique
createdAt DateTime @default(now())

View File

@ -6,12 +6,15 @@ import type {
import type { TDocumentEmailSettings } from '@documenso/lib/types/document-email';
import type { TDocumentFormValues } from '@documenso/lib/types/document-form-values';
import type { TFieldMetaNotOptionalSchema } from '@documenso/lib/types/field-meta';
import type { TClaimFlags } from '@documenso/lib/types/subscription';
/**
* Global types for Prisma.Json instances.
*/
declare global {
namespace PrismaJson {
type ClaimFlags = TClaimFlags;
type DocumentFormValues = TDocumentFormValues;
type DocumentAuthOptions = TDocumentAuthOptions;
type DocumentEmailSettings = TDocumentEmailSettings;