58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
import { Repository } from "typeorm";
|
|
import { TMProject } from "../entities/project.entity";
|
|
|
|
@Injectable()
|
|
export class ProjectRepository extends Repository<TMProject> {
|
|
constructor(@InjectRepository(TMProject) projectRepository: Repository<TMProject>) {
|
|
super(projectRepository.target, projectRepository.manager, projectRepository.queryRunner);
|
|
}
|
|
|
|
async findUserProjects(userId: string, wsId: string, skip?: number, take?: number): Promise<[TMProject[], number]> {
|
|
return this.createQueryBuilder("project")
|
|
.innerJoin("project.workspace", "workspace")
|
|
.innerJoin("project.users", "user")
|
|
.where("workspace.id = :wsId", { wsId })
|
|
.andWhere("user.id = :userId", { userId })
|
|
.skip(skip ?? 0)
|
|
.take(take ?? 10)
|
|
.getManyAndCount();
|
|
}
|
|
|
|
async getProjectDetail(projectId: string): Promise<TMProject | null> {
|
|
return this.createQueryBuilder("project")
|
|
.leftJoinAndSelect("project.taskPhases", "taskPhase")
|
|
.leftJoinAndSelect("taskPhase.tasks", "task")
|
|
.leftJoinAndSelect("task.remarks", "remark")
|
|
|
|
.loadRelationCountAndMap("task.attachmentCount", "task.attachments")
|
|
|
|
.loadRelationCountAndMap("task.checkListItemCount", "task.checkListItems")
|
|
|
|
.loadRelationCountAndMap("task.completedCheckListItemCount", "task.checkListItems", "completedCheckListItems", (qb) =>
|
|
qb.where("completedCheckListItems.isDone = :isDone", { isDone: true }),
|
|
)
|
|
|
|
.where("project.id = :projectId", { projectId })
|
|
|
|
.select([
|
|
"project.id",
|
|
"project.name",
|
|
"taskPhase.id",
|
|
"taskPhase.name",
|
|
"taskPhase.order",
|
|
"task.id",
|
|
"task.title",
|
|
"task.startDate",
|
|
"task.endDate",
|
|
"remark.title",
|
|
"remark.color",
|
|
])
|
|
|
|
.orderBy("taskPhase.order", "ASC")
|
|
|
|
.getOne();
|
|
}
|
|
}
|