feat: added project section of TM with some refactors
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { ProjectRepository } from "../repositories/project.repository";
|
||||
import { TMProject } from "../entities/project.entity";
|
||||
import { CreateProjectDto } from "../dto/project/create-project.dto";
|
||||
import { UpdateProjectDto } from "../dto/project/update-project.dto";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
constructor(
|
||||
private readonly projectRepository: ProjectRepository,
|
||||
private readonly workspaceRepository: WorkspaceRepository,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMProject[]> {
|
||||
return this.projectRepository.findAll();
|
||||
}
|
||||
|
||||
findByWorkspace(workspaceId: string): Promise<TMProject[]> {
|
||||
return this.projectRepository.findByWorkspace(workspaceId);
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMProject> {
|
||||
const project = await this.projectRepository.findOneById(id);
|
||||
if (!project) throw new NotFoundException(`Project #${id} not found`);
|
||||
return project;
|
||||
}
|
||||
|
||||
async create(dto: CreateProjectDto): Promise<TMProject> {
|
||||
const { userIds, startDate, ...rest } = dto;
|
||||
|
||||
const workspace = await this.workspaceRepository.findOneById(dto.workspaceId);
|
||||
if (!workspace) throw new NotFoundException(`Workspace #${dto.workspaceId} not found`);
|
||||
|
||||
const project = this.projectRepository.create({
|
||||
...rest,
|
||||
startDate: new Date(startDate),
|
||||
});
|
||||
|
||||
if (userIds?.length) {
|
||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
||||
if (invalidUsers.length) {
|
||||
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
}
|
||||
|
||||
return this.projectRepository.save(project);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateProjectDto): Promise<TMProject> {
|
||||
const project = await this.findOne(id);
|
||||
const { userIds, startDate, ...rest } = dto;
|
||||
|
||||
Object.assign(project, {
|
||||
...rest,
|
||||
startDate: startDate ? new Date(startDate) : project.startDate,
|
||||
});
|
||||
|
||||
if (userIds) {
|
||||
if (userIds.length) {
|
||||
const workspaceId = dto.workspaceId ?? project.workspaceId;
|
||||
const workspace = await this.workspaceRepository.findOneById(workspaceId);
|
||||
if (!workspace) throw new NotFoundException(`Workspace #${workspaceId} not found`);
|
||||
|
||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
||||
if (invalidUsers.length) {
|
||||
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
} else {
|
||||
project.users = [];
|
||||
}
|
||||
}
|
||||
|
||||
return this.projectRepository.save(project);
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<TMProject> {
|
||||
const project = await this.findOne(id);
|
||||
await this.projectRepository.delete(id);
|
||||
|
||||
return project;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user