feat: added get workspaces by user and some refactors

This commit is contained in:
2026-07-04 17:32:02 +03:30
parent e57dd84ebc
commit c5b2cb93f0
6 changed files with 52 additions and 37 deletions
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from "typeorm";
import { WorkspaceRepository } from "../repositories/workspace.repository";
@@ -6,6 +6,7 @@ import { TMWorkspace } from "../entities/workspace.entity";
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
import { UpdateWorkspaceDto } from "../dto/workspace/update-workspace.dto";
import { User } from "../../users/entities/user.entity";
import { WorkSpaceMessage } from "../../../common/enums/message.enum";
@Injectable()
export class WorkspaceService {
@@ -16,11 +17,13 @@ export class WorkspaceService {
) {}
findAll(): Promise<TMWorkspace[]> {
return this.workspaceRepository.findAll();
return this.workspaceRepository.find({
relations: ["projects", "users"],
});
}
async findOneOrFail(id: string): Promise<TMWorkspace> {
const workspace = await this.workspaceRepository.findOneById(id);
const workspace = await this.workspaceRepository.findOne({ where: { id } });
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
return workspace;
}
@@ -28,6 +31,11 @@ export class WorkspaceService {
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
const { userIds, ...rest } = dto;
const existingWorkspace = await this.workspaceRepository.findOne({where: {name: rest.name}});
if(existingWorkspace) {
throw new BadRequestException(WorkSpaceMessage.WORKSPACE_EXISTS);
}
const workspace = this.workspaceRepository.create(rest);
if (userIds?.length) {
@@ -56,4 +64,8 @@ export class WorkspaceService {
return workspace;
}
async findByUser(userId: string): Promise<TMWorkspace[]> {
return await this.workspaceRepository.findByUser(userId);
}
}