fix: fixed the update func in workspace service to include same name checking

This commit is contained in:
2026-07-05 17:12:30 +03:30
parent 47a5a2abb4
commit a14bd490be
3 changed files with 27 additions and 11 deletions
@@ -1,6 +1,6 @@
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from "typeorm";
import { In, Not, Repository } from "typeorm";
import { WorkspaceRepository } from "../repositories/workspace.repository";
import { TMWorkspace } from "../entities/workspace.entity";
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
@@ -17,22 +17,20 @@ export class WorkspaceService {
) {}
findAll(): Promise<TMWorkspace[]> {
return this.workspaceRepository.find({
relations: ["projects", "users"],
});
return this.workspaceRepository.find({});
}
async findOneOrFail(id: string): Promise<TMWorkspace> {
const workspace = await this.workspaceRepository.findOne({ where: { id } });
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
const workspace = await this.workspaceRepository.findOneWorkSpace(id);
if (!workspace) throw new NotFoundException(WorkSpaceMessage.WORKSPACE_NOT_FOUND);
return workspace;
}
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
const { userIds, ...rest } = dto;
const existingWorkspace = await this.workspaceRepository.findOne({where: {name: rest.name}});
if(existingWorkspace) {
const existingWorkspace = await this.workspaceRepository.findOne({ where: { name: rest.name } });
if (existingWorkspace) {
throw new BadRequestException(WorkSpaceMessage.WORKSPACE_EXISTS);
}
@@ -49,6 +47,11 @@ export class WorkspaceService {
const workspace = await this.findOneOrFail(id);
const { userIds, ...rest } = dto;
if (rest.name) {
const existingWorkSpaceWithTheSameName = await this.workspaceRepository.findOne({ where: { name: rest.name, id: Not(id) } });
if (existingWorkSpaceWithTheSameName) throw new BadRequestException(WorkSpaceMessage.WORKSPACE_SAME_NAME_EXISTS);
}
Object.assign(workspace, rest);
if (userIds) {