Skip to content

Commit

Permalink
feat: 스냅샷 갱신 후에 섹션 변경 사항이 적용되지 않는 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchef1 committed Nov 14, 2024
1 parent 2363746 commit b449569
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
4 changes: 3 additions & 1 deletion apps/server/src/common/allException.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export class AllExceptionsFilter implements ExceptionFilter {
const status = exception instanceof HttpException ? exception.getStatus() : 500;
const message = exception.message || exception;

this.logger.error(`[RESPONSE] ${method} ${url} ${ip} - ${status} ${message}`);
this.logger.error(
`[RESPONSE] ${method} ${url} ${ip} - ${status} ${message} ${exception.stack}`
);
response.status(status).json({
status,
message,
Expand Down
9 changes: 6 additions & 3 deletions apps/server/src/task/domain/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Task } from '@/task/domain/task.entity';
import { Task } from './task.entity';

export class Snapshot {
constructor(project) {
Expand All @@ -20,8 +20,8 @@ export class Snapshot {
}[];
}[];

update(task: Task) {
const section = this.project.find((s) => s.id === task.section.id);
update(prevSectionId: number, task: Task) {
const section = this.project.find((s) => s.id === prevSectionId);
if (!section) {
return;
}
Expand All @@ -35,5 +35,8 @@ export class Snapshot {
target.description = task.description;
target.sectionName = task.section.name;
target.position = task.position;

this.project.find((s) => s.id === task.section.id).tasks.push(target);
section.tasks = section.tasks.filter((t) => t.id !== task.id);
}
}
5 changes: 4 additions & 1 deletion apps/server/src/task/dto/update-task-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { IsNumber, IsString } from 'class-validator';
import { IsNumber, IsOptional, IsString } from 'class-validator';

export class UpdateTaskRequest {
@IsString()
@IsOptional()
title: string;

@IsString()
@IsOptional()
description: string;

@IsNumber()
@IsOptional()
sectionId: number;
}
74 changes: 43 additions & 31 deletions apps/server/src/task/service/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export class TaskService {
connections.forEach((res) => {
if (res.userId !== userId) {
const snapshot = this.snapshots.get(projectId.toString());
if (res.headersSent) {
return;
}
res.json({
status: 200,
message: '스냅샷에 변경 사항이 발생했습니다.',
Expand All @@ -67,12 +70,17 @@ export class TaskService {
});
}

private updateSnapshot(projectId: number, userId: number, task: Task) {
private updateSnapshot(
projectId: number,
prevSectionId: number,
userId: number,
savedTask: Task
) {
const snapshot = this.snapshots.get(projectId.toString());
if (!snapshot) {
throw new NotFoundException('Snapshot not found');
}
snapshot.update(task);
snapshot.update(prevSectionId, savedTask);
this.sendConnection(projectId, userId);
}

Expand All @@ -95,51 +103,52 @@ export class TaskService {
}

async getAll(projectId: number) {
const sections = await this.sectionRepository.find({
where: { project: { id: projectId } },
order: { id: 'ASC' },
});

const tasks = await this.taskRepository.find({
where: { section: { project: { id: projectId } } },
relations: ['section'],
select: ['id', 'title', 'description', 'position', 'section'],
});

const taskBySection = tasks.reduce((acc, task) => {
const sectionId = task.section.id;
const sectionName = task.section.name;
let sectionData = acc.find((data) => data.id === sectionId);

if (!sectionData) {
acc.push({
id: sectionId,
name: sectionName,
tasks: [],
});
}

sectionData = acc.find((data) => data.id === sectionId);

sectionData.tasks.push(new TaskResponse(task));

return acc;
}, []);
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)),
});
});

this.snapshots.set(projectId.toString(), new Snapshot(taskBySection));

return taskBySection;
}

async update(id: number, userId: number, updateTaskRequest: UpdateTaskRequest) {
const task = await this.findTaskOrThrow(id);
const prevTask = await this.findTaskOrThrow(id);
const prevSectionId = prevTask.section.id;
const projectId = prevTask.section.project.id;

task.title = updateTaskRequest.title ?? task.title;
task.description = updateTaskRequest.description ?? task.description;
const newTask = new Task();
newTask.title = updateTaskRequest.title ?? prevTask.title;
newTask.description = updateTaskRequest.description ?? prevTask.description;

const section = await this.findSectionOrThrow(updateTaskRequest.sectionId);
task.section = section;
if (updateTaskRequest.sectionId) {
const section = await this.findSectionOrThrow(updateTaskRequest.sectionId);
newTask.section = section;
}

const result = await this.taskRepository.save(task);
const savedTask = await this.taskRepository.save(newTask);

this.updateSnapshot(1, userId, result);
this.updateSnapshot(projectId, prevSectionId, userId, savedTask);

return new UpdateTaskResponse(result);
return new UpdateTaskResponse(savedTask);
}

async move(id: number, moveTaskRequest: MoveTaskRequest) {
Expand Down Expand Up @@ -167,15 +176,18 @@ export class TaskService {
}

private async findTaskOrThrow(id: number) {
const task = await this.taskRepository.findOneBy({ id });
const task = await this.taskRepository.findOne({
where: { id },
relations: ['section', 'section.project'],
});
if (!task) {
throw new NotFoundException('Task not found');
}
return task;
}

private async findSectionOrThrow(id: number) {
const section = await this.sectionRepository.findOneBy({ id });
const section = await this.sectionRepository.findOne({ where: { id }, relations: ['project'] });
if (!section) {
throw new NotFoundException('Section not found');
}
Expand Down

0 comments on commit b449569

Please sign in to comment.