refactore role service

This commit is contained in:
Alex Holliday committed 2026-01-08 18:51:56 +00:00
1 parent 58a77d6aa2
commit 0dd977eeef
4 files changed
+42 -20

No files matched your search

@@ -4,8 +4,12 @@ export interface IRoleRepository {
// create // create
createMany(rolesData: Array<Partial<Role>>): Promise<Role[]>; createMany(rolesData: Array<Partial<Role>>): Promise<Role[]>;
// single fetch // single fetch
findById(roleId: string, orgId: string): Promise<Role | null>; findById(roleId: string): Promise<Role | null>;
findByIdAndOrgId(roleId: string, orgId: string): Promise<Role | null>;
// collection fetch // collection fetch
findAll(orgId: string): Promise<Role[]>;
findAllByScope(orgId: string, scope: string): Promise<Role[]>;
findAllByTeam(orgId: string, teamId: string): Promise<Role[]>;
// update // update
// delete // delete
deleteMany(roleIds: string[]): Promise<number>; deleteMany(roleIds: string[]): Promise<number>;
@@ -32,13 +32,17 @@ class MongoRoleRepository implements IRoleRepository {
}); });
}; };
findById = async (roleId: string, orgId: string) => { findById = async (roleId: string): Promise<RoleEntity | null> => {
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({ const role = await Role.findOne({
_id: roleId, _id: roleId,
organizationId: orgId, organizationId: orgId,
}) });
.lean<IRole>()
.exec();
if (!role) { if (!role) {
return null; return null;
@@ -46,6 +50,26 @@ class MongoRoleRepository implements IRoleRepository {
return this.toEntity(role); 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[]) => { deleteMany = async (roleIds: string[]) => {
const result = await Role.deleteMany({ _id: { $in: roleIds } }); const result = await Role.deleteMany({ _id: { $in: roleIds } });
return result.deletedCount ?? 0; return result.deletedCount ?? 0;
+1 -1
View File
@@ -351,7 +351,7 @@ class AuthService implements IAuthService {
}; };
}); });
const orgRole = await this.roleRepository.findById( const orgRole = await this.roleRepository.findByIdAndOrgId(
orgMembership.roleId || "", orgMembership.roleId || "",
org.id org.id
); );
+8 -14
View File
@@ -1,12 +1,12 @@
import { Role, IRole } from "@/db/models/index.js";
import type { IRoleRepository } from "@/repositories/index.js"; import type { IRoleRepository } from "@/repositories/index.js";
import ApiError from "@/utils/ApiError.js"; import ApiError from "@/utils/ApiError.js";
import { Role as RoleEntity } from "@/types/domain/index.js";
const SERVICE_NAME = "RoleService"; const SERVICE_NAME = "RoleService";
export interface IRoleService { export interface IRoleService {
getAll: (orgId: string, type: string) => Promise<Partial<IRole[]>>; getAll: (orgId: string, type: string) => Promise<Partial<RoleEntity[]>>;
getTeam: (orgId: string, teamId: string) => Promise<Partial<IRole[]>>; getTeam: (orgId: string, teamId: string) => Promise<Partial<RoleEntity[]>>;
get: (roleId: string) => Promise<IRole>; get: (roleId: string) => Promise<RoleEntity>;
} }
class RoleService implements IRoleService { class RoleService implements IRoleService {
@@ -20,23 +20,17 @@ class RoleService implements IRoleService {
getAll = async (orgId: string, type: string) => { getAll = async (orgId: string, type: string) => {
if (!type) { if (!type) {
return await Role.find({ organizationId: orgId }); return await this.roleRepository.findAll(orgId);
} }
const roles = await Role.find({ organizationId: orgId, scope: type }); return await this.roleRepository.findAllByScope(orgId, type);
return roles;
}; };
getTeam = async (orgId: string, teamId: string) => { getTeam = async (orgId: string, teamId: string) => {
const roles = await Role.find({ return await this.roleRepository.findAllByTeam(orgId, teamId);
organizationId: orgId,
teamId,
scope: "team",
});
return roles;
}; };
get = async (roleId: string) => { get = async (roleId: string) => {
const role = await Role.findById(roleId); const role = await this.roleRepository.findById(roleId);
if (!role) { if (!role) {
throw new ApiError("Role not found", 404); throw new ApiError("Role not found", 404);
} }