refactor: refactored the getProjectDetail function to include more values

This commit is contained in:
2026-07-06 16:17:02 +03:30
parent 0cd5a6a0cc
commit e310411a4d
2 changed files with 43 additions and 12 deletions
@@ -57,4 +57,9 @@ export class TMTask extends BaseEntity {
inverseJoinColumn: { name: "userId", referencedColumnName: "id" },
})
users: User[];
// deriven(calculated) attributes. not database columns
attachmentCount?: number;
checkListItemCount?: number;
completedCheckListItemCount?: number;
}
@@ -23,17 +23,43 @@ export class ProjectRepository extends Repository<TMProject> {
}
async getProjectDetail(projectId: string): Promise<TMProject | null> {
return this.createQueryBuilder('project')
.leftJoinAndSelect('project.taskPhases', 'taskPhase')
.leftJoinAndSelect('taskPhase.tasks', 'task')
.where('project.id = :projectId', {projectId})
.select([
"project.id",
"project.name",
"taskPhase.id",
"taskPhase.name",
"task.id",
"task.title"
]).getOne();
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",
"task.id",
"task.title",
"task.startDate",
"task.endDate",
"remark.title",
"remark.color",
])
.getOne();
}
}