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
+2 -1
View File
@@ -919,7 +919,8 @@ export const enum AccessLogMessage {
export const enum WorkSpaceMessage {
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!",
WORKSPACE_NOT_FOUND = "فضای کار مورد نظر شما وجود ندارد!",
USERS_DONT_BELONG_TO_THIS_WORKSPACE = "کاربران تعیین شده برای این فضای کاری نیستند!"
USERS_DONT_BELONG_TO_THIS_WORKSPACE = "کاربران تعیین شده برای این فضای کاری نیستند!",
WORKSPACE_SAME_NAME_EXISTS = "فضای کاری با این اسم از قبل وجود دارد!"
}
export const enum ProjectMessage {
@@ -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) {
@@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { TMWorkspace } from "../entities/workspace.entity";
import { Repository } from 'typeorm';
import { TMWorkspace } from '../entities/workspace.entity';
@Injectable()
export class WorkspaceRepository extends Repository<TMWorkspace> {
@@ -9,6 +9,18 @@ export class WorkspaceRepository extends Repository<TMWorkspace> {
super(workspaceRepository.target, workspaceRepository.manager, workspaceRepository.queryRunner);
}
async findOneWorkSpace(workspaceId: string): Promise<TMWorkspace | null> {
return this.createQueryBuilder("workspace")
.leftJoinAndSelect('workspace.users', 'user')
.select([
'workspace',
'user.id',
'user.firstName',
'user.lastName'
]).where('workspace.id = :workspaceId', {workspaceId})
.getOne();
}
async findByUser(userId: string): Promise<TMWorkspace[]> {
return this.createQueryBuilder("workspace")
.innerJoin("workspace.users", "users")