import { Injectable, NotFoundException } from '@nestjs/common'; import { CheckListItemRepository } from '../repositories/check-list-item.repository'; import { TMCheckListItem } from '../entities/check-list-item.entity'; import { CreateCheckListItemDto } from '../dto/check-list-item/create-check-list-item.dto'; import { UpdateCheckListItemDto } from '../dto/check-list-item/update-check-list-item.dto'; import { checkListItemMessage } from '../../../common/enums/message.enum'; import { TaskService } from './task.service'; @Injectable() export class CheckListItemService { constructor( private readonly checkListItemRepository: CheckListItemRepository, private readonly taskService: TaskService, ) {} async findOneOrFail(id: string): Promise { const checkListItem = await this.checkListItemRepository.findOneById(id); if (!checkListItem) throw new NotFoundException(checkListItemMessage.CHECKLISTITEM_NOT_FOUND); return checkListItem; } async create(dto: CreateCheckListItemDto): Promise { await this.taskService.findOneOrFail(dto.taskId); const checkListItem = this.checkListItemRepository.create(dto); return this.checkListItemRepository.save(checkListItem); } async update(id: string, dto: UpdateCheckListItemDto): Promise { const checkListItem = await this.findOneOrFail(id); if (dto.taskId) { await this.taskService.findOneOrFail(dto.taskId); } Object.assign(checkListItem, dto); return this.checkListItemRepository.save(checkListItem); } async remove(id: string): Promise { const checkListItem = await this.findOneOrFail(id); await this.checkListItemRepository.delete(id); return checkListItem; } async toggleStatus(id: string) : Promise { const checkListItem = await this.findOneOrFail(id); const isDone = checkListItem.isDone; checkListItem.isDone = !isDone; await this.checkListItemRepository.save(checkListItem); return checkListItem; } }