mirror of
https://github.com/BradNut/weddingsite
synced 2025-09-08 17:40:36 +00:00
33 lines
562 B
TypeScript
33 lines
562 B
TypeScript
|
|
import prisma from "./db";
|
||
|
|
|
||
|
|
export async function add_user_to_role(user_id: string, role_name: string) {
|
||
|
|
// Find the role by its name
|
||
|
|
const role = await prisma.role.findUnique({
|
||
|
|
where: {
|
||
|
|
name: role_name,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!role) {
|
||
|
|
throw new Error(`Role with name ${role_name} not found`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create a UserRole entry linking the user and the role
|
||
|
|
const userRole = await prisma.userRole.create({
|
||
|
|
data: {
|
||
|
|
user: {
|
||
|
|
connect: {
|
||
|
|
id: user_id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
role: {
|
||
|
|
connect: {
|
||
|
|
id: role.id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return userRole;
|
||
|
|
}
|