fix: fixed the update func in workspace service to include same name checking
This commit is contained in:
@@ -919,7 +919,8 @@ export const enum AccessLogMessage {
|
|||||||
export const enum WorkSpaceMessage {
|
export const enum WorkSpaceMessage {
|
||||||
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!",
|
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!",
|
||||||
WORKSPACE_NOT_FOUND = "فضای کار مورد نظر شما وجود ندارد!",
|
WORKSPACE_NOT_FOUND = "فضای کار مورد نظر شما وجود ندارد!",
|
||||||
USERS_DONT_BELONG_TO_THIS_WORKSPACE = "کاربران تعیین شده برای این فضای کاری نیستند!"
|
USERS_DONT_BELONG_TO_THIS_WORKSPACE = "کاربران تعیین شده برای این فضای کاری نیستند!",
|
||||||
|
WORKSPACE_SAME_NAME_EXISTS = "فضای کاری با این اسم از قبل وجود دارد!"
|
||||||
}
|
}
|
||||||
|
|
||||||
export const enum ProjectMessage {
|
export const enum ProjectMessage {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
import { In, Repository } from "typeorm";
|
import { In, Not, Repository } from "typeorm";
|
||||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||||
import { TMWorkspace } from "../entities/workspace.entity";
|
import { TMWorkspace } from "../entities/workspace.entity";
|
||||||
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
|
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
|
||||||
@@ -17,14 +17,12 @@ export class WorkspaceService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAll(): Promise<TMWorkspace[]> {
|
findAll(): Promise<TMWorkspace[]> {
|
||||||
return this.workspaceRepository.find({
|
return this.workspaceRepository.find({});
|
||||||
relations: ["projects", "users"],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
||||||
const workspace = await this.workspaceRepository.findOne({ where: { id } });
|
const workspace = await this.workspaceRepository.findOneWorkSpace(id);
|
||||||
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
|
if (!workspace) throw new NotFoundException(WorkSpaceMessage.WORKSPACE_NOT_FOUND);
|
||||||
return workspace;
|
return workspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +47,11 @@ export class WorkspaceService {
|
|||||||
const workspace = await this.findOneOrFail(id);
|
const workspace = await this.findOneOrFail(id);
|
||||||
const { userIds, ...rest } = dto;
|
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);
|
Object.assign(workspace, rest);
|
||||||
|
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
import { Repository } from "typeorm";
|
import { Repository } from 'typeorm';
|
||||||
import { TMWorkspace } from "../entities/workspace.entity";
|
import { TMWorkspace } from '../entities/workspace.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkspaceRepository extends Repository<TMWorkspace> {
|
export class WorkspaceRepository extends Repository<TMWorkspace> {
|
||||||
@@ -9,6 +9,18 @@ export class WorkspaceRepository extends Repository<TMWorkspace> {
|
|||||||
super(workspaceRepository.target, workspaceRepository.manager, workspaceRepository.queryRunner);
|
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[]> {
|
async findByUser(userId: string): Promise<TMWorkspace[]> {
|
||||||
return this.createQueryBuilder("workspace")
|
return this.createQueryBuilder("workspace")
|
||||||
.innerJoin("workspace.users", "users")
|
.innerJoin("workspace.users", "users")
|
||||||
|
|||||||
Reference in New Issue
Block a user