refactor: refactored task APIs

This commit is contained in:
2026-07-06 15:21:34 +03:30
parent 263ede68b2
commit da71c20d6d
6 changed files with 75 additions and 78 deletions
+7 -1
View File
@@ -933,5 +933,11 @@ export const enum ProjectMessage {
} }
export const enum TaskPhaseMessage { export const enum TaskPhaseMessage {
TASK_PHASE_NOT_FOUND = "فاز تسک مورد نظر پیدا نشد!" TASK_PHASE_NOT_FOUND = "فاز تسک مورد نظر پیدا نشد!",
TASK_PHASE_NOT_SAME_PROJECT = "فاز تسک جدید با فاز تسک قبلی در یک پروژه نیستند!"
}
export const enum TaskMessage {
TASK_NOT_FOUND = "تسک مورد نظر پیدا نشد!",
USERS_ASSIGNED_TO_TASK_NOT_PROJECT_MEMBER = "حداقل یکی از کاربران تعیین شده عضو پروژه نیستند!"
} }
@@ -32,7 +32,7 @@ export class TaskPhaseController {
@ApiResponse({ status: 200, description: "TaskPhase found", type: TMTaskPhase }) @ApiResponse({ status: 200, description: "TaskPhase found", type: TMTaskPhase })
@ApiResponse({ status: 404, description: "TaskPhase not found" }) @ApiResponse({ status: 404, description: "TaskPhase not found" })
findOne(@Param("id") id: string): Promise<TMTaskPhase> { findOne(@Param("id") id: string): Promise<TMTaskPhase> {
return this.taskPhaseService.findOne(id); return this.taskPhaseService.findOneOrFail(id);
} }
@Post() @Post()
@@ -26,7 +26,7 @@ export class TaskController {
@ApiResponse({ status: 200, description: 'Task found', type: TMTask }) @ApiResponse({ status: 200, description: 'Task found', type: TMTask })
@ApiResponse({ status: 404, description: 'Task not found' }) @ApiResponse({ status: 404, description: 'Task not found' })
findOne(@Param('id') id: string): Promise<TMTask> { findOne(@Param('id') id: string): Promise<TMTask> {
return this.taskService.findOne(id); return this.taskService.findOneOrFail(id);
} }
@Post() @Post()
@@ -23,7 +23,7 @@ export class TaskPhaseService {
}); });
} }
async findOne(id: string): Promise<TMTaskPhase> { async findOneOrFail(id: string): Promise<TMTaskPhase> {
const taskPhase = await this.taskPhaseRepository.findOne({where: {id}}); const taskPhase = await this.taskPhaseRepository.findOne({where: {id}});
if (!taskPhase) throw new NotFoundException(TaskPhaseMessage.TASK_PHASE_NOT_FOUND); if (!taskPhase) throw new NotFoundException(TaskPhaseMessage.TASK_PHASE_NOT_FOUND);
return taskPhase; return taskPhase;
@@ -36,7 +36,7 @@ export class TaskPhaseService {
} }
async update(id: string, dto: UpdateTaskPhaseDto): Promise<TMTaskPhase> { async update(id: string, dto: UpdateTaskPhaseDto): Promise<TMTaskPhase> {
const taskPhase = await this.findOne(id); const taskPhase = await this.findOneOrFail(id);
if (dto.projectId) { if (dto.projectId) {
await this.projectService.findOneOrFail(dto.projectId); await this.projectService.findOneOrFail(dto.projectId);
@@ -47,7 +47,7 @@ export class TaskPhaseService {
} }
async remove(id: string): Promise<void> { async remove(id: string): Promise<void> {
await this.findOne(id); await this.findOneOrFail(id);
await this.taskPhaseRepository.delete(id); await this.taskPhaseRepository.delete(id);
} }
} }
@@ -1,13 +1,15 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from 'typeorm'; import { In, Repository } from "typeorm";
import { TaskRepository } from '../repositories/task.repository'; import { TaskRepository } from "../repositories/task.repository";
import { TaskPhaseRepository } from '../repositories/task-phase.repository'; import { TMTask } from "../entities/task.entity";
import { TMTask } from '../entities/task.entity'; import { CreateTaskDto } from "../dto/task/create-task.dto";
import { CreateTaskDto } from '../dto/task/create-task.dto'; import { UpdateTaskDto } from "../dto/task/update-task.dto";
import { UpdateTaskDto } from '../dto/task/update-task.dto'; import { ChangeTaskPhaseDto } from "../dto/task/change-task-phase.dto";
import { ChangeTaskPhaseDto } from '../dto/task/change-task-phase.dto'; import { User } from "../../users/entities/user.entity";
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";
@Injectable() @Injectable()
export class TaskService { export class TaskService {
@@ -18,25 +20,50 @@ export class TaskService {
private readonly userRepository: Repository<User>, private readonly userRepository: Repository<User>,
) {} ) {}
findAll(): Promise<TMTask[]> { async findAll(): Promise<TMTask[]> {
return this.taskRepository.findAll(); return this.taskRepository.find({
relations: ["taskPhase", "remarks", "attachments", "checkListItems", "users"],
});
} }
findByPhase(taskPhaseId: string): Promise<TMTask[]> { async findByPhase(taskPhaseId: string): Promise<TMTask[]> {
return this.taskRepository.findByPhase(taskPhaseId); return this.taskRepository.find({ where: { taskPhaseId } });
} }
async findOne(id: string): Promise<TMTask> { async findOneOrFail(id: string): Promise<TMTask> {
const task = await this.taskRepository.findOneById(id); const task = await this.taskRepository.findOne({
if (!task) throw new NotFoundException(`Task #${id} not found`); where: { id },
relations: {
taskPhase: true,
},
});
if (!task) throw new NotFoundException(TaskMessage.TASK_NOT_FOUND);
return task; return task;
} }
async findTaskPhaseOrFail(taskPhaseId: string): Promise<TMTaskPhase> {
const taskPhase = await this.taskPhaseRepository.findOne({
where: { id: taskPhaseId },
relations: {
project: {
users: true,
},
},
});
if (!taskPhase) throw new NotFoundException(TaskPhaseMessage.TASK_PHASE_NOT_FOUND);
return taskPhase;
}
async create(dto: CreateTaskDto): Promise<TMTask> { async create(dto: CreateTaskDto): Promise<TMTask> {
const { userIds, startDate, endDate, ...rest } = dto; const { userIds, startDate, endDate, ...rest } = dto;
const taskPhase = await this.taskPhaseRepository.findOneById(dto.taskPhaseId); const taskPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
if (!taskPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
if (userIds?.length) {
const isUsersValid = userIds.every((userId) => taskPhase.project.users.some((user) => user.id === userId));
if (!isUsersValid) throw new BadRequestException(TaskMessage.USERS_ASSIGNED_TO_TASK_NOT_PROJECT_MEMBER);
}
const task = this.taskRepository.create({ const task = this.taskRepository.create({
...rest, ...rest,
@@ -52,12 +79,16 @@ export class TaskService {
} }
async update(id: string, dto: UpdateTaskDto): Promise<TMTask> { async update(id: string, dto: UpdateTaskDto): Promise<TMTask> {
const task = await this.findOne(id); const task = await this.findOneOrFail(id);
const { userIds, startDate, endDate, ...rest } = dto; const { userIds, startDate, endDate, ...rest } = dto;
if (dto.taskPhaseId) { if (dto.taskPhaseId) {
const taskPhase = await this.taskPhaseRepository.findOneById(dto.taskPhaseId); const taskPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
if (!taskPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
if (userIds?.length) {
const isUsersValid = userIds.every((userId) => taskPhase.project.users.some((user) => user.id === userId));
if (!isUsersValid) throw new BadRequestException(TaskMessage.USERS_ASSIGNED_TO_TASK_NOT_PROJECT_MEMBER);
}
} }
Object.assign(task, { Object.assign(task, {
@@ -67,24 +98,19 @@ export class TaskService {
}); });
if (userIds) { if (userIds) {
task.users = userIds.length task.users = userIds.length ? await this.userRepository.findBy({ id: In(userIds) }) : [];
? await this.userRepository.findBy({ id: In(userIds) })
: [];
} }
return this.taskRepository.save(task); return this.taskRepository.save(task);
} }
async changePhase(id: string, dto: ChangeTaskPhaseDto): Promise<TMTask> { async changePhase(id: string, dto: ChangeTaskPhaseDto): Promise<TMTask> {
const task = await this.findOne(id); const task = await this.findOneOrFail(id);
const newPhase = await this.taskPhaseRepository.findOneById(dto.taskPhaseId); const newPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
if (!newPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
if (task.taskPhase.projectId !== newPhase.projectId) { if (task.taskPhase.projectId !== newPhase.projectId) {
throw new BadRequestException( throw new BadRequestException(TaskPhaseMessage.TASK_PHASE_NOT_SAME_PROJECT);
`TaskPhase #${dto.taskPhaseId} does not belong to the same project as the current phase`,
);
} }
task.taskPhaseId = dto.taskPhaseId; task.taskPhaseId = dto.taskPhaseId;
@@ -94,7 +120,7 @@ export class TaskService {
} }
async remove(id: string): Promise<void> { async remove(id: string): Promise<void> {
await this.findOne(id); await this.findOneOrFail(id);
await this.taskRepository.delete(id); await this.taskRepository.delete(id);
} }
} }
@@ -4,45 +4,10 @@ import { Repository } from "typeorm";
import { TMTask } from "../entities/task.entity"; import { TMTask } from "../entities/task.entity";
@Injectable() @Injectable()
export class TaskRepository { export class TaskRepository extends Repository<TMTask> {
constructor( constructor(
@InjectRepository(TMTask) @InjectRepository(TMTask) taskRepository: Repository<TMTask>,
private readonly repository: Repository<TMTask>, ) {
) {} super(taskRepository.target, taskRepository.manager, taskRepository.queryRunner)
findAll() {
return this.repository.find({
relations: ["taskPhase", "remarks", "attachments", "checkListItems", "users"],
});
}
findByPhase(taskPhaseId: string) {
return this.repository.find({
where: { taskPhaseId },
relations: ["remarks", "attachments", "checkListItems", "users"],
});
}
findOneById(id: string) {
return this.repository.findOne({
where: { id },
relations: ["taskPhase", "remarks", "attachments", "checkListItems", "users"],
});
}
create(data: Partial<TMTask>) {
return this.repository.create(data);
}
save(task: TMTask) {
return this.repository.save(task);
}
update(id: string, data: Partial<TMTask>) {
return this.repository.update(id, data);
}
delete(id: string) {
return this.repository.delete(id);
} }
} }