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
createMany(rolesData: Array<Partial<Role>>): Promise<Role[]>;
// 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
findAll(orgId: string): Promise<Role[]>;
findAllByScope(orgId: string, scope: string): Promise<Role[]>;
findAllByTeam(orgId: string, teamId: string): Promise<Role[]>;
// update
// delete
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({
_id: roleId,
organizationId: orgId,
})
.lean<IRole>()
.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;
+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 || "",
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 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<Partial<IRole[]>>;
getTeam: (orgId: string, teamId: string) => Promise<Partial<IRole[]>>;
get: (roleId: string) => Promise<IRole>;
getAll: (orgId: string, type: string) => Promise<Partial<RoleEntity[]>>;
getTeam: (orgId: string, teamId: string) => Promise<Partial<RoleEntity[]>>;
get: (roleId: string) => Promise<RoleEntity>;
}
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);
}