diff --git a/src/modules/task-manager/providers/workspace-type.service.ts b/src/modules/task-manager/providers/workspace-type.service.ts new file mode 100644 index 0000000..d2502dc --- /dev/null +++ b/src/modules/task-manager/providers/workspace-type.service.ts @@ -0,0 +1,51 @@ +import { Injectable, NotFoundException, ConflictException } from "@nestjs/common"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Repository } from "typeorm"; +import { TMWorkspaceType } from "../entities/workspace-type.entity"; +import { CreateWorkspaceTypeDto } from "../dto/workspace-type/create-workspace-type.dto"; +import { UpdateWorkspaceTypeDto } from "../dto/workspace-type/update-workspace-type.dto"; + +@Injectable() +export class WorkspaceTypeService { + constructor( + @InjectRepository(TMWorkspaceType) + private readonly workspaceTypeRepository: Repository, + ) {} + + findAll(): Promise { + return this.workspaceTypeRepository.find({ + order: { order: "ASC" }, + }); + } + + async findOne(id: string): Promise { + const workspaceType = await this.workspaceTypeRepository.findOne({ where: { id } }); + if (!workspaceType) throw new NotFoundException(`WorkspaceType #${id} not found`); + return workspaceType; + } + + async create(dto: CreateWorkspaceTypeDto): Promise { + const existing = await this.workspaceTypeRepository.findOne({ where: { name: dto.name } }); + if (existing) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`); + + const workspaceType = this.workspaceTypeRepository.create(dto); + return this.workspaceTypeRepository.save(workspaceType); + } + + async update(id: string, dto: UpdateWorkspaceTypeDto): Promise { + await this.findOne(id); + + if (dto.name) { + const existing = await this.workspaceTypeRepository.findOne({ where: { name: dto.name } }); + if (existing && existing.id !== id) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`); + } + + await this.workspaceTypeRepository.update(id, dto); + return this.findOne(id); + } + + async remove(id: string): Promise { + await this.findOne(id); + await this.workspaceTypeRepository.delete(id); + } +}