fix & enhance workspace
This commit is contained in:
@@ -1,41 +1,64 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
|
||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||
import { UpdateWorkspaceDto } from "../dto/workspace/update-workspace.dto";
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
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';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceService {
|
||||
constructor(
|
||||
private readonly workspaceRepository: WorkspaceRepository,
|
||||
@InjectRepository(TMWorkspace)
|
||||
private readonly workspaceRepository: Repository<TMWorkspace>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.workspaceRepository.findAll();
|
||||
findAll(): Promise<TMWorkspace[]> {
|
||||
return this.workspaceRepository.find({
|
||||
relations: ['workspaceType', 'projects', 'users'],
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const workspace = await this.workspaceRepository.findOneById(id);
|
||||
|
||||
if (!workspace) {
|
||||
throw new NotFoundException(`Workspace #${id} not found`);
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
||||
const workspace = await this.workspaceRepository.findOne({
|
||||
where: { id },
|
||||
relations: ['workspaceType', 'projects', 'users'],
|
||||
});
|
||||
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
async create(dto: CreateWorkspaceDto) {
|
||||
const workspace = this.workspaceRepository.create(dto);
|
||||
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
|
||||
const { userIds, ...rest } = dto;
|
||||
|
||||
const workspace = this.workspaceRepository.create(rest);
|
||||
|
||||
if (userIds?.length) {
|
||||
workspace.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
}
|
||||
|
||||
return this.workspaceRepository.save(workspace);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateWorkspaceDto) {
|
||||
await this.findOne(id);
|
||||
await this.workspaceRepository.update(id, dto);
|
||||
return this.findOne(id);
|
||||
async update(id: string, dto: UpdateWorkspaceDto): Promise<TMWorkspace> {
|
||||
const workspace = await this.findOneOrFail(id);
|
||||
const { userIds, ...rest } = dto;
|
||||
|
||||
Object.assign(workspace, rest);
|
||||
|
||||
if (userIds) {
|
||||
workspace.users = userIds.length
|
||||
? await this.userRepository.findBy({ id: In(userIds) })
|
||||
: [];
|
||||
}
|
||||
|
||||
return this.workspaceRepository.save(workspace);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
await this.findOne(id);
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOneOrFail(id);
|
||||
await this.workspaceRepository.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user