148 lines
3.9 KiB
TypeScript
148 lines
3.9 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
async function main() {
|
|
console.log('🌱 Starting database seeding...')
|
|
|
|
// 检查是否已存在订阅套餐
|
|
const existingPlans = await prisma.subscriptionPlan.count()
|
|
|
|
if (existingPlans > 0) {
|
|
console.log('📦 Subscription plans already exist, skipping seed...')
|
|
return
|
|
}
|
|
|
|
console.log('📦 Creating default subscription plans...')
|
|
|
|
// 创建免费套餐
|
|
const freePlan = await prisma.subscriptionPlan.create({
|
|
data: {
|
|
id: 'free',
|
|
name: 'Free',
|
|
displayName: 'Free Plan',
|
|
description: 'Basic features for getting started with AI prompt management',
|
|
price: 0,
|
|
currency: 'usd',
|
|
interval: 'month',
|
|
stripePriceId: null,
|
|
isActive: true,
|
|
sortOrder: 1,
|
|
features: {
|
|
promptLimit: true,
|
|
versionsPerPrompt: true,
|
|
basicSupport: true,
|
|
publicSharing: true
|
|
},
|
|
limits: {
|
|
maxVersionLimit: 3,
|
|
promptLimit: 500,
|
|
creditMonthly: 0
|
|
}
|
|
}
|
|
})
|
|
|
|
// 创建 Pro 套餐
|
|
const proPlan = await prisma.subscriptionPlan.create({
|
|
data: {
|
|
id: 'pro',
|
|
name: 'pro', // 重要:名称必须是 "pro" 用于查找
|
|
displayName: 'Pro Plan',
|
|
description: 'Advanced features for power users and professionals',
|
|
price: 19.9,
|
|
currency: 'usd',
|
|
interval: 'month',
|
|
stripePriceId: null, // 需要手动在数据库中设置 Stripe 价格 ID
|
|
isActive: true,
|
|
sortOrder: 2,
|
|
features: {
|
|
promptLimit: true,
|
|
versionsPerPrompt: true,
|
|
prioritySupport: true,
|
|
advancedAnalytics: true,
|
|
apiAccess: true,
|
|
teamCollaboration: true,
|
|
exportImport: true,
|
|
customBranding: true
|
|
},
|
|
limits: {
|
|
maxVersionLimit: 10,
|
|
promptLimit: 5000,
|
|
creditMonthly: 20
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log(`✅ Created subscription plan: ${freePlan.displayName}`)
|
|
console.log(`✅ Created subscription plan: ${proPlan.displayName}`)
|
|
|
|
// 更新现有用户的订阅套餐关联
|
|
console.log('👥 Updating existing users...')
|
|
|
|
// 获取所有用户,不管是否已有订阅套餐关联
|
|
const users = await prisma.user.findMany({
|
|
select: {
|
|
id: true,
|
|
subscribePlan: true,
|
|
subscriptionPlanId: true
|
|
}
|
|
})
|
|
|
|
// 过滤出需要更新的用户(没有正确的订阅套餐关联的)
|
|
const usersToUpdate = users.filter(user =>
|
|
!user.subscriptionPlanId ||
|
|
user.subscriptionPlanId === '' ||
|
|
(user.subscribePlan === 'pro' && user.subscriptionPlanId !== 'pro') ||
|
|
(user.subscribePlan === 'free' && user.subscriptionPlanId !== 'free')
|
|
)
|
|
|
|
if (usersToUpdate.length > 0) {
|
|
console.log(`📝 Found ${usersToUpdate.length} users to update`)
|
|
|
|
for (const user of usersToUpdate) {
|
|
let planId = 'free'
|
|
|
|
// 根据现有的 subscribePlan 字段确定新的套餐 ID
|
|
if (user.subscribePlan === 'pro') {
|
|
planId = 'pro'
|
|
}
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: {
|
|
subscriptionPlanId: planId
|
|
}
|
|
})
|
|
}
|
|
|
|
console.log(`✅ Updated ${usersToUpdate.length} users with subscription plan associations`)
|
|
} else {
|
|
console.log('👥 No users need subscription plan updates')
|
|
}
|
|
|
|
// 验证种子数据
|
|
const planCount = await prisma.subscriptionPlan.count()
|
|
const usersWithPlans = await prisma.user.count({
|
|
where: {
|
|
subscriptionPlanId: {
|
|
not: ''
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log(`\n📊 Seeding completed:`)
|
|
console.log(` • ${planCount} subscription plans created`)
|
|
console.log(` • ${usersWithPlans} users have valid plan associations`)
|
|
console.log(`\n🎉 Database seeding finished successfully!`)
|
|
}
|
|
|
|
main()
|
|
.then(async () => {
|
|
await prisma.$disconnect()
|
|
})
|
|
.catch(async (e) => {
|
|
console.error('❌ Seeding failed:', e)
|
|
await prisma.$disconnect()
|
|
process.exit(1)
|
|
})
|