library-picturebook-activity/backend/scripts/add-missing-contest-menus.ts

87 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-01-09 18:14:35 +08:00
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
console.log('🔧 添加缺失的赛事管理菜单...\n');
// 查找赛事管理父菜单
const contestMenu = await prisma.menu.findFirst({
where: { name: '赛事管理', parentId: null }
});
if (!contestMenu) {
console.log('❌ 未找到赛事管理菜单');
return;
}
console.log('✅ 找到赛事管理菜单 ID:', contestMenu.id);
// 要添加的菜单
const menusToAdd = [
{
name: '评委管理',
path: '/contests/judges',
component: 'contests/judges/Index',
permission: 'contest:judge:read',
sort: 35, // 在评审规则之后
},
{
name: '评审进度',
path: '/contests/reviews/progress',
component: 'contests/reviews/Progress',
permission: 'contest:review:read',
sort: 36,
},
];
for (const menuData of menusToAdd) {
// 检查是否已存在
const existing = await prisma.menu.findFirst({
where: { path: menuData.path }
});
if (existing) {
console.log(`⏭️ ${menuData.name} 已存在`);
continue;
}
// 创建菜单
const newMenu = await prisma.menu.create({
data: {
...menuData,
parentId: contestMenu.id,
validState: 1,
}
});
console.log(`✅ 已创建: ${menuData.name} (ID: ${newMenu.id})`);
// 分配给所有租户
const tenants = await prisma.tenant.findMany({ select: { id: true } });
for (const tenant of tenants) {
await prisma.tenantMenu.create({
data: { tenantId: tenant.id, menuId: newMenu.id }
});
}
console.log(` 已分配给 ${tenants.length} 个租户`);
}
// 显示最终结构
console.log('\n📋 赛事管理最终菜单结构:');
const children = await prisma.menu.findMany({
where: { parentId: contestMenu.id },
orderBy: { sort: 'asc' }
});
console.log(`- ${contestMenu.name}`);
children.forEach(c => {
console.log(` - ${c.name} | ${c.path}`);
});
console.log('\n✅ 完成!');
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());