mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-23 11:17:26 +02:00
🗃️ Store active space for user (#1807)
This commit is contained in:
parent
d93baeafd9
commit
d672eb1012
15 changed files with 196 additions and 94 deletions
|
@ -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
|
||||
);
|
|
@ -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")
|
||||
|
|
|
@ -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[]
|
||||
|
|
|
@ -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",
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue