Files
dsc-api/src/modules/task-manager/providers/task-phase.service.ts
T

44 lines
1.6 KiB
TypeScript

import { Injectable, NotFoundException } from "@nestjs/common";
import { TaskPhaseRepository } from "../repositories/task-phase.repository";
import { TMTaskPhase } from "../entities/task-phase.entity";
import { CreateTaskPhaseDto } from "../dto/task-phase/create-task-phase.dto";
import { UpdateTaskPhaseDto } from "../dto/task-phase/update-task-phase.dto";
import { TaskPhaseMessage } from "../../../common/enums/message.enum";
import { ProjectService } from "./project.service";
@Injectable()
export class TaskPhaseService {
constructor(
private readonly taskPhaseRepository: TaskPhaseRepository,
private readonly projectService: ProjectService
) {}
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;
}
async create(dto: CreateTaskPhaseDto): Promise<TMTaskPhase> {
await this.projectService.findOneOrFail(dto.projectId);
const taskPhase = this.taskPhaseRepository.create(dto);
return this.taskPhaseRepository.save(taskPhase);
}
async update(id: string, dto: UpdateTaskPhaseDto): Promise<TMTaskPhase> {
const taskPhase = await this.findOneOrFail(id);
if (dto.projectId) {
await this.projectService.findOneOrFail(dto.projectId);
}
Object.assign(taskPhase, dto);
return this.taskPhaseRepository.save(taskPhase);
}
async remove(id: string): Promise<void> {
await this.findOneOrFail(id);
await this.taskPhaseRepository.delete(id);
}
}