feat: add workspace

This commit is contained in:
moonrailgun 2023-09-03 03:49:20 +08:00
parent 2193ff4bcb
commit 84c663985a
5 changed files with 102 additions and 18 deletions

View File

@ -1,2 +1,9 @@
# tianji
Tianji must not be leaked
## Open Source
`Tianji` is open source with `Apache 2.0` license.
And its inspired by `umami` license which under `MIT` and `uptime-kuma` which under `MIT` license too

View File

@ -4,11 +4,11 @@ datasource db {
}
generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
}
model User {
id String @id @unique @db.Uuid @default(uuid())
id String @id @unique @default(uuid()) @db.Uuid
username String @unique @db.VarChar(255)
password String @db.VarChar(60)
role String @db.VarChar(50)
@ -16,25 +16,50 @@ model User {
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6)
workspaces WorkspacesOnUsers[]
}
model Workspace {
id String @id @unique @default(uuid()) @db.Uuid
name String @db.VarChar(100)
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
users WorkspacesOnUsers[]
website Website[]
}
model Website {
id String @id @unique @db.Uuid @default(uuid())
name String @db.VarChar(100)
domain String? @db.VarChar(500)
shareId String? @unique @db.VarChar(50)
resetAt DateTime? @db.Timestamptz(6)
userId String? @db.Uuid
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6)
model WorkspacesOnUsers {
userId String @db.Uuid
workspaceId String @db.Uuid
role String @db.VarChar(100)
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
user User @relation(fields: [userId], references: [id])
workspace Workspace @relation(fields: [workspaceId], references: [id])
@@id([userId, workspaceId])
@@index([userId])
@@index([workspaceId])
}
model Website {
id String @id @unique @default(uuid()) @db.Uuid
name String @db.VarChar(100)
domain String? @db.VarChar(500)
shareId String? @unique @db.VarChar(50)
resetAt DateTime? @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id])
workspaceId String @db.Uuid
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6)
user User? @relation(fields: [userId], references: [id])
eventData EventData[]
sessionData SessionData[]
@@index([userId])
@@index([workspaceId])
@@index([createdAt])
@@index([shareId])
}

View File

@ -1,5 +1,6 @@
import { prisma } from './_client';
import bcryptjs from 'bcryptjs';
import { ROLES } from '../utils/const';
async function hashPassword(password: string) {
return await bcryptjs.hash(password, 10);
@ -19,17 +20,51 @@ export async function createAdminUser(username: string, password: string) {
data: {
username,
password: await hashPassword(password),
role: 'admin',
role: ROLES.admin,
workspaces: {
create: [
{
role: ROLES.owner,
workspace: {
create: {
name: username,
},
},
},
],
},
},
});
}
export async function createUser(username: string, password: string) {
const existCount = await prisma.user.count({
where: {
username,
},
});
if (existCount > 0) {
throw new Error('User already exists');
}
await prisma.user.create({
data: {
username,
password: await hashPassword(password),
role: 'normal',
role: ROLES.user,
workspaces: {
create: [
{
role: ROLES.owner,
workspace: {
create: {
name: username,
},
},
},
],
},
},
});
}
@ -40,6 +75,14 @@ export async function authUser(username: string, password: string) {
username,
password: await hashPassword(password),
},
select: {
id: true,
username: true,
role: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
},
});
return user;

View File

@ -0,0 +1,9 @@
export const ROLES = {
// System Role
admin: 'admin',
user: 'user',
// Workspace Role
owner: 'owner',
readOnly: 'readOnly',
} as const;

View File

@ -1,5 +1,5 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({