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