import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { console.log('šŸ” ę£€ęŸ„ēŽ°ęœ‰ homework ꝃ限...'); // čŽ·å–ę‰€ęœ‰ē§Ÿęˆ· const tenants = await prisma.tenant.findMany({ select: { id: true, name: true } }); console.log(`ę‰¾åˆ° ${tenants.length} 个租户`); // å®šä¹‰ä½œäøšē®”ē†ęƒé™ const homeworkPermissions = [ { code: 'homework:read', name: 'ęŸ„ēœ‹ä½œäøš', resource: 'homework', action: 'read' }, { code: 'homework:create', name: 'åˆ›å»ŗä½œäøš', resource: 'homework', action: 'create' }, { code: 'homework:update', name: 'ē¼–č¾‘ä½œäøš', resource: 'homework', action: 'update' }, { code: 'homework:delete', name: 'åˆ é™¤ä½œäøš', resource: 'homework', action: 'delete' }, ]; for (const tenant of tenants) { console.log(`\nšŸ“ 为租户 "${tenant.name}" (ID: ${tenant.id}) ę·»åŠ ęƒé™...`); for (const perm of homeworkPermissions) { // ę£€ęŸ„ęƒé™ę˜Æå¦å·²å­˜åœØ const existing = await prisma.permission.findFirst({ where: { tenantId: tenant.id, code: perm.code, } }); if (existing) { console.log(` ā­ļø ${perm.name} (${perm.code}) 已存在`); } else { await prisma.permission.create({ data: { tenantId: tenant.id, code: perm.code, name: perm.name, resource: perm.resource, action: perm.action, description: `ä½œäøšē®”ē† - ${perm.name}`, } }); console.log(` āœ… ${perm.name} (${perm.code}) å·²åˆ›å»ŗ`); } } // čŽ·å–ē®”ē†å‘˜č§’č‰²å¹¶åˆ†é…ęƒé™ const adminRole = await prisma.role.findFirst({ where: { tenantId: tenant.id, code: 'admin', } }); if (adminRole) { console.log(`\nšŸ” äøŗē®”ē†å‘˜č§’č‰²åˆ†é…ęƒé™...`); const allHomeworkPerms = await prisma.permission.findMany({ where: { tenantId: tenant.id, resource: 'homework', } }); for (const perm of allHomeworkPerms) { const existing = await prisma.rolePermission.findFirst({ where: { roleId: adminRole.id, permissionId: perm.id, } }); if (!existing) { await prisma.rolePermission.create({ data: { roleId: adminRole.id, permissionId: perm.id, } }); console.log(` āœ… 已分配 ${perm.code} ē»™ē®”ē†å‘˜č§’č‰²`); } else { console.log(` ā­ļø ${perm.code} 已分配`); } } } } console.log('\nāœ… ęƒé™é…ē½®å®Œęˆļ¼'); } main() .catch(console.error) .finally(() => prisma.$disconnect());