refactor: refactored project
This commit is contained in:
@@ -917,5 +917,12 @@ export const enum AccessLogMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const enum WorkSpaceMessage {
|
export const enum WorkSpaceMessage {
|
||||||
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!"
|
WORKSPACE_EXISTS = "فضای کار با این اسم وجود دارد!",
|
||||||
|
WORKSPACE_NOT_FOUND = "فضای کار مورد نظر شما وجود ندارد!",
|
||||||
|
USERS_DONT_BELONG_TO_THIS_WORKSPACE = "کاربران تعیین شده برای این فضای کاری نیستند!"
|
||||||
|
}
|
||||||
|
|
||||||
|
export const enum ProjectMessage {
|
||||||
|
PROJECT_EXISTS = "پروژه مورد نظر شما از قبل وجود دارد!",
|
||||||
|
PROJECT_NOT_FOUND = "پروژه مورد نظر شما وجود ندارد!"
|
||||||
}
|
}
|
||||||
@@ -5,13 +5,18 @@ 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 { TMProject } from "../entities/project.entity";
|
import { TMProject } from "../entities/project.entity";
|
||||||
|
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
||||||
|
import { PermissionsDec } from "../../../common/decorators/permission.decorator";
|
||||||
|
import { PermissionEnum } from "../../users/enums/permission.enum";
|
||||||
|
|
||||||
|
@AdminRoute()
|
||||||
@ApiTags("Task Manager - Projects")
|
@ApiTags("Task Manager - Projects")
|
||||||
@Controller("task-manager/projects")
|
@Controller("task-manager/projects")
|
||||||
export class ProjectController {
|
export class ProjectController {
|
||||||
constructor(private readonly projectService: ProjectService) {}
|
constructor(private readonly projectService: ProjectService) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
@ApiOperation({ summary: "Get all projects, optionally filtered by workspace" })
|
@ApiOperation({ summary: "Get all projects, optionally filtered by workspace" })
|
||||||
@ApiQuery({ name: "workspaceId", required: false, description: "Filter projects by workspace ID" })
|
@ApiQuery({ name: "workspaceId", required: false, description: "Filter projects by workspace ID" })
|
||||||
@ApiResponse({ status: 200, description: "Paginated list of projects" })
|
@ApiResponse({ status: 200, description: "Paginated list of projects" })
|
||||||
@@ -28,15 +33,17 @@ export class ProjectController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get(":id")
|
@Get(":id")
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
@ApiOperation({ summary: "Get a project by ID" })
|
@ApiOperation({ summary: "Get a project by ID" })
|
||||||
@ApiParam({ name: "id", description: "Project ID" })
|
@ApiParam({ name: "id", description: "Project ID" })
|
||||||
@ApiResponse({ status: 200, description: "Project found", type: TMProject })
|
@ApiResponse({ status: 200, description: "Project found", type: TMProject })
|
||||||
@ApiResponse({ status: 404, description: "Project not found" })
|
@ApiResponse({ status: 404, description: "Project not found" })
|
||||||
findOne(@Param("id") id: string): Promise<TMProject> {
|
findOne(@Param("id") id: string): Promise<TMProject> {
|
||||||
return this.projectService.findOne(id);
|
return this.projectService.findOneOrFail(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
@ApiOperation({ summary: "Create a new project" })
|
@ApiOperation({ summary: "Create a new project" })
|
||||||
@ApiResponse({ status: 201, description: "Project created", type: TMProject })
|
@ApiResponse({ status: 201, description: "Project created", type: TMProject })
|
||||||
@ApiResponse({ status: 400, description: "One or more users are not members of the workspace" })
|
@ApiResponse({ status: 400, description: "One or more users are not members of the workspace" })
|
||||||
@@ -46,6 +53,7 @@ export class ProjectController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Patch(":id")
|
@Patch(":id")
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
@ApiOperation({ summary: "Update a project" })
|
@ApiOperation({ summary: "Update a project" })
|
||||||
@ApiParam({ name: "id", description: "Project ID" })
|
@ApiParam({ name: "id", description: "Project ID" })
|
||||||
@ApiResponse({ status: 200, description: "Project updated", type: TMProject })
|
@ApiResponse({ status: 200, description: "Project updated", type: TMProject })
|
||||||
@@ -56,6 +64,7 @@ export class ProjectController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Delete(":id")
|
@Delete(":id")
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
@ApiOperation({ summary: "Delete a project" })
|
@ApiOperation({ summary: "Delete a project" })
|
||||||
@ApiParam({ name: "id", description: "Project ID" })
|
@ApiParam({ name: "id", description: "Project ID" })
|
||||||
@ApiResponse({ status: 200, description: "Project deleted", type: TMProject })
|
@ApiResponse({ status: 200, description: "Project deleted", type: TMProject })
|
||||||
@@ -63,4 +72,13 @@ export class ProjectController {
|
|||||||
remove(@Param("id") id: string): Promise<TMProject> {
|
remove(@Param("id") id: string): Promise<TMProject> {
|
||||||
return this.projectService.remove(id);
|
return this.projectService.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get("/workspace/:wsId")
|
||||||
|
@ApiOperation({summary: "Get projects of a specific workspace"})
|
||||||
|
@ApiParam({ name: "wsId", description: "Workspace ID" })
|
||||||
|
@ApiResponse({ status: 200, description: "received projects successfully!"})
|
||||||
|
@ApiResponse({ status: 404, description: "WorkSpace not found" })
|
||||||
|
findByWorkspace(@Param("wsId") wsId: string): Promise<TMProject[]> {
|
||||||
|
return this.findByWorkspace(wsId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { BadRequestException, Injectable, NotFoundException } from "@nestjs/comm
|
|||||||
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 { 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";
|
||||||
@@ -10,12 +9,14 @@ 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";
|
||||||
|
import { ProjectMessage, WorkSpaceMessage } from "../../../common/enums/message.enum";
|
||||||
|
import { WorkspaceService } from "./workspace.service";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectService {
|
export class ProjectService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly projectRepository: ProjectRepository,
|
private readonly projectRepository: ProjectRepository,
|
||||||
private readonly workspaceRepository: WorkspaceRepository,
|
private readonly workspaceService: WorkspaceService,
|
||||||
@InjectRepository(User)
|
@InjectRepository(User)
|
||||||
private readonly userRepository: Repository<User>,
|
private readonly userRepository: Repository<User>,
|
||||||
) {}
|
) {}
|
||||||
@@ -34,11 +35,18 @@ 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>> {
|
||||||
|
await this.workspaceService.findOneOrFail(workspaceId);
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
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 {
|
return {
|
||||||
data,
|
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 } });
|
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;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
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.findOne({ where: { id: dto.workspaceId } });
|
const workspace = await this.workspaceService.findOneOrFail(dto.workspaceId);
|
||||||
if (!workspace) throw new NotFoundException(`Workspace #${dto.workspaceId} not found`);
|
|
||||||
|
|
||||||
const project = this.projectRepository.create({
|
const project = this.projectRepository.create({
|
||||||
...rest,
|
...rest,
|
||||||
@@ -67,7 +74,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(`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) });
|
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||||
}
|
}
|
||||||
@@ -76,7 +83,8 @@ export class ProjectService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, dto: UpdateProjectDto): Promise<TMProject> {
|
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;
|
const { userIds, startDate, ...rest } = dto;
|
||||||
|
|
||||||
Object.assign(project, {
|
Object.assign(project, {
|
||||||
@@ -87,13 +95,12 @@ 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.findOne({ where: { id: workspaceId } });
|
const workspace = await this.workspaceService.findOneOrFail(workspaceId);
|
||||||
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(`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) });
|
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||||
} else {
|
} else {
|
||||||
@@ -105,7 +112,7 @@ export class ProjectService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<TMProject> {
|
async remove(id: string): Promise<TMProject> {
|
||||||
const project = await this.findOne(id);
|
const project = await this.findOneOrFail(id);
|
||||||
await this.projectRepository.delete(id);
|
await this.projectRepository.delete(id);
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,18 +6,8 @@ import { TMProject } from "../entities/project.entity";
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectRepository extends Repository<TMProject> {
|
export class ProjectRepository extends Repository<TMProject> {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(TMProject)
|
@InjectRepository(TMProject) projectRepository: Repository<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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,5 +25,6 @@ export enum PermissionEnum {
|
|||||||
DPAGE = "dpage",
|
DPAGE = "dpage",
|
||||||
DMAIL = 'dmail',
|
DMAIL = 'dmail',
|
||||||
RESELLER="reseller",
|
RESELLER="reseller",
|
||||||
WORKSPACE="workspace"
|
WORKSPACE="workspace",
|
||||||
|
PROJECT="project"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user