Skip to content

Commit

Permalink
refactor: 단일 및 전체 태스크 조회 dto 필드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeonghak committed Nov 26, 2024
1 parent 1240910 commit 6ceae01
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
14 changes: 13 additions & 1 deletion apps/server/src/task/dto/task-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { Task } from '@/task/domain/task.entity';
import { AssigneeDetailsResponse } from '@/task/dto/assignee-details-response.dto';
import { LabelDetailsResponse } from '@/project/dto/label-details-response.dto';

export class TaskResponse {
constructor(task: Task) {
constructor(
task: Task,
assigneeDetails: AssigneeDetailsResponse[],
labelDetails: LabelDetailsResponse[]
) {
this.id = task.id;
this.title = task.title;
this.description = task.description;
this.sectionId = task.section.id;
this.position = task.position;
this.assignees = assigneeDetails;
this.labels = labelDetails;
}

id: number;
Expand All @@ -18,4 +26,8 @@ export class TaskResponse {
sectionId: number;

position: string;

assignees: AssigneeDetailsResponse[];

labels: LabelDetailsResponse[];
}
81 changes: 67 additions & 14 deletions apps/server/src/task/service/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,27 @@ export class TaskService {
select: ['id', 'title', 'description', 'position', 'section'],
});

const taskAssigneeRecords = await this.findTaskAssigneeRecordsByProject(projectId);
const labelRecords = await this.findTaskLabelRecordsByProject(projectId);

const taskBySection = [];
sections.forEach((section) => {
taskBySection.push({
id: section.id,
name: section.name,
tasks: tasks
.filter((task) => task.section.id === section.id)
.map((task) => new TaskResponse(task)),
.map((task) => {
return new TaskResponse(
task,
taskAssigneeRecords
.filter((record) => record.taskId === task.id)
.map((record) => new AssigneeDetailsResponse(record.id, record.username, '')),
labelRecords
.filter((record) => record.taskId === task.id)
.map((record) => new LabelDetailsResponse(record as Label))
);
}),
});
});

Expand Down Expand Up @@ -215,7 +228,15 @@ export class TaskService {
async get(userId: number, taskId: number) {
const task = await this.findTaskOrThrow(taskId);
await this.validateUserRole(userId, task.section.project.id);
return new TaskResponse(task);
const taskAssigneeRecords = await this.findTaskAssigneeRecordsByTask(taskId);
const labels = await this.findTaskLabelsByTask(taskId);
return new TaskResponse(
task,
taskAssigneeRecords.map(
(record) => new AssigneeDetailsResponse(record.id, record.username, '')
),
labels.map((label) => new LabelDetailsResponse(label))
);
}

async delete(userId: number, projectId: number, taskEvent: TaskEvent) {
Expand Down Expand Up @@ -344,23 +365,14 @@ export class TaskService {
: null;
result.setSprint(sprint ? new SprintDetailsResponse(sprint) : null);

const taskAssigneeRecords = await this.taskAssigneeRepository
.createQueryBuilder('ta')
.leftJoin('account', 'a', 'ta.accountId = a.id')
.where('ta.taskId = :taskId', { taskId })
.addSelect(['a.username'])
.getRawMany();
const taskAssigneeRecords = await this.findTaskAssigneeRecordsByTask(taskId);
result.setAssignees(
taskAssigneeRecords.map(
(record) => new AssigneeDetailsResponse(record.ta_accountId, record.a_username, '')
(record) => new AssigneeDetailsResponse(record.id, record.username, '')
)
);

const labels = await this.labelRepository
.createQueryBuilder('l')
.leftJoin('task_label', 'tl', 'l.id = tl.labelId')
.where('tl.taskId = :taskId', { taskId })
.getMany();
const labels = await this.findTaskLabelsByTask(taskId);
result.setLabels(labels.map((label) => new LabelDetailsResponse(label)));

const subTasks = await this.subTaskRepository.findBy({ taskId });
Expand Down Expand Up @@ -440,4 +452,45 @@ export class TaskService {
}
return section;
}

private async findTaskLabelsByTask(taskId: number) {
return this.labelRepository
.createQueryBuilder('l')
.leftJoin('task_label', 'tl', 'l.id = tl.labelId')
.where('tl.taskId = :taskId', { taskId })
.getMany();
}

private async findTaskAssigneeRecordsByTask(taskId: number) {
return this.taskAssigneeRepository
.createQueryBuilder('ta')
.leftJoin('account', 'a', 'ta.accountId = a.id')
.where('ta.taskId = :taskId', { taskId })
.select(['ta.accountId AS id', 'a.username AS username'])
.getRawMany();
}

private async findTaskLabelRecordsByProject(projectId: number) {
return this.taskLabelRepository
.createQueryBuilder('tl')
.leftJoin('label', 'l', 'tl.labelId = l.id')
.where('tl.projectId = :projectId', { projectId })
.select([
'tl.taskId AS taskId',
'l.id AS id',
'l.title AS title',
'l.description AS description',
'l.color AS color',
])
.getRawMany();
}

private async findTaskAssigneeRecordsByProject(projectId: number) {
return this.taskAssigneeRepository
.createQueryBuilder('ta')
.leftJoin('account', 'a', 'ta.accountId = a.id')
.where('ta.projectId = :projectId', { projectId })
.select(['ta.taskId AS taskId', 'ta.accountId AS id', 'a.username AS username'])
.getRawMany();
}
}

0 comments on commit 6ceae01

Please sign in to comment.