feat: added get workspaces by user and some refactors
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user