42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { prisma } from "../src/constants";
|
|
await (async () => {
|
|
const userIdentifier = Bun.argv[2];
|
|
const roleIdentifier = Bun.argv[3];
|
|
|
|
if (Bun.argv.length !== 4)
|
|
console.error(
|
|
"2 arguments expected. \n Format: bun utils:assign_user_role {user} {role}"
|
|
);
|
|
|
|
const user = await prisma.user.findFirst({
|
|
where: { OR: [{ id: userIdentifier }, { login: userIdentifier }] },
|
|
});
|
|
|
|
if (!user)
|
|
return console.error(
|
|
`User with identifier '${userIdentifier}' doesn't exist`
|
|
);
|
|
|
|
const role = await prisma.role.findFirst({
|
|
where: { OR: [{ id: roleIdentifier }, { moniker: roleIdentifier }] },
|
|
include: { users: true },
|
|
});
|
|
|
|
if (!role)
|
|
return console.error(
|
|
`Role with identifier '${roleIdentifier}' doesn't exist`
|
|
);
|
|
|
|
if (role.users.map((v) => v.id).includes(user.id))
|
|
return console.log("User already has role!");
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: { roles: { set: { id: role.id } } },
|
|
});
|
|
|
|
return console.log(
|
|
`User '${user.login}' has been given role '${role.title}'!`
|
|
);
|
|
})();
|