mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
|
|
import {inject, injectable} from "tsyringe";
|
||
|
|
import {type CreateUserRole, UserRolesRepository} from "$lib/server/api/repositories/user_roles.repository";
|
||
|
|
import db from "$db";
|
||
|
|
import {eq} from "drizzle-orm";
|
||
|
|
import {roles, userRoles} from "$db/schema";
|
||
|
|
import {RolesService} from "$lib/server/api/services/roles.service";
|
||
|
|
|
||
|
|
@injectable()
|
||
|
|
export class UserRolesService {
|
||
|
|
constructor(
|
||
|
|
@inject(UserRolesRepository) private readonly userRolesRepository: UserRolesRepository,
|
||
|
|
@inject(RolesService) private readonly rolesService: RolesService
|
||
|
|
) { }
|
||
|
|
|
||
|
|
async findOneById(id: string) {
|
||
|
|
return this.userRolesRepository.findOneById(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
async findAllByUserId(userId: string) {
|
||
|
|
return this.userRolesRepository.findAllByUserId(userId);
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(data: CreateUserRole) {
|
||
|
|
return this.userRolesRepository.create(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
async addRoleToUser(userId: string, roleName: string, primary = false) {
|
||
|
|
// Find the role by its name
|
||
|
|
const role = await this.rolesService.findOneByNameOrThrow(roleName);
|
||
|
|
|
||
|
|
if (!role || !role.id) {
|
||
|
|
throw new Error(`Role with name ${roleName} not found`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create a UserRole entry linking the user and the role
|
||
|
|
return db.insert(userRoles).values({
|
||
|
|
user_id: userId,
|
||
|
|
role_id: role.id,
|
||
|
|
primary,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|