refactor: refactored task APIs
This commit is contained in:
@@ -933,5 +933,11 @@ export const enum ProjectMessage {
|
||||
}
|
||||
|
||||
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: 404, description: "TaskPhase not found" })
|
||||
findOne(@Param("id") id: string): Promise<TMTaskPhase> {
|
||||
return this.taskPhaseService.findOne(id);
|
||||
return this.taskPhaseService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
|
||||
@@ -26,7 +26,7 @@ export class TaskController {
|
||||
@ApiResponse({ status: 200, description: 'Task found', type: TMTask })
|
||||
@ApiResponse({ status: 404, description: 'Task not found' })
|
||||
findOne(@Param('id') id: string): Promise<TMTask> {
|
||||
return this.taskService.findOne(id);
|
||||
return this.taskService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@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}});
|
||||
if (!taskPhase) throw new NotFoundException(TaskPhaseMessage.TASK_PHASE_NOT_FOUND);
|
||||
return taskPhase;
|
||||
@@ -36,7 +36,7 @@ export class TaskPhaseService {
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateTaskPhaseDto): Promise<TMTaskPhase> {
|
||||
const taskPhase = await this.findOne(id);
|
||||
const taskPhase = await this.findOneOrFail(id);
|
||||
|
||||
if (dto.projectId) {
|
||||
await this.projectService.findOneOrFail(dto.projectId);
|
||||
@@ -47,7 +47,7 @@ export class TaskPhaseService {
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.findOneOrFail(id);
|
||||
await this.taskPhaseRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { TaskRepository } from '../repositories/task.repository';
|
||||
import { TaskPhaseRepository } from '../repositories/task-phase.repository';
|
||||
import { TMTask } from '../entities/task.entity';
|
||||
import { CreateTaskDto } from '../dto/task/create-task.dto';
|
||||
import { UpdateTaskDto } from '../dto/task/update-task.dto';
|
||||
import { ChangeTaskPhaseDto } from '../dto/task/change-task-phase.dto';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { TaskRepository } from "../repositories/task.repository";
|
||||
import { TMTask } from "../entities/task.entity";
|
||||
import { CreateTaskDto } from "../dto/task/create-task.dto";
|
||||
import { UpdateTaskDto } from "../dto/task/update-task.dto";
|
||||
import { ChangeTaskPhaseDto } from "../dto/task/change-task-phase.dto";
|
||||
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()
|
||||
export class TaskService {
|
||||
@@ -18,25 +20,50 @@ export class TaskService {
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMTask[]> {
|
||||
return this.taskRepository.findAll();
|
||||
async findAll(): Promise<TMTask[]> {
|
||||
return this.taskRepository.find({
|
||||
relations: ["taskPhase", "remarks", "attachments", "checkListItems", "users"],
|
||||
});
|
||||
}
|
||||
|
||||
findByPhase(taskPhaseId: string): Promise<TMTask[]> {
|
||||
return this.taskRepository.findByPhase(taskPhaseId);
|
||||
async findByPhase(taskPhaseId: string): Promise<TMTask[]> {
|
||||
return this.taskRepository.find({ where: { taskPhaseId } });
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMTask> {
|
||||
const task = await this.taskRepository.findOneById(id);
|
||||
if (!task) throw new NotFoundException(`Task #${id} not found`);
|
||||
async findOneOrFail(id: string): Promise<TMTask> {
|
||||
const task = await this.taskRepository.findOne({
|
||||
where: { id },
|
||||
relations: {
|
||||
taskPhase: true,
|
||||
},
|
||||
});
|
||||
if (!task) throw new NotFoundException(TaskMessage.TASK_NOT_FOUND);
|
||||
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> {
|
||||
const { userIds, startDate, endDate, ...rest } = dto;
|
||||
|
||||
const taskPhase = await this.taskPhaseRepository.findOneById(dto.taskPhaseId);
|
||||
if (!taskPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
|
||||
const taskPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
|
||||
|
||||
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({
|
||||
...rest,
|
||||
@@ -52,12 +79,16 @@ export class TaskService {
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (dto.taskPhaseId) {
|
||||
const taskPhase = await this.taskPhaseRepository.findOneById(dto.taskPhaseId);
|
||||
if (!taskPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
|
||||
const taskPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
|
||||
|
||||
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, {
|
||||
@@ -67,24 +98,19 @@ export class TaskService {
|
||||
});
|
||||
|
||||
if (userIds) {
|
||||
task.users = userIds.length
|
||||
? await this.userRepository.findBy({ id: In(userIds) })
|
||||
: [];
|
||||
task.users = userIds.length ? await this.userRepository.findBy({ id: In(userIds) }) : [];
|
||||
}
|
||||
|
||||
return this.taskRepository.save(task);
|
||||
}
|
||||
|
||||
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);
|
||||
if (!newPhase) throw new NotFoundException(`TaskPhase #${dto.taskPhaseId} not found`);
|
||||
const newPhase = await this.findTaskPhaseOrFail(dto.taskPhaseId);
|
||||
|
||||
if (task.taskPhase.projectId !== newPhase.projectId) {
|
||||
throw new BadRequestException(
|
||||
`TaskPhase #${dto.taskPhaseId} does not belong to the same project as the current phase`,
|
||||
);
|
||||
throw new BadRequestException(TaskPhaseMessage.TASK_PHASE_NOT_SAME_PROJECT);
|
||||
}
|
||||
|
||||
task.taskPhaseId = dto.taskPhaseId;
|
||||
@@ -94,7 +120,7 @@ export class TaskService {
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.findOneOrFail(id);
|
||||
await this.taskRepository.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,45 +4,10 @@ import { Repository } from "typeorm";
|
||||
import { TMTask } from "../entities/task.entity";
|
||||
|
||||
@Injectable()
|
||||
export class TaskRepository {
|
||||
export class TaskRepository extends Repository<TMTask> {
|
||||
constructor(
|
||||
@InjectRepository(TMTask)
|
||||
private readonly repository: Repository<TMTask>,
|
||||
) {}
|
||||
|
||||
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);
|
||||
@InjectRepository(TMTask) taskRepository: Repository<TMTask>,
|
||||
) {
|
||||
super(taskRepository.target, taskRepository.manager, taskRepository.queryRunner)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user