refactor: removed unnecessary functions from task
This commit is contained in:
@@ -10,6 +10,7 @@ import { User } from "../../users/entities/user.entity";
|
||||
import { TaskMessage, TaskPhaseMessage } from "../../../common/enums/message.enum";
|
||||
import { TaskPhaseRepository } from "../repositories/task-phase.repository";
|
||||
import { TMTaskPhase } from "../entities/task-phase.entity";
|
||||
import { PermissionEnum } from "../../users/enums/permission.enum";
|
||||
|
||||
@Injectable()
|
||||
export class TaskService {
|
||||
@@ -20,22 +21,12 @@ export class TaskService {
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
async findAll(): Promise<TMTask[]> {
|
||||
return this.taskRepository.find({
|
||||
relations: ["taskPhase", "remarks", "attachments", "checkListItems", "users"],
|
||||
});
|
||||
}
|
||||
|
||||
async findByPhase(taskPhaseId: string): Promise<TMTask[]> {
|
||||
return this.taskRepository.find({ where: { taskPhaseId } });
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<TMTask> {
|
||||
const task = await this.taskRepository.findOne({
|
||||
where: { id },
|
||||
relations: {
|
||||
taskPhase: true,
|
||||
users: true
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
if (!task) throw new NotFoundException(TaskMessage.TASK_NOT_FOUND);
|
||||
@@ -125,11 +116,12 @@ export class TaskService {
|
||||
await this.taskRepository.delete(id);
|
||||
}
|
||||
|
||||
async getTaskDetail(userId: string, taskId: string): Promise<TMTask | null> {
|
||||
async getTaskDetail(userId: string, permissions: PermissionEnum[], taskId: string): Promise<TMTask | null> {
|
||||
const task = await this.findOneOrFail(taskId);
|
||||
|
||||
const isTaskMember = task.users.some((user) => user.id === userId);
|
||||
if(!isTaskMember) throw new ForbiddenException(TaskMessage.USER_NOT_TASK_MEMBER);
|
||||
const hasPermission = permissions.includes(PermissionEnum.TASK);
|
||||
if (!isTaskMember && !hasPermission) throw new ForbiddenException(TaskMessage.USER_NOT_TASK_MEMBER);
|
||||
|
||||
const taskDetail = await this.taskRepository.getTaskDetail(taskId);
|
||||
return taskDetail;
|
||||
|
||||
Reference in New Issue
Block a user