Add user role management script
This commit is contained in:
56
scripts/set-user-role.js
Normal file
56
scripts/set-user-role.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
|
|
||||||
|
import { prisma } from '../src/prisma-client.js';
|
||||||
|
|
||||||
|
const [, , emailArg, roleArg = 'MANAGER'] = process.argv;
|
||||||
|
|
||||||
|
const email = String(emailArg || '').trim().toLowerCase();
|
||||||
|
const role = String(roleArg || '').trim().toUpperCase();
|
||||||
|
const allowedRoles = new Set(['CLIENT', 'MANAGER']);
|
||||||
|
|
||||||
|
if (!email) {
|
||||||
|
throw new Error('Usage: node scripts/set-user-role.js <email> [CLIENT|MANAGER]');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!allowedRoles.has(role)) {
|
||||||
|
throw new Error(`Unsupported role: ${role}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingUser = await prisma.user.findUnique({
|
||||||
|
where: { email },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
role: true,
|
||||||
|
fullName: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingUser) {
|
||||||
|
throw new Error(`User not found: ${email}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedUser = await prisma.user.update({
|
||||||
|
where: { email },
|
||||||
|
data: { role },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
role: true,
|
||||||
|
fullName: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
ok: true,
|
||||||
|
user: updatedUser,
|
||||||
|
previousRole: existingUser.role,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await prisma.$disconnect();
|
||||||
Reference in New Issue
Block a user