diff --git a/server/src/repositories/role/IRoleRepository.ts b/server/src/repositories/role/IRoleRepository.ts index 40f4ac8fa..0ca18043c 100644 --- a/server/src/repositories/role/IRoleRepository.ts +++ b/server/src/repositories/role/IRoleRepository.ts @@ -4,8 +4,12 @@ export interface IRoleRepository { // create createMany(rolesData: Array>): Promise; // single fetch - findById(roleId: string, orgId: string): Promise; + findById(roleId: string): Promise; + findByIdAndOrgId(roleId: string, orgId: string): Promise; // collection fetch + findAll(orgId: string): Promise; + findAllByScope(orgId: string, scope: string): Promise; + findAllByTeam(orgId: string, teamId: string): Promise; // update // delete deleteMany(roleIds: string[]): Promise; diff --git a/server/src/repositories/role/MongoRoleRepository.ts b/server/src/repositories/role/MongoRoleRepository.ts index 6882e8e7a..4293b07ca 100644 --- a/server/src/repositories/role/MongoRoleRepository.ts +++ b/server/src/repositories/role/MongoRoleRepository.ts @@ -32,13 +32,17 @@ class MongoRoleRepository implements IRoleRepository { }); }; - findById = async (roleId: string, orgId: string) => { + findById = async (roleId: string): Promise => { + const role = await Role.findById(roleId); + if (!role) return null; + return this.toEntity(role); + }; + + findByIdAndOrgId = async (roleId: string, orgId: string) => { const role = await Role.findOne({ _id: roleId, organizationId: orgId, - }) - .lean() - .exec(); + }); if (!role) { return null; @@ -46,6 +50,26 @@ class MongoRoleRepository implements IRoleRepository { return this.toEntity(role); }; + findAll = async (orgId: string) => { + const res = await Role.find({ organizationId: orgId }); + return res.map(this.toEntity); + }; + + findAllByScope = async (orgId: string, scope: string) => { + const result = await Role.find({ organizationId: orgId, scope }); + return result.map(this.toEntity); + }; + + findAllByTeam = async (orgId: string, teamId: string) => { + const result = await Role.find({ + organizationId: orgId, + teamId, + scope: "team", + }); + + return result.map(this.toEntity); + }; + deleteMany = async (roleIds: string[]) => { const result = await Role.deleteMany({ _id: { $in: roleIds } }); return result.deletedCount ?? 0; diff --git a/server/src/services/business/AuthService.ts b/server/src/services/business/AuthService.ts index 883f0dc65..c727fd3cb 100644 --- a/server/src/services/business/AuthService.ts +++ b/server/src/services/business/AuthService.ts @@ -351,7 +351,7 @@ class AuthService implements IAuthService { }; }); - const orgRole = await this.roleRepository.findById( + const orgRole = await this.roleRepository.findByIdAndOrgId( orgMembership.roleId || "", org.id ); diff --git a/server/src/services/business/RoleService.ts b/server/src/services/business/RoleService.ts index 393e52f89..29c45345e 100644 --- a/server/src/services/business/RoleService.ts +++ b/server/src/services/business/RoleService.ts @@ -1,12 +1,12 @@ -import { Role, IRole } from "@/db/models/index.js"; import type { IRoleRepository } from "@/repositories/index.js"; import ApiError from "@/utils/ApiError.js"; +import { Role as RoleEntity } from "@/types/domain/index.js"; const SERVICE_NAME = "RoleService"; export interface IRoleService { - getAll: (orgId: string, type: string) => Promise>; - getTeam: (orgId: string, teamId: string) => Promise>; - get: (roleId: string) => Promise; + getAll: (orgId: string, type: string) => Promise>; + getTeam: (orgId: string, teamId: string) => Promise>; + get: (roleId: string) => Promise; } class RoleService implements IRoleService { @@ -20,23 +20,17 @@ class RoleService implements IRoleService { getAll = async (orgId: string, type: string) => { if (!type) { - return await Role.find({ organizationId: orgId }); + return await this.roleRepository.findAll(orgId); } - const roles = await Role.find({ organizationId: orgId, scope: type }); - return roles; + return await this.roleRepository.findAllByScope(orgId, type); }; getTeam = async (orgId: string, teamId: string) => { - const roles = await Role.find({ - organizationId: orgId, - teamId, - scope: "team", - }); - return roles; + return await this.roleRepository.findAllByTeam(orgId, teamId); }; get = async (roleId: string) => { - const role = await Role.findById(roleId); + const role = await this.roleRepository.findById(roleId); if (!role) { throw new ApiError("Role not found", 404); }