feat: added get workspaces by user and some refactors
This commit is contained in:
@@ -915,3 +915,7 @@ export const enum UploaderMessage {
|
||||
export const enum AccessLogMessage {
|
||||
ACCESS_LOG_NOT_FOUND = "لاگ دسترسی مورد نظر یافت نشد",
|
||||
}
|
||||
|
||||
export const enum WorkSpaceMessage {
|
||||
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!"
|
||||
}
|
||||
@@ -4,13 +4,22 @@ import { WorkspaceService } from '../providers/workspace.service';
|
||||
import { CreateWorkspaceDto } from '../dto/workspace/create-workspace.dto';
|
||||
import { UpdateWorkspaceDto } from '../dto/workspace/update-workspace.dto';
|
||||
import { TMWorkspace } from '../entities/workspace.entity';
|
||||
// import { AuthGuards } from '../../../common/decorators/auth-guard.decorator';
|
||||
import { AdminRoute } from '../../../common/decorators/admin.decorator';
|
||||
import { PermissionsDec } from '../../../common/decorators/permission.decorator';
|
||||
import { PermissionEnum } from '../../users/enums/permission.enum';
|
||||
import { UserDec } from '../../../common/decorators/user.decorator';
|
||||
import { AuthGuards } from '../../../common/decorators/auth-guard.decorator';
|
||||
|
||||
@ApiTags('Task Manager')
|
||||
@AuthGuards()
|
||||
@AdminRoute()
|
||||
@ApiTags('Task Manager - Workspaces')
|
||||
@Controller('task-manager/workspaces')
|
||||
export class WorkspaceController {
|
||||
constructor(private readonly workspaceService: WorkspaceService) {}
|
||||
|
||||
@Get()
|
||||
@PermissionsDec(PermissionEnum.WORKSPACE)
|
||||
@ApiOperation({ summary: 'Get all workspaces' })
|
||||
@ApiResponse({ status: 200, description: 'List of workspaces', type: [TMWorkspace] })
|
||||
findAll(): Promise<TMWorkspace[]> {
|
||||
@@ -18,6 +27,7 @@ export class WorkspaceController {
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@PermissionsDec(PermissionEnum.WORKSPACE)
|
||||
@ApiOperation({ summary: 'Get a workspace by ID' })
|
||||
@ApiParam({ name: 'id', description: 'Workspace ID' })
|
||||
@ApiResponse({ status: 200, description: 'Workspace found', type: TMWorkspace })
|
||||
@@ -27,6 +37,7 @@ export class WorkspaceController {
|
||||
}
|
||||
|
||||
@Post()
|
||||
@PermissionsDec(PermissionEnum.WORKSPACE)
|
||||
@ApiOperation({ summary: 'Create a new workspace' })
|
||||
@ApiResponse({ status: 201, description: 'Workspace created', type: TMWorkspace })
|
||||
create(@Body() body: CreateWorkspaceDto): Promise<TMWorkspace> {
|
||||
@@ -34,6 +45,7 @@ export class WorkspaceController {
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@PermissionsDec(PermissionEnum.WORKSPACE)
|
||||
@ApiOperation({ summary: 'Update a workspace' })
|
||||
@ApiParam({ name: 'id', description: 'Workspace ID' })
|
||||
@ApiResponse({ status: 200, description: 'Workspace updated', type: TMWorkspace })
|
||||
@@ -43,6 +55,7 @@ export class WorkspaceController {
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@PermissionsDec(PermissionEnum.WORKSPACE)
|
||||
@ApiOperation({ summary: 'Delete a workspace' })
|
||||
@ApiParam({ name: 'id', description: 'Workspace ID' })
|
||||
@ApiResponse({ status: 200, description: 'Workspace deleted' })
|
||||
@@ -50,4 +63,12 @@ export class WorkspaceController {
|
||||
remove(@Param('id') id: string): Promise<TMWorkspace> {
|
||||
return this.workspaceService.remove(id);
|
||||
}
|
||||
|
||||
@Get('/byuser')
|
||||
@ApiOperation({summary: 'Get User WorkSpaces'})
|
||||
@ApiResponse({ status: 200, description: 'Got Workspaces successfully!' })
|
||||
@ApiResponse({ status: 404, description: 'Bad Request!' })
|
||||
findByUser(@UserDec('id') userId: string) {
|
||||
return this.workspaceService.findByUser(userId);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { User } from "../../users/entities/user.entity";
|
||||
|
||||
@Entity("tm_workspaces")
|
||||
export class TMWorkspace extends BaseEntity {
|
||||
@Column({ type: "varchar", length: 100 })
|
||||
@Column({ type: "varchar", length: 100, unique: true })
|
||||
name: string;
|
||||
|
||||
@Column({ type: "text", nullable: true })
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,38 +4,15 @@ import { Repository } from "typeorm";
|
||||
import { TMWorkspace } from "../entities/workspace.entity";
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceRepository {
|
||||
constructor(
|
||||
@InjectRepository(TMWorkspace)
|
||||
private readonly repository: Repository<TMWorkspace>,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find({
|
||||
relations: ["projects", "users"],
|
||||
});
|
||||
export class WorkspaceRepository extends Repository<TMWorkspace> {
|
||||
constructor(@InjectRepository(TMWorkspace) workspaceRepository: Repository<TMWorkspace>) {
|
||||
super(workspaceRepository.target, workspaceRepository.manager, workspaceRepository.queryRunner);
|
||||
}
|
||||
|
||||
findOneById(id: string) {
|
||||
return this.repository.findOne({
|
||||
where: { id },
|
||||
relations: ["projects", "users"],
|
||||
});
|
||||
}
|
||||
|
||||
create(data: Partial<TMWorkspace>) {
|
||||
return this.repository.create(data);
|
||||
}
|
||||
|
||||
save(workspace: TMWorkspace) {
|
||||
return this.repository.save(workspace);
|
||||
}
|
||||
|
||||
update(id: string, data: Partial<TMWorkspace>) {
|
||||
return this.repository.update(id, data);
|
||||
}
|
||||
|
||||
delete(id: string) {
|
||||
return this.repository.delete(id);
|
||||
async findByUser(userId: string): Promise<TMWorkspace[]> {
|
||||
return this.createQueryBuilder("workspace")
|
||||
.innerJoin("workspace.users", "users")
|
||||
.where("user.id = :userId", { userId })
|
||||
.getMany();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,6 @@ export enum PermissionEnum {
|
||||
DKALA = "dkala",
|
||||
DPAGE = "dpage",
|
||||
DMAIL = 'dmail',
|
||||
RESELLER="reseller"
|
||||
RESELLER="reseller",
|
||||
WORKSPACE="workspace"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user