refactor: refactoring some project service functions
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
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';
|
||||
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 { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
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";
|
||||
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";
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
@@ -25,7 +25,7 @@ export class ProjectService {
|
||||
const limit = pagination.limit ?? 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, totalItems] = await this.projectRepository.findAll(skip, limit);
|
||||
const [data, totalItems] = await this.projectRepository.findAndCount({ skip, take: limit });
|
||||
|
||||
return {
|
||||
data,
|
||||
@@ -33,11 +33,7 @@ export class ProjectService {
|
||||
};
|
||||
}
|
||||
|
||||
async findByWorkspace(
|
||||
workspaceId: string,
|
||||
pagination: PaginationDto,
|
||||
baseUrl: string,
|
||||
): Promise<PaginatedResult<TMProject>> {
|
||||
async findByWorkspace(workspaceId: string, pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
|
||||
const page = pagination.page ?? 1;
|
||||
const limit = pagination.limit ?? 10;
|
||||
const skip = (page - 1) * limit;
|
||||
@@ -51,7 +47,7 @@ export class ProjectService {
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMProject> {
|
||||
const project = await this.projectRepository.findOneById(id);
|
||||
const project = await this.projectRepository.findOne({ where: { id } });
|
||||
if (!project) throw new NotFoundException(`Project #${id} not found`);
|
||||
return project;
|
||||
}
|
||||
@@ -59,7 +55,7 @@ export class ProjectService {
|
||||
async create(dto: CreateProjectDto): Promise<TMProject> {
|
||||
const { userIds, startDate, ...rest } = dto;
|
||||
|
||||
const workspace = await this.workspaceRepository.findOneById(dto.workspaceId);
|
||||
const workspace = await this.workspaceRepository.findOne({ where: { id: dto.workspaceId } });
|
||||
if (!workspace) throw new NotFoundException(`Workspace #${dto.workspaceId} not found`);
|
||||
|
||||
const project = this.projectRepository.create({
|
||||
@@ -71,9 +67,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(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
}
|
||||
@@ -93,15 +87,13 @@ export class ProjectService {
|
||||
if (userIds) {
|
||||
if (userIds.length) {
|
||||
const workspaceId = dto.workspaceId ?? project.workspaceId;
|
||||
const workspace = await this.workspaceRepository.findOneById(workspaceId);
|
||||
const workspace = await this.workspaceRepository.findOne({ where: { id: 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`,
|
||||
);
|
||||
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
} else {
|
||||
|
||||
@@ -6,8 +6,18 @@ import { TMProject } from "../entities/project.entity";
|
||||
@Injectable()
|
||||
export class ProjectRepository extends Repository<TMProject> {
|
||||
constructor(
|
||||
@InjectRepository(TMProject) projectRepository: Repository<TMProject>,
|
||||
@InjectRepository(TMProject)
|
||||
private readonly projectRepository: Repository<TMProject>,
|
||||
) {
|
||||
super(projectRepository.target, projectRepository.manager, projectRepository.queryRunner);
|
||||
}
|
||||
|
||||
findByWorkspace(workspaceId: string, skip: number, take: number) {
|
||||
return this.projectRepository.findAndCount({
|
||||
where: { workspaceId },
|
||||
relations: ["taskPhases", "users"],
|
||||
skip,
|
||||
take,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user