refactor: refactored project

This commit is contained in:
2026-07-05 16:22:36 +03:30
parent 5763cb7d6c
commit 47a5a2abb4
5 changed files with 50 additions and 27 deletions
@@ -2,7 +2,6 @@ import { BadRequestException, Injectable, NotFoundException } from "@nestjs/comm
import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from "typeorm";
import { ProjectRepository } from "../repositories/project.repository";
import { WorkspaceRepository } from "../repositories/workspace.repository";
import { TMProject } from "../entities/project.entity";
import { CreateProjectDto } from "../dto/project/create-project.dto";
import { UpdateProjectDto } from "../dto/project/update-project.dto";
@@ -10,12 +9,14 @@ import { PaginationDto } from "../../../common/DTO/pagination.dto";
import { PaginatedResult } from "../../../common/interfaces/paginated-result.interface";
import { buildPageFormat } from "../../../common/helpers/pagination.helper";
import { User } from "../../users/entities/user.entity";
import { ProjectMessage, WorkSpaceMessage } from "../../../common/enums/message.enum";
import { WorkspaceService } from "./workspace.service";
@Injectable()
export class ProjectService {
constructor(
private readonly projectRepository: ProjectRepository,
private readonly workspaceRepository: WorkspaceRepository,
private readonly workspaceService: WorkspaceService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
@@ -34,11 +35,18 @@ export class ProjectService {
}
async findByWorkspace(workspaceId: string, pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
await this.workspaceService.findOneOrFail(workspaceId);
const page = pagination.page ?? 1;
const limit = pagination.limit ?? 10;
const skip = (page - 1) * limit;
const [data, totalItems] = await this.projectRepository.findByWorkspace(workspaceId, skip, limit);
const [data, totalItems] = await this.projectRepository.findAndCount({
where: { workspaceId },
relations: ["taskPhases", "users"],
skip,
take: limit,
});
return {
data,
@@ -46,17 +54,16 @@ export class ProjectService {
};
}
async findOne(id: string): Promise<TMProject> {
async findOneOrFail(id: string): Promise<TMProject> {
const project = await this.projectRepository.findOne({ where: { id } });
if (!project) throw new NotFoundException(`Project #${id} not found`);
if (!project) throw new NotFoundException(ProjectMessage.PROJECT_NOT_FOUND);
return project;
}
async create(dto: CreateProjectDto): Promise<TMProject> {
const { userIds, startDate, ...rest } = dto;
const workspace = await this.workspaceRepository.findOne({ where: { id: dto.workspaceId } });
if (!workspace) throw new NotFoundException(`Workspace #${dto.workspaceId} not found`);
const workspace = await this.workspaceService.findOneOrFail(dto.workspaceId);
const project = this.projectRepository.create({
...rest,
@@ -67,7 +74,7 @@ export class ProjectService {
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`);
throw new BadRequestException(WorkSpaceMessage.USERS_DONT_BELONG_TO_THIS_WORKSPACE);
}
project.users = await this.userRepository.findBy({ id: In(userIds) });
}
@@ -76,7 +83,8 @@ export class ProjectService {
}
async update(id: string, dto: UpdateProjectDto): Promise<TMProject> {
const project = await this.findOne(id);
const project = await this.findOneOrFail(id);
const { userIds, startDate, ...rest } = dto;
Object.assign(project, {
@@ -87,13 +95,12 @@ export class ProjectService {
if (userIds) {
if (userIds.length) {
const workspaceId = dto.workspaceId ?? project.workspaceId;
const workspace = await this.workspaceRepository.findOne({ where: { id: workspaceId } });
if (!workspace) throw new NotFoundException(`Workspace #${workspaceId} not found`);
const workspace = await this.workspaceService.findOneOrFail(workspaceId);
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`);
throw new BadRequestException(WorkSpaceMessage.USERS_DONT_BELONG_TO_THIS_WORKSPACE);
}
project.users = await this.userRepository.findBy({ id: In(userIds) });
} else {
@@ -105,7 +112,7 @@ export class ProjectService {
}
async remove(id: string): Promise<TMProject> {
const project = await this.findOne(id);
const project = await this.findOneOrFail(id);
await this.projectRepository.delete(id);
return project;
}