feat: added get user projects function in project

This commit is contained in:
2026-07-06 12:06:33 +03:30
parent 9f61152d94
commit 16a0c14149
8 changed files with 54 additions and 13 deletions
@@ -8,7 +8,10 @@ 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";
import { UserDec } from "../../../common/decorators/user.decorator";
import { AuthGuards } from "../../../common/decorators/auth-guard.decorator";
@AuthGuards()
@AdminRoute()
@ApiTags("Task Manager - Projects")
@Controller("task-manager/projects")
@@ -76,4 +79,14 @@ export class ProjectController {
): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
return this.projectService.findByWorkspace(wsId, pagination);
}
@Get("/user")
@ApiOperation({ summary: "Get user projects!" })
@ApiResponse({ status: 200, description: "Got user projects successfully!" })
findUserProjects(
@UserDec("id") userId: string,
@Query() pagination: PaginationDto,
): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
return this.projectService.findUserProjects(userId, pagination);
}
}
@@ -140,8 +140,8 @@ export class ProjectService {
},
});
if(!project) throw new NotFoundException(ProjectMessage.PROJECT_NOT_FOUND);
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);
@@ -149,4 +149,14 @@ export class ProjectService {
await this.projectRepository.delete(id);
return project;
}
async findUserProjects(userId: string, pagination: PaginationDto): Promise<{ projects: TMProject[]; count: number; paginate: true }> {
const page = pagination.page ?? 1;
const limit = pagination.limit ?? 10;
const skip = (page - 1) * limit;
const [projects, count] = await this.projectRepository.findUserProjects(userId, skip, limit);
return { projects, count, paginate: true };
}
}
@@ -10,4 +10,13 @@ export class ProjectRepository extends Repository<TMProject> {
) {
super(projectRepository.target, projectRepository.manager, projectRepository.queryRunner);
}
async findUserProjects(userId: string, skip?: number, take?: number): Promise<[TMProject[], number]> {
return this.createQueryBuilder('project')
.innerJoin('project.users', 'user')
.where('user.id = :userId', {userId})
.skip(skip ?? 0)
.take(take ?? 0)
.getManyAndCount();
}
}