feat: add current workspace when create user

This commit is contained in:
moonrailgun 2023-09-03 14:38:38 +08:00
parent 55463ac17f
commit 421ee8edc1
2 changed files with 53 additions and 21 deletions

View File

@ -8,13 +8,16 @@ generator client {
} }
model User { model User {
id String @id @unique @default(uuid()) @db.Uuid id String @id @unique @default(uuid()) @db.Uuid
username String @unique @db.VarChar(255) username String @unique @db.VarChar(255)
password String @db.VarChar(60) password String @db.VarChar(60)
role String @db.VarChar(50) role String @db.VarChar(50)
createdAt DateTime? @default(now()) @db.Timestamptz(6) createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6) updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6) deletedAt DateTime? @db.Timestamptz(6)
currentWorkspaceId String? @db.Uuid
currentWorkspace Workspace? @relation(fields: [currentWorkspaceId], references: [id])
workspaces WorkspacesOnUsers[] workspaces WorkspacesOnUsers[]
} }
@ -27,6 +30,7 @@ model Workspace {
users WorkspacesOnUsers[] users WorkspacesOnUsers[]
website Website[] website Website[]
User User[]
} }
model WorkspacesOnUsers { model WorkspacesOnUsers {
@ -56,15 +60,15 @@ model Website {
updatedAt DateTime? @updatedAt @db.Timestamptz(6) updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6) deletedAt DateTime? @db.Timestamptz(6)
eventData EventData[] eventData WebsiteEventData[]
sessionData SessionData[] sessionData WebsiteSessionData[]
@@index([workspaceId]) @@index([workspaceId])
@@index([createdAt]) @@index([createdAt])
@@index([shareId]) @@index([shareId])
} }
model Session { model WebsiteSession {
id String @id @unique @db.Uuid id String @id @unique @db.Uuid
websiteId String @db.Uuid websiteId String @db.Uuid
hostname String? @db.VarChar(100) hostname String? @db.VarChar(100)
@ -80,7 +84,7 @@ model Session {
createdAt DateTime? @default(now()) @db.Timestamptz(6) createdAt DateTime? @default(now()) @db.Timestamptz(6)
websiteEvent WebsiteEvent[] websiteEvent WebsiteEvent[]
sessionData SessionData[] sessionData WebsiteSessionData[]
@@index([createdAt]) @@index([createdAt])
@@index([websiteId]) @@index([websiteId])
@ -110,8 +114,8 @@ model WebsiteEvent {
eventType Int @default(1) @db.Integer eventType Int @default(1) @db.Integer
eventName String? @db.VarChar(50) eventName String? @db.VarChar(50)
eventData EventData[] eventData WebsiteEventData[]
session Session @relation(fields: [sessionId], references: [id]) session WebsiteSession @relation(fields: [sessionId], references: [id])
@@index([createdAt]) @@index([createdAt])
@@index([sessionId]) @@index([sessionId])
@ -125,7 +129,7 @@ model WebsiteEvent {
@@index([websiteId, sessionId, createdAt]) @@index([websiteId, sessionId, createdAt])
} }
model EventData { model WebsiteEventData {
id String @id() @db.Uuid id String @id() @db.Uuid
websiteId String @db.Uuid websiteId String @db.Uuid
websiteEventId String @db.Uuid websiteEventId String @db.Uuid
@ -146,7 +150,7 @@ model EventData {
@@index([websiteId, createdAt, eventKey]) @@index([websiteId, createdAt, eventKey])
} }
model SessionData { model WebsiteSessionData {
id String @id() @db.Uuid id String @id() @db.Uuid
websiteId String @db.Uuid websiteId String @db.Uuid
sessionId String @db.Uuid sessionId String @db.Uuid
@ -158,8 +162,8 @@ model SessionData {
createdAt DateTime? @default(now()) @db.Timestamptz(6) createdAt DateTime? @default(now()) @db.Timestamptz(6)
deletedAt DateTime? @default(now()) @db.Timestamptz(6) deletedAt DateTime? @default(now()) @db.Timestamptz(6)
website Website @relation(fields: [websiteId], references: [id]) website Website @relation(fields: [websiteId], references: [id])
session Session @relation(fields: [sessionId], references: [id]) session WebsiteSession @relation(fields: [sessionId], references: [id])
@@index([createdAt]) @@index([createdAt])
@@index([websiteId]) @@index([websiteId])

View File

@ -16,7 +16,7 @@ export async function createAdminUser(username: string, password: string) {
throw new Error('Create Admin User Just Only allow in non people exist'); throw new Error('Create Admin User Just Only allow in non people exist');
} }
await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -34,7 +34,21 @@ export async function createAdminUser(username: string, password: string) {
], ],
}, },
}, },
include: {
workspaces: true,
},
}); });
if (user.workspaces[0]) {
prisma.user.update({
where: {
id: user.id,
},
data: {
currentWorkspaceId: user.workspaces[0].workspaceId,
},
});
}
} }
export async function createUser(username: string, password: string) { export async function createUser(username: string, password: string) {
@ -48,7 +62,7 @@ export async function createUser(username: string, password: string) {
throw new Error('User already exists'); throw new Error('User already exists');
} }
await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -66,11 +80,25 @@ export async function createUser(username: string, password: string) {
], ],
}, },
}, },
include: {
workspaces: true,
},
}); });
if (user.workspaces[0]) {
prisma.user.update({
where: {
id: user.id,
},
data: {
currentWorkspaceId: user.workspaces[0].workspaceId,
},
});
}
} }
export async function authUser(username: string, password: string) { export async function authUser(username: string, password: string) {
const user = await prisma.user.findFirstOrThrow({ const user = await prisma.user.findUniqueOrThrow({
where: { where: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -89,7 +117,7 @@ export async function authUser(username: string, password: string) {
} }
export async function findUser(userId: string) { export async function findUser(userId: string) {
const user = await prisma.user.findFirst({ const user = await prisma.user.findUnique({
where: { where: {
id: userId, id: userId,
}, },