-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): support team workspace subscription
- Loading branch information
Showing
24 changed files
with
2,117 additions
and
760 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/backend/server/migrations/20241202070358_universal_subscription/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-- DropForeignKey | ||
ALTER TABLE "user_invoices" DROP CONSTRAINT "user_invoices_user_id_fkey"; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE "user_subscriptions" DROP CONSTRAINT "user_subscriptions_user_id_fkey"; | ||
|
||
-- CreateTable | ||
CREATE TABLE "subscriptions" ( | ||
"id" SERIAL NOT NULL, | ||
"target_id" VARCHAR NOT NULL, | ||
"plan" VARCHAR(20) NOT NULL, | ||
"recurring" VARCHAR(20) NOT NULL, | ||
"variant" VARCHAR(20), | ||
"quantity" INTEGER NOT NULL DEFAULT 1, | ||
"stripe_subscription_id" TEXT, | ||
"stripe_schedule_id" VARCHAR, | ||
"status" VARCHAR(20) NOT NULL, | ||
"start" TIMESTAMPTZ(3) NOT NULL, | ||
"end" TIMESTAMPTZ(3), | ||
"next_bill_at" TIMESTAMPTZ(3), | ||
"canceled_at" TIMESTAMPTZ(3), | ||
"trial_start" TIMESTAMPTZ(3), | ||
"trial_end" TIMESTAMPTZ(3), | ||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMPTZ(3) NOT NULL, | ||
|
||
CONSTRAINT "subscriptions_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "invoices" ( | ||
"stripe_invoice_id" TEXT NOT NULL, | ||
"target_id" VARCHAR NOT NULL, | ||
"currency" VARCHAR(3) NOT NULL, | ||
"amount" INTEGER NOT NULL, | ||
"status" VARCHAR(20) NOT NULL, | ||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMPTZ(3) NOT NULL, | ||
"reason" VARCHAR, | ||
"last_payment_error" TEXT, | ||
"link" TEXT, | ||
|
||
CONSTRAINT "invoices_pkey" PRIMARY KEY ("stripe_invoice_id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "subscriptions_stripe_subscription_id_key" ON "subscriptions"("stripe_subscription_id"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "subscriptions_target_id_plan_key" ON "subscriptions"("target_id", "plan"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "invoices_target_id_idx" ON "invoices"("target_id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/backend/server/src/data/migrations/1733125339942-universal-subscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
import { loop } from './utils/loop'; | ||
|
||
export class UniversalSubscription1733125339942 { | ||
// do the migration | ||
static async up(db: PrismaClient) { | ||
await loop(async (offset, take) => { | ||
const oldSubscriptions = await db.deprecatedUserSubscription.findMany({ | ||
skip: offset, | ||
take, | ||
}); | ||
|
||
await db.subscription.createMany({ | ||
data: oldSubscriptions.map(s => ({ | ||
targetId: s.userId, | ||
...s, | ||
})), | ||
}); | ||
|
||
return oldSubscriptions.length; | ||
}, 50); | ||
} | ||
|
||
// revert the migration | ||
static async down(_db: PrismaClient) { | ||
// noop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,5 @@ export class ConfigModule { | |
}; | ||
}; | ||
} | ||
|
||
export { Runtime }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.