refactor: refactoring some project service functions
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
import { BadRequestException, 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 { ProjectRepository } from '../repositories/project.repository';
|
import { ProjectRepository } from "../repositories/project.repository";
|
||||||
import { WorkspaceRepository } from '../repositories/workspace.repository';
|
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||||
import { TMProject } from '../entities/project.entity';
|
import { TMProject } from "../entities/project.entity";
|
||||||
import { CreateProjectDto } from '../dto/project/create-project.dto';
|
import { CreateProjectDto } from "../dto/project/create-project.dto";
|
||||||
import { UpdateProjectDto } from '../dto/project/update-project.dto';
|
import { UpdateProjectDto } from "../dto/project/update-project.dto";
|
||||||
import { PaginationDto } from '../../../common/DTO/pagination.dto';
|
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||||
import { PaginatedResult } from '../../../common/interfaces/paginated-result.interface';
|
import { PaginatedResult } from "../../../common/interfaces/paginated-result.interface";
|
||||||
import { buildPageFormat } from '../../../common/helpers/pagination.helper';
|
import { buildPageFormat } from "../../../common/helpers/pagination.helper";
|
||||||
import { User } from '../../users/entities/user.entity';
|
import { User } from "../../users/entities/user.entity";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectService {
|
export class ProjectService {
|
||||||
@@ -25,7 +25,7 @@ export class ProjectService {
|
|||||||
const limit = pagination.limit ?? 10;
|
const limit = pagination.limit ?? 10;
|
||||||
const skip = (page - 1) * limit;
|
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 {
|
return {
|
||||||
data,
|
data,
|
||||||
@@ -33,11 +33,7 @@ export class ProjectService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByWorkspace(
|
async findByWorkspace(workspaceId: string, pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
|
||||||
workspaceId: string,
|
|
||||||
pagination: PaginationDto,
|
|
||||||
baseUrl: string,
|
|
||||||
): Promise<PaginatedResult<TMProject>> {
|
|
||||||
const page = pagination.page ?? 1;
|
const page = pagination.page ?? 1;
|
||||||
const limit = pagination.limit ?? 10;
|
const limit = pagination.limit ?? 10;
|
||||||
const skip = (page - 1) * limit;
|
const skip = (page - 1) * limit;
|
||||||
@@ -51,7 +47,7 @@ export class ProjectService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string): Promise<TMProject> {
|
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`);
|
if (!project) throw new NotFoundException(`Project #${id} not found`);
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
@@ -59,7 +55,7 @@ export class ProjectService {
|
|||||||
async create(dto: CreateProjectDto): Promise<TMProject> {
|
async create(dto: CreateProjectDto): Promise<TMProject> {
|
||||||
const { userIds, startDate, ...rest } = dto;
|
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`);
|
if (!workspace) throw new NotFoundException(`Workspace #${dto.workspaceId} not found`);
|
||||||
|
|
||||||
const project = this.projectRepository.create({
|
const project = this.projectRepository.create({
|
||||||
@@ -71,9 +67,7 @@ export class ProjectService {
|
|||||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||||
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
||||||
if (invalidUsers.length) {
|
if (invalidUsers.length) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||||
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||||
}
|
}
|
||||||
@@ -93,15 +87,13 @@ export class ProjectService {
|
|||||||
if (userIds) {
|
if (userIds) {
|
||||||
if (userIds.length) {
|
if (userIds.length) {
|
||||||
const workspaceId = dto.workspaceId ?? project.workspaceId;
|
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`);
|
if (!workspace) throw new NotFoundException(`Workspace #${workspaceId} not found`);
|
||||||
|
|
||||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||||
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
||||||
if (invalidUsers.length) {
|
if (invalidUsers.length) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||||
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -6,8 +6,18 @@ import { TMProject } from "../entities/project.entity";
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectRepository extends Repository<TMProject> {
|
export class ProjectRepository extends Repository<TMProject> {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(TMProject) projectRepository: Repository<TMProject>,
|
@InjectRepository(TMProject)
|
||||||
|
private readonly projectRepository: Repository<TMProject>,
|
||||||
) {
|
) {
|
||||||
super(projectRepository.target, projectRepository.manager, projectRepository.queryRunner);
|
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