Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f61152d94 | |||
| a14bd490be | |||
| 47a5a2abb4 | |||
| 5763cb7d6c | |||
| 35d0d9f7cd | |||
| d4bd593f5b |
@@ -0,0 +1,11 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||||
|
|
||||||
|
export class Migrations1783244654421 implements MigrationInterface {
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||||
|
|
||||||
|
export class Taskmanager31783244669371 implements MigrationInterface {
|
||||||
|
name = 'Taskmanager31783244669371'
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`ALTER TABLE "tm_workspaces" ADD CONSTRAINT "UQ_6496168de3e6c5f006d494ebac5" UNIQUE ("name")`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`ALTER TABLE "tm_workspaces" DROP CONSTRAINT "UQ_6496168de3e6c5f006d494ebac5"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ import { connectionSource } from "./connection";
|
|||||||
// import { seedCityAndProvince } from "./seeders/iran-city.seeder";
|
// import { seedCityAndProvince } from "./seeders/iran-city.seeder";
|
||||||
// import { seedNotifSettings } from "./seeders/notif-setting.seeder";
|
// import { seedNotifSettings } from "./seeders/notif-setting.seeder";
|
||||||
// import { seedPaymentGateways } from "./seeders/payment-gateway.seeder";
|
// import { seedPaymentGateways } from "./seeders/payment-gateway.seeder";
|
||||||
// import { seedPermissionsAndRoles } from "./seeders/role.seeder";
|
import { seedPermissionsAndRoles } from "./seeders/role.seeder";
|
||||||
import { seedBlogCommentReplies } from "./seeders/blog-comment-reply.seeder";
|
// import { seedBlogCommentReplies } from "./seeders/blog-comment-reply.seeder";
|
||||||
import { seedBlogComments } from "./seeders/blog-comment.seeder";
|
// import { seedBlogComments } from "./seeders/blog-comment.seeder";
|
||||||
|
|
||||||
const logger = new Logger("Seeder");
|
const logger = new Logger("Seeder");
|
||||||
|
|
||||||
@@ -15,14 +15,14 @@ export const runSeeder = async () => {
|
|||||||
await connectionSource.initialize();
|
await connectionSource.initialize();
|
||||||
logger.log("start seeding database");
|
logger.log("start seeding database");
|
||||||
//
|
//
|
||||||
// await seedPermissionsAndRoles(connectionSource, logger);
|
await seedPermissionsAndRoles(connectionSource, logger);
|
||||||
// await seedAdmin(connectionSource, logger);
|
// await seedAdmin(connectionSource, logger);
|
||||||
// await seedNotifSettings(connectionSource, logger);
|
// await seedNotifSettings(connectionSource, logger);
|
||||||
// await seedPaymentGateways(connectionSource, logger);
|
// await seedPaymentGateways(connectionSource, logger);
|
||||||
// await seedCityAndProvince(connectionSource, logger);
|
// await seedCityAndProvince(connectionSource, logger);
|
||||||
|
|
||||||
await seedBlogComments(connectionSource, logger);
|
// await seedBlogComments(connectionSource, logger);
|
||||||
await seedBlogCommentReplies(connectionSource, logger);
|
// await seedBlogCommentReplies(connectionSource, logger);
|
||||||
|
|
||||||
logger.log("seeding completed");
|
logger.log("seeding completed");
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Logger } from "@nestjs/common";
|
import { Logger } from "@nestjs/common";
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
|
|
||||||
import { Permission } from "../../src/modules/users/entities/permission.entity";
|
// import { Permission } from "../../src/modules/users/entities/permission.entity";
|
||||||
import { Role } from "../../src/modules/users/entities/role.entity";
|
// import { Role } from "../../src/modules/users/entities/role.entity";
|
||||||
import { PermissionEnum } from "../../src/modules/users/enums/permission.enum";
|
import { PermissionEnum } from "../../src/modules/users/enums/permission.enum";
|
||||||
import { RoleEnum } from "../../src/modules/users/enums/role.enum";
|
import { RoleEnum } from "../../src/modules/users/enums/role.enum";
|
||||||
|
|
||||||
@@ -23,8 +23,8 @@ const rolesWithPermissions: { name: RoleEnum; permissions: PermissionEnum[] }[]
|
|||||||
|
|
||||||
export const seedPermissionsAndRoles = async (dataSource: DataSource, logger: Logger) => {
|
export const seedPermissionsAndRoles = async (dataSource: DataSource, logger: Logger) => {
|
||||||
try {
|
try {
|
||||||
const permissionRepo = dataSource.getRepository(Permission);
|
const permissionRepo = dataSource.getRepository('Permission');
|
||||||
const roleRepo = dataSource.getRepository(Role);
|
const roleRepo = dataSource.getRepository('Role');
|
||||||
|
|
||||||
// Seed Permissions
|
// Seed Permissions
|
||||||
logger.log("Seeding Permissions...");
|
logger.log("Seeding Permissions...");
|
||||||
|
|||||||
@@ -917,5 +917,14 @@ 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 = "کاربران تعیین شده برای این فضای کاری نیستند!",
|
||||||
|
WORKSPACE_SAME_NAME_EXISTS = "فضای کاری با این اسم از قبل وجود دارد!"
|
||||||
|
}
|
||||||
|
|
||||||
|
export const enum ProjectMessage {
|
||||||
|
PROJECT_EXISTS = "پروژه مورد نظر شما از قبل وجود دارد!",
|
||||||
|
PROJECT_NOT_FOUND = "پروژه مورد نظر شما وجود ندارد!",
|
||||||
|
PROJECT_CONTAINS_TASK_PHASES = "پروژه شامل فاز تسک است. لطفا ابتدا فاز تسک های این پروژه را حذف کنید."
|
||||||
}
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import { IPageFormat } from "../interfaces/IPagination";
|
|
||||||
|
|
||||||
export function buildPageFormat(
|
|
||||||
page: number,
|
|
||||||
limit: number,
|
|
||||||
totalItems: number,
|
|
||||||
baseUrl: string,
|
|
||||||
): IPageFormat {
|
|
||||||
const totalPages = Math.ceil(totalItems / limit);
|
|
||||||
|
|
||||||
const prevPage =
|
|
||||||
page > 1
|
|
||||||
? `${baseUrl}?page=${page - 1}&limit=${limit}`
|
|
||||||
: false;
|
|
||||||
|
|
||||||
const nextPage =
|
|
||||||
page < totalPages
|
|
||||||
? `${baseUrl}?page=${page + 1}&limit=${limit}`
|
|
||||||
: false;
|
|
||||||
|
|
||||||
return {
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
totalItems,
|
|
||||||
totalPages,
|
|
||||||
prevPage,
|
|
||||||
nextPage,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { IPageFormat } from "./IPagination";
|
|
||||||
|
|
||||||
export interface PaginatedResult<T> {
|
|
||||||
data: T[];
|
|
||||||
meta: IPageFormat;
|
|
||||||
}
|
|
||||||
@@ -1,42 +1,40 @@
|
|||||||
import { Controller, Get, Post, Patch, Delete, Param, Body, Query, Headers } from "@nestjs/common";
|
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from "@nestjs/common";
|
||||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from "@nestjs/swagger";
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from "@nestjs/swagger";
|
||||||
import { ProjectService } from "../providers/project.service";
|
import { ProjectService } from "../providers/project.service";
|
||||||
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 { 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" })
|
|
||||||
@ApiResponse({ status: 200, description: "Paginated list of projects" })
|
@ApiResponse({ status: 200, description: "Paginated list of projects" })
|
||||||
findAll(
|
findAll(@Query() pagination: PaginationDto): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
|
||||||
@Query() pagination: PaginationDto,
|
return this.projectService.findAll(pagination);
|
||||||
@Query("workspaceId") workspaceId: string,
|
|
||||||
@Headers("host") host: string,
|
|
||||||
@Headers("x-forwarded-proto") proto?: string,
|
|
||||||
) {
|
|
||||||
const protocol = proto ?? "http";
|
|
||||||
const baseUrl = `${protocol}://${host}/task-manager/projects`;
|
|
||||||
if (workspaceId) return this.projectService.findByWorkspace(workspaceId, pagination, baseUrl);
|
|
||||||
return this.projectService.findAll(pagination, baseUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 +44,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 +55,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 +63,17 @@ 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")
|
||||||
|
@PermissionsDec(PermissionEnum.PROJECT)
|
||||||
|
@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,
|
||||||
|
@Query() pagination: PaginationDto,
|
||||||
|
): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
|
||||||
|
return this.projectService.findByWorkspace(wsId, pagination);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,66 +1,83 @@
|
|||||||
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 { 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 { User } from "../../users/entities/user.entity";
|
||||||
import { PaginatedResult } from '../../../common/interfaces/paginated-result.interface';
|
import { ProjectMessage, WorkSpaceMessage } from "../../../common/enums/message.enum";
|
||||||
import { buildPageFormat } from '../../../common/helpers/pagination.helper';
|
import { WorkspaceService } from "./workspace.service";
|
||||||
import { User } from '../../users/entities/user.entity';
|
|
||||||
|
|
||||||
@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>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async findAll(pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
|
async findAll(pagination: PaginationDto): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
|
||||||
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.findAll(skip, limit);
|
const [projects, count] = await this.projectRepository.findAndCount({
|
||||||
|
skip: skip,
|
||||||
|
take: limit,
|
||||||
|
relations: {
|
||||||
|
users: true,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
users: {
|
||||||
|
id: true,
|
||||||
|
firstName: true,
|
||||||
|
lastName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return { projects, count, paginate: true };
|
||||||
data,
|
|
||||||
meta: buildPageFormat(page, limit, totalItems, baseUrl),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByWorkspace(
|
async findByWorkspace(workspaceId: string, pagination: PaginationDto): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
|
||||||
workspaceId: string,
|
await this.workspaceService.findOneOrFail(workspaceId);
|
||||||
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;
|
||||||
|
|
||||||
const [data, totalItems] = await this.projectRepository.findByWorkspace(workspaceId, skip, limit);
|
const [projects, count] = await this.projectRepository.findAndCount({
|
||||||
|
where: { workspaceId },
|
||||||
|
relations: {
|
||||||
|
users: true,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
users: {
|
||||||
|
id: true,
|
||||||
|
firstName: true,
|
||||||
|
lastName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
skip,
|
||||||
|
take: limit,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return { projects, count, paginate: true };
|
||||||
data,
|
|
||||||
meta: buildPageFormat(page, limit, totalItems, baseUrl),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string): Promise<TMProject> {
|
async findOneOrFail(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(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.findOneById(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,
|
||||||
@@ -71,9 +88,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(WorkSpaceMessage.USERS_DONT_BELONG_TO_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) });
|
||||||
}
|
}
|
||||||
@@ -82,7 +97,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, {
|
||||||
@@ -93,15 +109,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.findOneById(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(
|
throw new BadRequestException(WorkSpaceMessage.USERS_DONT_BELONG_TO_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 {
|
||||||
@@ -113,8 +126,27 @@ 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.projectRepository.findOne({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
relations: {
|
||||||
|
taskPhases: true,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
taskPhases: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!project) throw new NotFoundException(ProjectMessage.PROJECT_NOT_FOUND);
|
||||||
|
|
||||||
|
const taskPhases = project.taskPhases;
|
||||||
|
if (taskPhases !== undefined && taskPhases.length) {
|
||||||
|
throw new BadRequestException(ProjectMessage.PROJECT_CONTAINS_TASK_PHASES);
|
||||||
|
}
|
||||||
await this.projectRepository.delete(id);
|
await this.projectRepository.delete(id);
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
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, Not, Repository } from "typeorm";
|
||||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||||
import { TMWorkspace } from "../entities/workspace.entity";
|
import { TMWorkspace } from "../entities/workspace.entity";
|
||||||
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
|
import { CreateWorkspaceDto } from "../dto/workspace/create-workspace.dto";
|
||||||
@@ -17,22 +17,20 @@ export class WorkspaceService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAll(): Promise<TMWorkspace[]> {
|
findAll(): Promise<TMWorkspace[]> {
|
||||||
return this.workspaceRepository.find({
|
return this.workspaceRepository.find({});
|
||||||
relations: ["projects", "users"],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
||||||
const workspace = await this.workspaceRepository.findOne({ where: { id } });
|
const workspace = await this.workspaceRepository.findOneWorkSpace(id);
|
||||||
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
|
if (!workspace) throw new NotFoundException(WorkSpaceMessage.WORKSPACE_NOT_FOUND);
|
||||||
return workspace;
|
return workspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
|
async create(dto: CreateWorkspaceDto): Promise<TMWorkspace> {
|
||||||
const { userIds, ...rest } = dto;
|
const { userIds, ...rest } = dto;
|
||||||
|
|
||||||
const existingWorkspace = await this.workspaceRepository.findOne({where: {name: rest.name}});
|
const existingWorkspace = await this.workspaceRepository.findOne({ where: { name: rest.name } });
|
||||||
if(existingWorkspace) {
|
if (existingWorkspace) {
|
||||||
throw new BadRequestException(WorkSpaceMessage.WORKSPACE_EXISTS);
|
throw new BadRequestException(WorkSpaceMessage.WORKSPACE_EXISTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +47,11 @@ export class WorkspaceService {
|
|||||||
const workspace = await this.findOneOrFail(id);
|
const workspace = await this.findOneOrFail(id);
|
||||||
const { userIds, ...rest } = dto;
|
const { userIds, ...rest } = dto;
|
||||||
|
|
||||||
|
if (rest.name) {
|
||||||
|
const existingWorkSpaceWithTheSameName = await this.workspaceRepository.findOne({ where: { name: rest.name, id: Not(id) } });
|
||||||
|
if (existingWorkSpaceWithTheSameName) throw new BadRequestException(WorkSpaceMessage.WORKSPACE_SAME_NAME_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
Object.assign(workspace, rest);
|
Object.assign(workspace, rest);
|
||||||
|
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
|
|||||||
@@ -1,52 +1,13 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from "@nestjs/common";
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from "typeorm";
|
||||||
import { TMProject } from '../entities/project.entity';
|
import { TMProject } from "../entities/project.entity";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectRepository {
|
export class ProjectRepository extends Repository<TMProject> {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(TMProject)
|
@InjectRepository(TMProject) projectRepository: Repository<TMProject>,
|
||||||
private readonly repository: Repository<TMProject>,
|
) {
|
||||||
) {}
|
super(projectRepository.target, projectRepository.manager, projectRepository.queryRunner);
|
||||||
|
|
||||||
findAll(skip: number, take: number) {
|
|
||||||
return this.repository.findAndCount({
|
|
||||||
relations: ['workspace', 'taskPhases', 'users'],
|
|
||||||
skip,
|
|
||||||
take,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
findByWorkspace(workspaceId: string, skip: number, take: number) {
|
|
||||||
return this.repository.findAndCount({
|
|
||||||
where: { workspaceId },
|
|
||||||
relations: ['taskPhases', 'users'],
|
|
||||||
skip,
|
|
||||||
take,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
findOneById(id: string) {
|
|
||||||
return this.repository.findOne({
|
|
||||||
where: { id },
|
|
||||||
relations: ['workspace', 'taskPhases', 'users'],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
create(data: Partial<TMProject>) {
|
|
||||||
return this.repository.create(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
save(project: TMProject) {
|
|
||||||
return this.repository.save(project);
|
|
||||||
}
|
|
||||||
|
|
||||||
update(id: string, data: Partial<TMProject>) {
|
|
||||||
return this.repository.update(id, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(id: string) {
|
|
||||||
return this.repository.delete(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
import { Repository } from "typeorm";
|
import { Repository } from 'typeorm';
|
||||||
import { TMWorkspace } from "../entities/workspace.entity";
|
import { TMWorkspace } from '../entities/workspace.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkspaceRepository extends Repository<TMWorkspace> {
|
export class WorkspaceRepository extends Repository<TMWorkspace> {
|
||||||
@@ -9,10 +9,22 @@ export class WorkspaceRepository extends Repository<TMWorkspace> {
|
|||||||
super(workspaceRepository.target, workspaceRepository.manager, workspaceRepository.queryRunner);
|
super(workspaceRepository.target, workspaceRepository.manager, workspaceRepository.queryRunner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findOneWorkSpace(workspaceId: string): Promise<TMWorkspace | null> {
|
||||||
|
return this.createQueryBuilder("workspace")
|
||||||
|
.leftJoinAndSelect('workspace.users', 'user')
|
||||||
|
.select([
|
||||||
|
'workspace',
|
||||||
|
'user.id',
|
||||||
|
'user.firstName',
|
||||||
|
'user.lastName'
|
||||||
|
]).where('workspace.id = :workspaceId', {workspaceId})
|
||||||
|
.getOne();
|
||||||
|
}
|
||||||
|
|
||||||
async findByUser(userId: string): Promise<TMWorkspace[]> {
|
async findByUser(userId: string): Promise<TMWorkspace[]> {
|
||||||
return this.createQueryBuilder("workspace")
|
return this.createQueryBuilder("workspace")
|
||||||
.innerJoin("workspace.users", "users")
|
.innerJoin("workspace.users", "users")
|
||||||
.where("user.id = :userId", { userId })
|
.where("users.id = :userId", { userId })
|
||||||
.getMany();
|
.getMany();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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