Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Move space tier to spaces and enforce subscription link (#1999)

Luke Vellad35429b42bcb
diff --git a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts b/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
index 8e5e7d185..e78ba90dd 100644
--- a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
+++ b/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
@@ -13,7 +13,13 @@ export const deleteCurrentUserAction = authActionClient
const user = await prisma.user.findUnique({
where: { id: userId },
- include: { spaces: { include: { subscription: true } } },
+ include: {
+ spaces: {
+ select: {
+ tier: true,
+ },
+ },
+ },
});
if (!user) {
diff --git a/apps/web/src/app/api/house-keeping/[...method]/route.ts b/apps/web/src/app/api/house-keeping/[...method]/route.ts
index 9fffa5290..3e9af4924 100644
--- a/apps/web/src/app/api/house-keeping/[...method]/route.ts
+++ b/apps/web/src/app/api/house-keeping/[...method]/route.ts
@@ -45,13 +45,8 @@ app.get("/delete-inactive-polls", async (c) => {
{ spaceId: null },
{
space: {
- subscription: null,
- },
- },
- {
- space: {
- subscription: {
- active: false,
+ tier: {
+ not: "pro",
},
},
},
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
index c67bc45fa..e2ccc55c4 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
@@ -16,6 +16,7 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
);
const isActive = isSubscriptionActive(subscription);
+ const tier = isActive ? "pro" : "hobby";
const { priceId, currency, interval, amount } =
getSubscriptionDetails(subscription);
@@ -25,8 +26,7 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
throw new Error("Invalid subscription metadata");
}
- const userId = res.data.userId;
- const spaceId = res.data.spaceId;
+ const { userId, spaceId } = res.data;
const subscriptionItem = subscription.items.data[0];
@@ -38,84 +38,79 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
const subscriptionItemId = subscriptionItem.id;
const quantity = subscriptionItem.quantity ?? 1;
- // If user already has a subscription, update it or replace it
- // Update the existing subscription with new data
- const updatedSubscription = await prisma.subscription.upsert({
- where: { id: subscription.id },
- create: {
- id: subscription.id,
- userId,
- active: isActive,
- quantity,
- subscriptionItemId,
- priceId,
- currency,
- interval,
- amount,
- status: subscription.status,
- createdAt: toDate(subscription.created),
- periodStart: toDate(subscription.current_period_start),
- periodEnd: toDate(subscription.current_period_end),
- cancelAtPeriodEnd: subscription.cancel_at_period_end,
- spaceId,
- },
- update: {
- id: subscription.id,
- userId,
- active: isActive,
- subscriptionItemId,
- quantity,
- priceId,
- currency,
- interval,
- amount,
- status: subscription.status,
- createdAt: toDate(subscription.created),
- periodStart: toDate(subscription.current_period_start),
- periodEnd: toDate(subscription.current_period_end),
- cancelAtPeriodEnd: subscription.cancel_at_period_end,
- spaceId,
- },
- include: {
- space: true,
- },
- });
- if (updatedSubscription.space) {
- const space = await prisma.space.findUniqueOrThrow({
- where: {
- id: spaceId,
+ await prisma.$transaction(async (tx) => {
+ await tx.subscription.upsert({
+ where: { id: subscription.id },
+ create: {
+ id: subscription.id,
+ userId,
+ active: isActive,
+ quantity,
+ subscriptionItemId,
+ priceId,
+ currency,
+ interval,
+ amount,
+ status: subscription.status,
+ createdAt: toDate(subscription.created),
+ periodStart: toDate(subscription.current_period_start),
+ periodEnd: toDate(subscription.current_period_end),
+ cancelAtPeriodEnd: subscription.cancel_at_period_end,
+ spaceId,
},
- });
-
- updateSpaceGroup({
- spaceId,
- properties: {
- name: space?.name,
- seatCount: quantity,
- tier: isActive ? "pro" : "hobby",
+ update: {
+ id: subscription.id,
+ userId,
+ active: isActive,
+ subscriptionItemId,
+ quantity,
+ priceId,
+ currency,
+ interval,
+ amount,
+ status: subscription.status,
+ createdAt: toDate(subscription.created),
+ periodStart: toDate(subscription.current_period_start),
+ periodEnd: toDate(subscription.current_period_end),
+ cancelAtPeriodEnd: subscription.cancel_at_period_end,
+ spaceId,
+ },
+ include: {
+ space: true,
},
});
- posthog?.capture({
- distinctId: userId,
- uuid: event.id,
- event: "upgrade",
- properties: {
- interval,
- $set: {
- tier: isActive ? "pro" : "hobby",
- },
+ await tx.space.update({
+ where: {
+ id: spaceId,
},
- groups: {
- space: spaceId,
+ data: {
+ tier,
},
});
- } else {
- console.error("Failed to update space group on subscription.created", {
- eventId: event.id,
- subscriptionId: subscription.id,
- spaceId: spaceId,
- });
- }
+ });
+
+ updateSpaceGroup({
+ spaceId,
+ properties: {
+ seatCount: quantity,
+ tier,
+ },
+ });
+
+ posthog?.capture({
+ distinctId: userId,
+ uuid: event.id,
+ event: "upgrade",
+ properties: {
+ interval,
+ $set: {
+ tier,
+ },
+ },
+ groups: {
+ space: spaceId,
+ },
+ });
}
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
index ff53dd131..da626802a 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/deleted.ts
@@ -2,11 +2,8 @@ import type { Stripe } from "@rallly/billing";
import { stripe } from "@rallly/billing";
import { prisma } from "@rallly/database";
import { posthog } from "@rallly/posthog/server";
-import { z } from "zod";
-
-const subscriptionMetadataSchema = z.object({
- userId: z.string(),
-});
+import { updateSpaceGroup } from "@/features/space/analytics";
+import { subscriptionMetadataSchema } from "@/features/subscription/schema";
export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
const subscription = await stripe.subscriptions.retrieve(
@@ -23,14 +20,41 @@ export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
await stripe.invoices.voidInvoice(invoice.id);
}
- // delete the subscription from the database
- await prisma.subscription.delete({
- where: {
- id: subscription.id,
- },
+ const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
+
+ if (!res.success) {
+ throw new Error("Invalid subscription metadata");
+ }
+
+ const { userId, spaceId } = res.data;
+
+ await prisma.$transaction(async (tx) => {
+ await tx.subscription.update({
+ where: {
+ id: subscription.id,
+ },
+ data: {
+ active: false,
+ status: "canceled",
+ },
+ });
+
+ await tx.space.update({
+ where: {
+ id: spaceId,
+ },
+ data: {
+ tier: "hobby",
+ },
+ });
});
- const { userId } = subscriptionMetadataSchema.parse(subscription.metadata);
+ updateSpaceGroup({
+ spaceId,
+ properties: {
+ tier: "hobby",
+ },
+ });
posthog?.capture({
distinctId: userId,
@@ -40,5 +64,8 @@ export async function onCustomerSubscriptionDeleted(event: Stripe.Event) {
tier: "hobby",
},
},
+ groups: {
+ space: spaceId,
+ },
});
}
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
index 713eca7bf..c1821c549 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
@@ -29,6 +29,10 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
throw new Error("Invalid subscription metadata");
}
+ const { userId, spaceId } = res.data;
+
+ const tier = isActive ? "pro" : "hobby";
+
const subscriptionItem = subscription.items.data[0];
if (!subscriptionItem) {
@@ -39,58 +43,60 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
const quantity = subscriptionItem.quantity ?? 1;
// Update the subscription in the database
- const updatedSubscription = await prisma.subscription.update({
- where: {
- id: subscription.id,
- },
- data: {
- active: isActive,
- priceId,
- currency,
- interval,
- subscriptionItemId,
- quantity,
- amount,
- status: subscription.status,
- periodStart: toDate(subscription.current_period_start),
- periodEnd: toDate(subscription.current_period_end),
- cancelAtPeriodEnd: subscription.cancel_at_period_end,
- },
- include: {
- space: true,
- },
- });
-
- if (updatedSubscription.space) {
- updateSpaceGroup({
- spaceId: updatedSubscription.space.id,
- properties: {
- name: updatedSubscription.space.name,
- seatCount: quantity,
- tier: isActive ? "pro" : "hobby",
+ await prisma.$transaction(async (tx) => {
+ await tx.subscription.update({
+ where: {
+ id: subscription.id,
},
- });
-
- posthog?.capture({
- distinctId: res.data.userId,
- uuid: event.id,
- event: "subscription change",
- properties: {
- type: event.type,
+ data: {
+ active: isActive,
+ priceId,
+ currency,
interval,
- $set: {
- tier: isActive ? "pro" : "hobby",
- },
+ subscriptionItemId,
+ quantity,
+ amount,
+ status: subscription.status,
+ periodStart: toDate(subscription.current_period_start),
+ periodEnd: toDate(subscription.current_period_end),
+ cancelAtPeriodEnd: subscription.cancel_at_period_end,
},
- groups: {
- space: res.data.spaceId,
+ include: {
+ space: true,
},
});
- } else {
- console.error("Failed to update space group on subscription.updated", {
- eventId: event.id,
- subscriptionId: subscription.id,
- spaceId: res.data.spaceId,
+
+ await tx.space.update({
+ where: {
+ id: spaceId,
+ },
+ data: {
+ tier,
+ },
});
- }
+ });
+
+ updateSpaceGroup({
+ spaceId,
+ properties: {
+ seatCount: quantity,
+ tier,
+ },
+ });
+
+ posthog?.capture({
+ distinctId: userId,
+ uuid: event.id,
+ event: "subscription change",
+ properties: {
+ type: event.type,
+ interval,
+ $set: {
+ tier,
+ },
+ },
+ groups: {
+ space: spaceId,
+ },
+ });
}
diff --git a/apps/web/src/auth/data.ts b/apps/web/src/auth/data.ts
index b136afc5f..517763f89 100644
--- a/apps/web/src/auth/data.ts
+++ b/apps/web/src/auth/data.ts
@@ -47,11 +47,7 @@ export const getCurrentUserSpace = async () => {
id: true,
name: true,
ownerId: true,
- subscription: {
- select: {
- active: true,
- },
- },
+ tier: true,
},
},
},
@@ -70,7 +66,7 @@ export const getCurrentUserSpace = async () => {
id: spaceMember.space.id,
ownerId: spaceMember.space.ownerId,
name: spaceMember.space.name,
- subscription: spaceMember.space.subscription,
+ tier: spaceMember.space.tier,
role: spaceMember.role,
}),
};
@@ -120,9 +116,12 @@ export const requireSpace = cache(async () => {
},
include: {
space: {
- include: {
- subscription: true,
- members: true,
+ select: {
+ id: true,
+ ownerId: true,
+ name: true,
+ tier: true,
+ image: true,
},
},
},
@@ -136,7 +135,7 @@ export const requireSpace = cache(async () => {
id: spaceMember.space.id,
ownerId: spaceMember.space.ownerId,
name: spaceMember.space.name,
- subscription: spaceMember.space.subscription,
+ tier: spaceMember.space.tier,
role: spaceMember.role,
image: spaceMember.space.image,
});
diff --git a/apps/web/src/features/billing/data.ts b/apps/web/src/features/billing/data.ts
index e8d48f6ad..b2b21ba6a 100644
--- a/apps/web/src/features/billing/data.ts
+++ b/apps/web/src/features/billing/data.ts
@@ -2,10 +2,18 @@ import { prisma } from "@rallly/database";
import type { SpaceTier } from "@/features/space/schema";
export async function getSpaceSubscription(spaceId: string) {
- const subscription = await prisma.subscription.findUnique({
+ const subscription = await prisma.subscription.findFirst({
where: {
spaceId,
},
+ orderBy: [
+ {
+ active: "desc",
+ },
+ {
+ createdAt: "desc",
+ },
+ ],
});
if (!subscription) {
diff --git a/apps/web/src/features/space/actions.ts b/apps/web/src/features/space/actions.ts
index 18e18db60..7d251ba99 100644
--- a/apps/web/src/features/space/actions.ts
+++ b/apps/web/src/features/space/actions.ts
@@ -120,8 +120,10 @@ export const deleteSpaceAction = spaceActionClient
where: {
id: parsedInput.spaceId,
},
- include: {
- subscription: true,
+ select: {
+ id: true,
+ ownerId: true,
+ tier: true,
},
});
@@ -140,7 +142,7 @@ export const deleteSpaceAction = spaceActionClient
}
// Check if space has an active subscription
- if (space.subscription?.active) {
+ if (space.tier === "pro") {
throw new AppError({
code: "FORBIDDEN",
message:
diff --git a/apps/web/src/features/space/data.ts b/apps/web/src/features/space/data.ts
index 514da239d..3bb645ae7 100644
--- a/apps/web/src/features/space/data.ts
+++ b/apps/web/src/features/space/data.ts
@@ -1,5 +1,8 @@
import { accessibleBy } from "@casl/prisma";
-import type { SpaceMemberRole as DBSpaceMemberRole } from "@rallly/database";
+import type {
+ SpaceMemberRole as DBSpaceMemberRole,
+ SpaceTier as DBSpaceTier,
+} from "@rallly/database";
import { prisma } from "@rallly/database";
import { cache } from "react";
import { requireSpace, requireUser } from "@/auth/data";
@@ -35,14 +38,6 @@ function createMemberDTO(member: {
} satisfies MemberDTO;
}
-function getSpaceTier(space: {
- subscription: {
- active: boolean;
- } | null;
-}) {
- return isSelfHosted ? "pro" : space.subscription?.active ? "pro" : "hobby";
-}
-
export async function getSpaceSeatCount(spaceId: string) {
return await prisma.spaceMember.count({
where: {
@@ -57,15 +52,13 @@ export function createSpaceDTO(space: {
name: string;
role: DBSpaceMemberRole;
image?: string | null;
- subscription: {
- active: boolean;
- } | null;
+ tier: DBSpaceTier;
}) {
return {
id: space.id,
name: space.name,
ownerId: space.ownerId,
- tier: getSpaceTier(space),
+ tier: isSelfHosted ? "pro" : space.tier,
role: fromDBRole(space.role),
image: space.image ?? undefined,
} satisfies SpaceDTO;
@@ -81,11 +74,7 @@ export const loadSpaces = cache(async () => {
name: true,
ownerId: true,
image: true,
- subscription: {
- select: {
- active: true,
- },
- },
+ tier: true,
members: {
where: {
userId: user.id,
@@ -112,7 +101,7 @@ export const loadSpaces = cache(async () => {
id: space.id,
name: space.name,
ownerId: space.ownerId,
- tier: space.subscription?.active ? "pro" : "hobby",
+ tier: isSelfHosted ? "pro" : space.tier,
role: fromDBRole(role),
image: space.image ?? undefined,
} satisfies SpaceDTO;
@@ -132,28 +121,6 @@ export const loadSpace = cache(async ({ id }: { id: string }) => {
return space;
});
-export const loadSubscription = cache(async () => {
- const space = await requireSpace();
- const subscription = await prisma.subscription.findUnique({
- where: {
- spaceId: space.id,
- },
- select: {
- id: true,
- active: true,
- amount: true,
- currency: true,
- interval: true,
- status: true,
- quantity: true,
- periodEnd: true,
- cancelAtPeriodEnd: true,
- },
- });
-
- return subscription;
-});
-
export const loadPaymentMethods = cache(async () => {
const user = await requireUser();
const paymentMethods = await prisma.paymentMethod.findMany({
diff --git a/apps/web/src/features/user/ability.ts b/apps/web/src/features/user/ability.ts
index 3c5d17de7..64c85c23f 100644
--- a/apps/web/src/features/user/ability.ts
+++ b/apps/web/src/features/user/ability.ts
@@ -53,11 +53,7 @@ function defineAbilityForUser(
cannot("delete", "User", {
spaces: {
some: {
- subscription: {
- is: {
- active: true,
- },
- },
+ tier: "pro",
},
},
});
diff --git a/apps/web/src/features/user/actions.ts b/apps/web/src/features/user/actions.ts
index f2a85ad3a..71e6714dc 100644
--- a/apps/web/src/features/user/actions.ts
+++ b/apps/web/src/features/user/actions.ts
@@ -63,7 +63,13 @@ export const deleteUserAction = adminActionClient
const user = await prisma.user.findUnique({
where: { id: userId },
- include: { spaces: { include: { subscription: true } } },
+ include: {
+ spaces: {
+ select: {
+ tier: true,
+ },
+ },
+ },
});
if (!user) {
diff --git a/apps/web/tests/house-keeping.spec.ts b/apps/web/tests/house-keeping.spec.ts
index c7055f516..553314e95 100644
--- a/apps/web/tests/house-keeping.spec.ts
+++ b/apps/web/tests/house-keeping.spec.ts
@@ -1,7 +1,7 @@
import { expect, test } from "@playwright/test";
import { prisma } from "@rallly/database";
import dayjs from "dayjs";
-import { createUserInDb, createSpaceWithSubscription, createTestPoll } from "./test-utils";
+import { createUserInDb, createSpaceInDb, createTestPoll } from "./test-utils";
/**
* This test suite tests the house-keeping API endpoints:
@@ -71,10 +71,10 @@ test.describe("House-keeping API", () => {
createdUserIds.push(spaceOwner.id);
// Create a space with an active subscription
- const paidSpace = await createSpaceWithSubscription({
+ const paidSpace = await createSpaceInDb({
name: "Paid Space",
ownerId: spaceOwner.id,
- subscriptionId: "sub_test_paid",
+ tier: "pro",
});
// Create test polls - These should be marked as deleted (free space, old, no future dates)
diff --git a/apps/web/tests/test-utils.ts b/apps/web/tests/test-utils.ts
index 46ae14518..ef7a688f4 100644
--- a/apps/web/tests/test-utils.ts
+++ b/apps/web/tests/test-utils.ts
@@ -1,5 +1,5 @@
import type { Page } from "@playwright/test";
-import type { UserRole } from "@rallly/database";
+import type { SpaceTier, UserRole } from "@rallly/database";
import { prisma } from "@rallly/database";
import dayjs from "dayjs";
import { LoginPage } from "./login-page";
@@ -29,6 +29,7 @@ export async function createUserInDb({
data: {
name: "Personal",
ownerId: user.id,
+ tier: "hobby"
},
});
@@ -44,34 +45,20 @@ export async function createUserInDb({
});
}
-export async function createSpaceWithSubscription({
+export async function createSpaceInDb({
name,
ownerId,
- subscriptionId = "sub_test",
+ tier
}: {
name: string;
ownerId: string;
- subscriptionId?: string;
+ tier: SpaceTier;
}) {
- return await prisma.space.create({
+ return prisma.space.create({
data: {
name,
ownerId,
- subscription: {
- create: {
- id: subscriptionId,
- priceId: "price_test",
- amount: 1000,
- status: "active",
- active: true,
- subscriptionItemId: "si_test",
- currency: "USD",
- interval: "month",
- periodStart: new Date(),
- periodEnd: dayjs().add(30, "day").toDate(),
- userId: ownerId,
- },
- },
+ tier,
},
});
}
diff --git a/packages/database/prisma/migrations/20251107120000_allow_multiple_space_subscriptions/migration.sql b/packages/database/prisma/migrations/20251107120000_allow_multiple_space_subscriptions/migration.sql
new file mode 100644
index 000000000..5b492dff1
--- /dev/null
+++ b/packages/database/prisma/migrations/20251107120000_allow_multiple_space_subscriptions/migration.sql
@@ -0,0 +1,3 @@
+-- DropIndex
+DROP INDEX IF EXISTS "subscriptions_space_id_key";
+
diff --git a/packages/database/prisma/migrations/20251107131500_add_space_tier/migration.sql b/packages/database/prisma/migrations/20251107131500_add_space_tier/migration.sql
new file mode 100644
index 000000000..c43c5eef3
--- /dev/null
+++ b/packages/database/prisma/migrations/20251107131500_add_space_tier/migration.sql
@@ -0,0 +1,16 @@
+-- CreateEnum
+CREATE TYPE "space_tiers" AS ENUM ('hobby', 'pro');
+
+-- AlterTable
+ALTER TABLE "spaces" ADD COLUMN "tier" "space_tiers" NOT NULL DEFAULT 'hobby';
+
+-- Populate new column based on active subscriptions
+UPDATE "spaces"
+SET "tier" = 'pro'
+WHERE EXISTS (
+ SELECT 1
+ FROM "subscriptions"
+ WHERE "subscriptions"."space_id" = "spaces"."id"
+ AND "subscriptions"."active" = true
+);
+
diff --git a/packages/database/prisma/migrations/20251110113931_non_nullable_space_id_susbcription/migration.sql b/packages/database/prisma/migrations/20251110113931_non_nullable_space_id_susbcription/migration.sql
new file mode 100644
index 000000000..214939138
--- /dev/null
+++ b/packages/database/prisma/migrations/20251110113931_non_nullable_space_id_susbcription/migration.sql
@@ -0,0 +1,8 @@
+-- DropForeignKey
+ALTER TABLE "subscriptions" DROP CONSTRAINT "subscriptions_space_id_fkey";
+
+-- AlterTable
+ALTER TABLE "subscriptions" ALTER COLUMN "space_id" SET NOT NULL;
+
+-- AddForeignKey
+ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "spaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/packages/database/prisma/models/billing.prisma b/packages/database/prisma/models/billing.prisma
index 721cb7497..411f7dcff 100644
--- a/packages/database/prisma/models/billing.prisma
+++ b/packages/database/prisma/models/billing.prisma
@@ -13,10 +13,10 @@ model Subscription {
periodEnd DateTime @map("period_end")
cancelAtPeriodEnd Boolean @default(false) @map("cancel_at_period_end")
userId String @map("user_id")
- spaceId String? @unique @map("space_id")
+ spaceId String @map("space_id")
- user User @relation("UserToSubscription", fields: [userId], references: [id], onDelete: Cascade)
- space Space? @relation("SpaceToSubscription", fields: [spaceId], references: [id], onDelete: SetNull)
+ user User @relation("UserToSubscription", fields: [userId], references: [id], onDelete: Cascade)
+ space Space @relation("SpaceToSubscription", fields: [spaceId], references: [id], onDelete: Cascade)
@@index([userId], type: Hash)
@@index([spaceId], type: Hash)
diff --git a/packages/database/prisma/models/space.prisma b/packages/database/prisma/models/space.prisma
index 7f0b0d2d8..ffd80f52c 100644
--- a/packages/database/prisma/models/space.prisma
+++ b/packages/database/prisma/models/space.prisma
@@ -1,15 +1,16 @@
model Space {
- id String @id @default(uuid())
+ id String @id @default(uuid())
name String
image String?
- ownerId String @map("owner_id")
- createdAt DateTime @default(now()) @map("created_at")
- updatedAt DateTime @updatedAt @map("updated_at")
+ ownerId String @map("owner_id")
+ tier SpaceTier @default(hobby)
+ createdAt DateTime @default(now()) @map("created_at")
+ updatedAt DateTime @updatedAt @map("updated_at")
owner User @relation("UserSpaces", fields: [ownerId], references: [id], onDelete: Cascade)
polls Poll[]
scheduledEvents ScheduledEvent[]
- subscription Subscription? @relation("SpaceToSubscription")
+ subscriptions Subscription[] @relation("SpaceToSubscription")
members SpaceMember[]
@@ -24,6 +25,13 @@ enum SpaceMemberRole {
MEMBER
}
+enum SpaceTier {
+ hobby
+ pro
+
+ @@map("space_tiers")
+}
+
model SpaceMember {
id String @id @default(uuid())
spaceId String @map("space_id")
diff --git a/packages/database/prisma/seed/users.ts b/packages/database/prisma/seed/users.ts
index f97aaf298..30fc67511 100644
--- a/packages/database/prisma/seed/users.ts
+++ b/packages/database/prisma/seed/users.ts
@@ -28,31 +28,12 @@ async function createUser({
create: {
id: space.id,
name: space.name,
+ tier: space.isPro ? "pro" : "hobby",
},
},
},
});
- if (space.isPro) {
- await prisma.subscription.create({
- data: {
- id: "sub-1",
- spaceId: space.id,
- userId: user.id,
- subscriptionItemId: "subitem-1",
- quantity: 1,
- active: true,
- amount: 700,
- currency: "USD",
- interval: "month",
- periodStart: new Date(),
- periodEnd: dayjs().add(1, "month").toDate(),
- priceId: "price_1M6ZJZGZJZJZJZJZJZJZJZJZ",
- status: "active",
- },
- });
- }
-
await prisma.spaceMember.create({
data: {
spaceId: space.id,
@@ -83,6 +64,7 @@ async function createTeamSpace() {
id: "team-space-1",
name: "Acme Corp Team",
ownerId: teamOwner.id,
+ tier: "pro",
},
});
@@ -165,25 +147,6 @@ async function createTeamSpace() {
}),
]);
- // Add Pro subscription for the team space
- await prisma.subscription.create({
- data: {
- id: "team-sub-1",
- spaceId: teamSpace.id,
- userId: teamOwner.id,
- subscriptionItemId: "team-subitem-1",
- quantity: 5, // Team of 5
- active: true,
- amount: 2800, // $28 for team plan
- currency: "USD",
- interval: "month",
- periodStart: new Date(),
- periodEnd: dayjs().add(1, "month").toDate(),
- priceId: "price_team_monthly",
- status: "active",
- },
- });
-
console.info(`✓ Seeded team space with ${[teamOwner, ...teamMembers].length} members`);
return [teamOwner, ...teamMembers];