first commit
This commit is contained in:
99
backend/prisma/schema.prisma
Normal file
99
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,99 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
username String @unique
|
||||
password String
|
||||
firstName String?
|
||||
lastName String?
|
||||
isActive Boolean @default(true)
|
||||
isAdmin Boolean @default(false)
|
||||
lastLoginAt DateTime?
|
||||
loginAttempts Int @default(0)
|
||||
lockedUntil DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
sessions Session[]
|
||||
auditLogs AuditLog[]
|
||||
accountAssignments AccountAssignment[]
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model WebsiteAccount {
|
||||
id String @id @default(cuid())
|
||||
website String
|
||||
accountName String
|
||||
token String
|
||||
isActive Boolean @default(true)
|
||||
maxUsers Int @default(1)
|
||||
currentUsers Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
accountAssignments AccountAssignment[]
|
||||
|
||||
@@unique([website, accountName])
|
||||
@@map("website_accounts")
|
||||
}
|
||||
|
||||
model AccountAssignment {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
accountId String
|
||||
assignedAt DateTime @default(now())
|
||||
expiresAt DateTime?
|
||||
isActive Boolean @default(true)
|
||||
|
||||
// Relations
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
account WebsiteAccount @relation(fields: [accountId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([userId, accountId])
|
||||
@@map("account_assignments")
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
token String @unique
|
||||
ipAddress String?
|
||||
userAgent String?
|
||||
expiresAt DateTime
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@map("sessions")
|
||||
}
|
||||
|
||||
model AuditLog {
|
||||
id String @id @default(cuid())
|
||||
userId String?
|
||||
action String
|
||||
resource String?
|
||||
resourceId String?
|
||||
details String? // JSON string
|
||||
ipAddress String?
|
||||
userAgent String?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
// Relations
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@map("audit_logs")
|
||||
}
|
||||
153
backend/prisma/seed.ts
Normal file
153
backend/prisma/seed.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('开始初始化数据库...');
|
||||
|
||||
// 创建管理员用户
|
||||
const adminPassword = await bcrypt.hash('admin123', 12);
|
||||
const admin = await prisma.user.upsert({
|
||||
where: { email: 'admin@pandora.com' },
|
||||
update: {},
|
||||
create: {
|
||||
email: 'admin@pandora.com',
|
||||
username: 'admin',
|
||||
password: adminPassword,
|
||||
firstName: '管理员',
|
||||
lastName: '系统',
|
||||
isAdmin: true,
|
||||
isActive: true,
|
||||
emailVerified: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 创建测试用户
|
||||
const userPassword = await bcrypt.hash('user123', 12);
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email: 'user@pandora.com' },
|
||||
update: {},
|
||||
create: {
|
||||
email: 'user@pandora.com',
|
||||
username: 'testuser',
|
||||
password: userPassword,
|
||||
firstName: '测试',
|
||||
lastName: '用户',
|
||||
isAdmin: false,
|
||||
isActive: true,
|
||||
emailVerified: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 创建网站账号
|
||||
const accounts = [
|
||||
{
|
||||
website: 'claude.ai',
|
||||
accountName: 'claude_pro_1',
|
||||
token: 'sk-ant-api03-xxx-claude-pro-1',
|
||||
maxUsers: 3,
|
||||
currentUsers: 0,
|
||||
},
|
||||
{
|
||||
website: 'openai.com',
|
||||
accountName: 'gpt4_plus_1',
|
||||
token: 'sk-xxx-gpt4-plus-1',
|
||||
maxUsers: 2,
|
||||
currentUsers: 0,
|
||||
},
|
||||
{
|
||||
website: 'gemini.google.com',
|
||||
accountName: 'gemini_pro_1',
|
||||
token: 'AIzaSyCxxx-gemini-pro-1',
|
||||
maxUsers: 1,
|
||||
currentUsers: 0,
|
||||
},
|
||||
];
|
||||
|
||||
for (const accountData of accounts) {
|
||||
await prisma.websiteAccount.upsert({
|
||||
where: {
|
||||
website_accountName: {
|
||||
website: accountData.website,
|
||||
accountName: accountData.accountName,
|
||||
}
|
||||
},
|
||||
update: {},
|
||||
create: accountData,
|
||||
});
|
||||
}
|
||||
|
||||
// 为管理员分配所有账号
|
||||
const allAccounts = await prisma.websiteAccount.findMany();
|
||||
for (const account of allAccounts) {
|
||||
await prisma.accountAssignment.upsert({
|
||||
where: {
|
||||
userId_accountId: {
|
||||
userId: admin.id,
|
||||
accountId: account.id,
|
||||
}
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
userId: admin.id,
|
||||
accountId: account.id,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 为用户分配部分账号
|
||||
const userAccounts = await prisma.websiteAccount.findMany({
|
||||
where: {
|
||||
website: {
|
||||
in: ['claude.ai', 'openai.com']
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const account of userAccounts) {
|
||||
await prisma.accountAssignment.upsert({
|
||||
where: {
|
||||
userId_accountId: {
|
||||
userId: user.id,
|
||||
accountId: account.id,
|
||||
}
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
userId: user.id,
|
||||
accountId: account.id,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 更新账号当前用户数
|
||||
for (const account of allAccounts) {
|
||||
const userCount = await prisma.accountAssignment.count({
|
||||
where: {
|
||||
accountId: account.id,
|
||||
isActive: true,
|
||||
}
|
||||
});
|
||||
|
||||
await prisma.websiteAccount.update({
|
||||
where: { id: account.id },
|
||||
data: { currentUsers: userCount }
|
||||
});
|
||||
}
|
||||
|
||||
console.log('数据库初始化完成!');
|
||||
console.log('管理员账户:', admin.email, '密码: admin123');
|
||||
console.log('测试用户账户:', user.email, '密码: user123');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error('数据库初始化失败:', e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user