153 lines
3.5 KiB
TypeScript
153 lines
3.5 KiB
TypeScript
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();
|
|
});
|