import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const admin = await prisma.role.findFirst({ where: { code: 'admin' } }); if (!admin) throw new Error('缺少 admin 角色'); await prisma.role.updateMany({ where: { code: 'rd_director' }, data: { dataScope: 'ALL' } }); const users = await prisma.user.findMany({ where: { status: 'ACTIVE', OR: [ { displayName: { contains: '潘兴辉' } }, { employee: { name: { contains: '潘兴辉' } } }, { roles: { some: { role: { code: 'rd_director' } } } }, ], }, select: { id: true, displayName: true, username: true }, }); if (!users.length) throw new Error('没有找到潘兴辉或研发总监账号'); const result = await prisma.userRole.createMany({ data: users.map((u) => ({ userId: u.id, roleId: admin.id })), skipDuplicates: true, }); console.log( `granted admin to: ${users.map((u) => u.displayName).join('、')} (new links ${result.count})`, ); } main() .then(async () => { await prisma.$disconnect(); }) .catch(async (e) => { console.error(e); await prisma.$disconnect(); process.exit(1); });