🗃️ Store active space for user (#1807)

This commit is contained in:
Luke Vella 2025-07-11 10:35:28 +01:00 committed by GitHub
parent d93baeafd9
commit d672eb1012
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 196 additions and 94 deletions

View file

@ -0,0 +1,14 @@
-- AlterTable
ALTER TABLE "users" ADD COLUMN "active_space_id" TEXT;
-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_active_space_id_fkey" FOREIGN KEY ("active_space_id") REFERENCES "spaces"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- Set active_space_id to each user's default space (where they are the owner)
UPDATE "users" u
SET "active_space_id" = (
SELECT s.id
FROM "spaces" s
WHERE s."owner_id" = u.id
LIMIT 1
);

View file

@ -10,7 +10,8 @@ model Space {
scheduledEvents ScheduledEvent[]
subscription Subscription? @relation("SpaceToSubscription")
members SpaceMember[]
members SpaceMember[]
activeForUsers User[] @relation("UserActiveSpace")
@@index([ownerId], type: Hash)
@@map("spaces")

View file

@ -49,6 +49,7 @@ model User {
bannedAt DateTime? @map("banned_at")
banReason String? @map("ban_reason")
role UserRole @default(user)
activeSpaceId String? @map("active_space_id")
comments Comment[]
polls Poll[]
@ -57,6 +58,7 @@ model User {
participants Participant[]
paymentMethods PaymentMethod[]
subscription Subscription? @relation("UserToSubscription")
activeSpace Space? @relation("UserActiveSpace", fields: [activeSpaceId], references: [id], onDelete: SetNull)
spaces Space[] @relation("UserSpaces")
memberOf SpaceMember[]

View file

@ -1,68 +1,72 @@
import { prisma } from "@rallly/database";
import dayjs from "dayjs";
async function createUser({
id,
name,
email,
timeZone,
space,
}: {
id: string;
name: string;
email: string;
timeZone: string;
space: {
id: string;
name: string;
};
}) {
const user = await prisma.user.create({
data: {
id,
name,
email,
timeZone,
spaces: {
create: space,
},
},
});
await prisma.spaceMember.create({
data: {
spaceId: space.id,
userId: id,
role: "OWNER",
},
});
await prisma.user.update({
where: { id },
data: {
activeSpaceId: space.id,
},
});
return user;
}
export async function seedUsers() {
console.info("Seeding users...");
const freeUser = await prisma.user.upsert({
where: { email: "dev@rallly.co" },
update: {},
create: {
id: "free-user",
name: "Dev User",
email: "dev@rallly.co",
timeZone: "America/New_York",
spaces: {
create: {
id: "space-1",
name: "Personal",
},
},
const freeUser = await createUser({
id: "free-user",
name: "Dev User",
email: "dev@rallly.co",
timeZone: "America/New_York",
space: {
id: "space-1",
name: "Personal",
},
});
await prisma.spaceMember.create({
data: {
spaceId: "space-1",
userId: "free-user",
role: "OWNER",
},
});
const proUser = await prisma.user.upsert({
where: { email: "dev+pro@rallly.co" },
update: {},
create: {
id: "pro-user",
name: "Pro User",
email: "dev+pro@rallly.co",
spaces: {
create: {
id: "space-2",
name: "Personal",
},
},
subscription: {
create: {
id: "sub_123",
currency: "usd",
amount: 700,
interval: "month",
status: "active",
active: true,
priceId: "price_123",
periodStart: new Date(),
periodEnd: dayjs().add(1, "month").toDate(),
spaceId: "space-2",
},
},
},
});
await prisma.spaceMember.create({
data: {
spaceId: "space-2",
userId: "pro-user",
role: "OWNER",
const proUser = await createUser({
id: "pro-user",
name: "Pro User",
email: "dev+pro@rallly.co",
timeZone: "America/New_York",
space: {
id: "space-2",
name: "Personal",
},
});