refactor: refactored remark entirely
This commit is contained in:
@@ -941,4 +941,8 @@ export const enum TaskMessage {
|
||||
TASK_NOT_FOUND = "تسک مورد نظر پیدا نشد!",
|
||||
USERS_ASSIGNED_TO_TASK_NOT_PROJECT_MEMBER = "حداقل یکی از کاربران تعیین شده عضو پروژه نیستند!",
|
||||
USER_NOT_TASK_MEMBER = "شما عضو این تسک نیستید!"
|
||||
}
|
||||
|
||||
export const enum remarkMessage {
|
||||
REMARK_NOT_FOUND = "برچسب مورد نظر پیدا نشد!"
|
||||
}
|
||||
@@ -4,7 +4,14 @@ import { RemarkService } from "../providers/remark.service";
|
||||
import { CreateRemarkDto } from "../dto/remark/create-remark.dto";
|
||||
import { UpdateRemarkDto } from "../dto/remark/update-remark.dto";
|
||||
import { TMRemark } from "../entities/remark.entity";
|
||||
import { AuthGuards } from "../../../common/decorators/auth-guard.decorator";
|
||||
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
||||
import { PermissionsDec } from "../../../common/decorators/permission.decorator";
|
||||
import { PermissionEnum } from "../../users/enums/permission.enum";
|
||||
|
||||
@AuthGuards()
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.REMARK)
|
||||
@ApiTags("Task Manager - Remarks")
|
||||
@Controller("task-manager/remarks")
|
||||
export class RemarkController {
|
||||
@@ -25,7 +32,7 @@ export class RemarkController {
|
||||
@ApiResponse({ status: 200, description: "Remark found", type: TMRemark })
|
||||
@ApiResponse({ status: 404, description: "Remark not found" })
|
||||
findOne(@Param("id") id: string): Promise<TMRemark> {
|
||||
return this.remarkService.findOne(id);
|
||||
return this.remarkService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
|
||||
@@ -1,45 +1,49 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { RemarkRepository } from "../repositories/remark.repository";
|
||||
import { TaskRepository } from "../repositories/task.repository";
|
||||
import { TMRemark } from "../entities/remark.entity";
|
||||
import { CreateRemarkDto } from "../dto/remark/create-remark.dto";
|
||||
import { UpdateRemarkDto } from "../dto/remark/update-remark.dto";
|
||||
import { remarkMessage } from "../../../common/enums/message.enum";
|
||||
import { TaskService } from './task.service';
|
||||
|
||||
@Injectable()
|
||||
export class RemarkService {
|
||||
constructor(
|
||||
private readonly remarkRepository: RemarkRepository,
|
||||
private readonly taskRepository: TaskRepository,
|
||||
private readonly taskService: TaskService,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findAll();
|
||||
return this.remarkRepository.find({ relations: { task: true } });
|
||||
}
|
||||
|
||||
findByTask(taskId: string): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findByTask(taskId);
|
||||
return this.remarkRepository.find({
|
||||
where: { taskId },
|
||||
relations: {
|
||||
task: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMRemark> {
|
||||
const remark = await this.remarkRepository.findOneById(id);
|
||||
if (!remark) throw new NotFoundException(`Remark #${id} not found`);
|
||||
async findOneOrFail(id: string): Promise<TMRemark> {
|
||||
const remark = await this.remarkRepository.findOne({ where: { id } });
|
||||
if (!remark) throw new NotFoundException(remarkMessage.REMARK_NOT_FOUND);
|
||||
return remark;
|
||||
}
|
||||
|
||||
async create(dto: CreateRemarkDto): Promise<TMRemark> {
|
||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
||||
await this.taskService.findOneOrFail(dto.taskId);
|
||||
|
||||
const remark = this.remarkRepository.create(dto);
|
||||
return this.remarkRepository.save(remark);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateRemarkDto): Promise<TMRemark> {
|
||||
const remark = await this.findOne(id);
|
||||
const remark = await this.findOneOrFail(id);
|
||||
|
||||
if (dto.taskId) {
|
||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
||||
await this.taskService.findOneOrFail(dto.taskId);
|
||||
}
|
||||
|
||||
Object.assign(remark, dto);
|
||||
@@ -47,7 +51,7 @@ export class RemarkService {
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.findOneOrFail(id);
|
||||
await this.remarkRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,44 +4,10 @@ import { Repository } from "typeorm";
|
||||
import { TMRemark } from "../entities/remark.entity";
|
||||
|
||||
@Injectable()
|
||||
export class RemarkRepository {
|
||||
export class RemarkRepository extends Repository<TMRemark>{
|
||||
constructor(
|
||||
@InjectRepository(TMRemark)
|
||||
private readonly repository: Repository<TMRemark>,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find({
|
||||
relations: ["task"],
|
||||
});
|
||||
}
|
||||
|
||||
findByTask(taskId: string) {
|
||||
return this.repository.find({
|
||||
where: { taskId },
|
||||
});
|
||||
}
|
||||
|
||||
findOneById(id: string) {
|
||||
return this.repository.findOne({
|
||||
where: { id },
|
||||
relations: ["task"],
|
||||
});
|
||||
}
|
||||
|
||||
create(data: Partial<TMRemark>) {
|
||||
return this.repository.create(data);
|
||||
}
|
||||
|
||||
save(remark: TMRemark) {
|
||||
return this.repository.save(remark);
|
||||
}
|
||||
|
||||
update(id: string, data: Partial<TMRemark>) {
|
||||
return this.repository.update(id, data);
|
||||
}
|
||||
|
||||
delete(id: string) {
|
||||
return this.repository.delete(id);
|
||||
@InjectRepository(TMRemark) remarkRepository: Repository<TMRemark>,
|
||||
) {
|
||||
super(remarkRepository.target, remarkRepository.manager, remarkRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,5 +28,6 @@ export enum PermissionEnum {
|
||||
WORKSPACE="workspace",
|
||||
PROJECT="project",
|
||||
TASK_PHASE="task_phase",
|
||||
TASK="task"
|
||||
TASK="task",
|
||||
REMARK="remark"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user