refactor: removed unnecessary functions from task
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
|
||||
import { Controller, Get, Post, Patch, Delete, Param, Body } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
||||
import { TaskService } from '../providers/task.service';
|
||||
import { CreateTaskDto } from '../dto/task/create-task.dto';
|
||||
import { UpdateTaskDto } from '../dto/task/update-task.dto';
|
||||
@@ -11,6 +11,7 @@ import { UserDec } from '../../../common/decorators/user.decorator';
|
||||
import { ParamDto } from '../../../common/DTO/param.dto';
|
||||
import { PermissionsDec } from '../../../common/decorators/permission.decorator';
|
||||
import { PermissionEnum } from '../../users/enums/permission.enum';
|
||||
import { ITokenPayload } from '../../auth/interfaces/IToken-payload';
|
||||
|
||||
@AuthGuards()
|
||||
@AdminRoute()
|
||||
@@ -19,26 +20,6 @@ import { PermissionEnum } from '../../users/enums/permission.enum';
|
||||
export class TaskController {
|
||||
constructor(private readonly taskService: TaskService) {}
|
||||
|
||||
@Get()
|
||||
@PermissionsDec(PermissionEnum.TASK)
|
||||
@ApiOperation({ summary: 'Get all tasks, optionally filtered by phase' })
|
||||
@ApiQuery({ name: 'taskPhaseId', required: false, description: 'Filter tasks by task phase ID' })
|
||||
@ApiResponse({ status: 200, description: 'List of tasks', type: [TMTask] })
|
||||
findAll(@Query('taskPhaseId') taskPhaseId?: string): Promise<TMTask[]> {
|
||||
if (taskPhaseId) return this.taskService.findByPhase(taskPhaseId);
|
||||
return this.taskService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@PermissionsDec(PermissionEnum.TASK)
|
||||
@ApiOperation({ summary: 'Get a task by ID' })
|
||||
@ApiParam({ name: 'id', description: 'Task ID' })
|
||||
@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.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@PermissionsDec(PermissionEnum.TASK)
|
||||
@ApiOperation({ summary: 'Create a new task' })
|
||||
@@ -85,9 +66,9 @@ export class TaskController {
|
||||
@ApiResponse({ status: 200, description: 'Task detail received!' })
|
||||
@ApiResponse({ status: 404, description: 'Task not found' })
|
||||
getTaskDetail(
|
||||
@UserDec('id') userId: string,
|
||||
@UserDec() user: ITokenPayload,
|
||||
@Param() paramDto: ParamDto
|
||||
): Promise<TMTask | null> {
|
||||
return this.taskService.getTaskDetail(userId, paramDto.id);
|
||||
return this.taskService.getTaskDetail(user.id, user.permissions, paramDto.id);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -22,21 +22,22 @@ export class TaskRepository extends Repository<TMTask> {
|
||||
qb.where("completedCheckListItems.isDone = :isDone", { isDone: true }),
|
||||
)
|
||||
|
||||
.select(["remark.id",
|
||||
"remark.title",
|
||||
"remark.color",
|
||||
"checkListItem.id",
|
||||
"checkListItem.title",
|
||||
"checkListItem.isDone",
|
||||
"attachment.id",
|
||||
"attachment.title",
|
||||
"attachment.type",
|
||||
"attachment.file",
|
||||
|
||||
"user.id",
|
||||
"user.firstName",
|
||||
"user.lastName"
|
||||
])
|
||||
.select([
|
||||
"remark.id",
|
||||
"remark.title",
|
||||
"remark.color",
|
||||
"checkListItem.id",
|
||||
"checkListItem.title",
|
||||
"checkListItem.isDone",
|
||||
"attachment.id",
|
||||
"attachment.title",
|
||||
"attachment.type",
|
||||
"attachment.file",
|
||||
|
||||
"user.id",
|
||||
"user.firstName",
|
||||
"user.lastName",
|
||||
])
|
||||
.where("task.id = :taskId", { taskId })
|
||||
.getOne();
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export enum PermissionEnum {
|
||||
VIEW_ALL_WORKSPACES="view_all_workspaces",
|
||||
PROJECT="project", // create, update, remove
|
||||
VIEW_ALL_PROJECTS="view_all_projects",
|
||||
TASK="task",
|
||||
TASK="task", // read, create, update, remove
|
||||
TASK_PHASE="task_phase",
|
||||
REMARK="remark"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user