refactor: refactored task APIs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user