Files
apollo-backend/scripts/set-user-role.js
2026-04-04 09:41:36 +07:00

57 lines
1.1 KiB
JavaScript

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', 'SUPER_MANAGER']);
if (!email) {
throw new Error('Usage: node scripts/set-user-role.js <email> [CLIENT|MANAGER|SUPER_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();