refactor: add validation to workspace create
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { WorkspaceRepository } from '../repositories/workspace.repository';
|
||||
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 { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||
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 { WorkspaceTypeRepository } from "../repositories/workspace-type.repository";
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceService {
|
||||
constructor(
|
||||
private readonly workspaceRepository: WorkspaceRepository,
|
||||
private readonly workspaceTypeRepository: WorkspaceTypeRepository,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
@@ -26,7 +28,10 @@ export class WorkspaceService {
|
||||
}
|
||||
|
||||
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
|
||||
const { userIds, ...rest } = dto;
|
||||
const { userIds, workspaceTypeId, ...rest } = dto;
|
||||
|
||||
const workspaceType = await this.workspaceTypeRepository.findOneById(workspaceTypeId);
|
||||
if (!workspaceType) throw new NotFoundException(`WorkSpaceType #${workspaceTypeId} not found!`);
|
||||
|
||||
const workspace = this.workspaceRepository.create(rest);
|
||||
|
||||
@@ -39,14 +44,17 @@ export class WorkspaceService {
|
||||
|
||||
async update(id: string, dto: UpdateWorkspaceDto): Promise<TMWorkspace> {
|
||||
const workspace = await this.findOneOrFail(id);
|
||||
const { userIds, ...rest } = dto;
|
||||
const { userIds, workspaceTypeId, ...rest } = dto;
|
||||
|
||||
if (workspaceTypeId) {
|
||||
const workspaceType = await this.workspaceTypeRepository.findOneById(workspaceTypeId);
|
||||
if (!workspaceType) throw new NotFoundException(`WorkSpaceType #${workspaceTypeId} not found!`);
|
||||
}
|
||||
|
||||
Object.assign(workspace, rest);
|
||||
|
||||
if (userIds) {
|
||||
workspace.users = userIds.length
|
||||
? await this.userRepository.findBy({ id: In(userIds) })
|
||||
: [];
|
||||
workspace.users = userIds.length ? await this.userRepository.findBy({ id: In(userIds) }) : [];
|
||||
}
|
||||
|
||||
return this.workspaceRepository.save(workspace);
|
||||
@@ -58,4 +66,4 @@ export class WorkspaceService {
|
||||
|
||||
return workspace;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ export class WorkspaceRepository {
|
||||
findOneById(id: string) {
|
||||
return this.repository.findOne({
|
||||
where: { id },
|
||||
relations: ["workspaceType", "projects", "users"],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,6 @@ import { WorkspaceTypeController } from "./controllers/workspace-type.controller
|
||||
// TaskPhaseService,
|
||||
// TaskService,
|
||||
],
|
||||
exports: [WorkspaceService, WorkspaceRepository],
|
||||
exports: [WorkspaceService, WorkspaceRepository, WorkspaceTypeService, WorkspaceTypeRepository],
|
||||
})
|
||||
export class TaskManagerModule {}
|
||||
|
||||
Reference in New Issue
Block a user