import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { console.log('๐Ÿ”ง ๆœ€็ปˆ้…็ฝฎๅญฆ็”Ÿๆƒ้™...\n'); // ่Žทๅ–ๅญฆ็”Ÿ่ง’่‰ฒ const studentRoles = await prisma.role.findMany({ where: { code: 'student' } }); for (const studentRole of studentRoles) { console.log(`\nๅค„็†ๅญฆ็”Ÿ่ง’่‰ฒ ID: ${studentRole.id}`); // ๅญฆ็”Ÿๆœ€็ปˆ้œ€่ฆ็š„ๆƒ้™ const neededPerms = [ 'workbench:read', // ๅทฅไฝœๅฐ 'contest:activity:read', // ่ต›ไบ‹ๆดปๅŠจ 'homework:student:read', // ๆˆ‘็š„ไฝœไธš่œๅ• ]; // ็กฎไฟๆ‰€ๆœ‰้œ€่ฆ็š„ๆƒ้™้ƒฝๅทฒๅˆ†้… for (const permCode of neededPerms) { const perm = await prisma.permission.findFirst({ where: { tenantId: studentRole.tenantId, code: permCode } }); if (perm) { const existing = await prisma.rolePermission.findFirst({ where: { roleId: studentRole.id, permissionId: perm.id } }); if (!existing) { await prisma.rolePermission.create({ data: { roleId: studentRole.id, permissionId: perm.id } }); console.log(` โœ… ๅทฒๆทปๅŠ : ${permCode}`); } else { console.log(` โญ๏ธ ๅทฒๅญ˜ๅœจ: ${permCode}`); } } else { console.log(` โŒ ๆƒ้™ไธๅญ˜ๅœจ: ${permCode}`); } } // ็งป้™คไธ้œ€่ฆ็š„ๆƒ้™ const permsToRemove = ['homework:read']; for (const permCode of permsToRemove) { const perm = await prisma.permission.findFirst({ where: { tenantId: studentRole.tenantId, code: permCode } }); if (perm) { const deleted = await prisma.rolePermission.deleteMany({ where: { roleId: studentRole.id, permissionId: perm.id } }); if (deleted.count > 0) { console.log(` ๐Ÿ—‘๏ธ ๅทฒ็งป้™ค: ${permCode}`); } } } } // ๆ›ดๆ–ฐๅญฆ็”Ÿไฝœไธš่œๅ• console.log('\n๐Ÿ“ ๆ›ดๆ–ฐๅญฆ็”Ÿไฝœไธš่œๅ•...'); const studentMenu = await prisma.menu.findFirst({ where: { path: '/student-homework' } }); if (studentMenu) { await prisma.menu.update({ where: { id: studentMenu.id }, data: { permission: 'homework:student:read', component: 'homework/StudentList', } }); console.log(' โœ… ่œๅ•ๅทฒๆ›ดๆ–ฐ'); } // ๆ˜พ็คบๆœ€็ปˆๆƒ้™ console.log('\n๐Ÿ“‹ ๅญฆ็”Ÿ่ง’่‰ฒๆœ€็ปˆๆƒ้™:'); for (const studentRole of studentRoles) { const perms = await prisma.rolePermission.findMany({ where: { roleId: studentRole.id }, include: { permission: true } }); console.log(`\n ่ง’่‰ฒ ID ${studentRole.id}:`); perms.forEach(p => console.log(` - ${p.permission.code}: ${p.permission.name}`)); } console.log('\nโœ… ้…็ฝฎๅฎŒๆˆ๏ผ'); } main() .catch(console.error) .finally(() => prisma.$disconnect());