Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] 실시간 편집 API 연동 시 발생한 문제 해결 #106

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/server/src/task/domain/update-information.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export type UpdateInformation = {
import { IsNumber, IsOptional, IsString } from 'class-validator';

export class UpdateInformation {
@IsOptional()
@IsNumber()
position: number;

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

@IsOptional()
@IsNumber()
length: number;
};
}
15 changes: 12 additions & 3 deletions apps/server/src/task/dto/task-event-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ export class TaskEventResponse {

event: EventType;

constructor(taskEvent: TaskEvent) {
this.taskId = taskEvent.taskId;
this.event = taskEvent.event;
static from(taskEvent: TaskEvent) {
const response = new TaskEventResponse();
response.taskId = taskEvent.taskId;
response.event = taskEvent.event;
return response;
}

static of(taskId: number, taskEvent: TaskEvent) {
const response = new TaskEventResponse();
response.taskId = taskId;
response.event = taskEvent.event;
return response;
}
}
16 changes: 15 additions & 1 deletion apps/server/src/task/dto/task-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { IsNumber, IsNotEmpty, IsString, IsEnum } from 'class-validator';
import {
IsNumber,
IsNotEmpty,
IsString,
IsEnum,
ValidateNested,
IsOptional,
} from 'class-validator';
import { Type } from 'class-transformer';
import { EventType } from '@/task/domain/eventType.enum';
import { UpdateInformation } from '@/task/domain/update-information.type';

export class TaskEvent {
@IsOptional()
@IsNumber()
taskId: number;

@IsOptional()
@IsNumber()
sectionId: number;

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

@IsNotEmpty()
@IsEnum(EventType)
event: EventType;

@IsOptional()
@ValidateNested()
@Type(() => UpdateInformation)
title: UpdateInformation;
}
28 changes: 14 additions & 14 deletions apps/server/src/task/service/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ContributorStatus } from '@/project/enum/contributor-status.enum';
import { TaskEventResponse } from '@/task/dto/task-event-response.dto';
import { UpdateInformation } from '@/task/domain/update-information.type';

const { json0 } = ShareDB.types;
const { defaultType: json0 } = ShareDB.types;

@Injectable()
export class TaskService {
Expand Down Expand Up @@ -73,13 +73,13 @@ export class TaskService {
const existing = await this.findTaskOrThrow(taskEvent.taskId);
const result = this.merge(taskEvent, existing);

const transformedOp = lastOp.length ? json0.type.transform(result, lastOp, 'right') : result;
const transformedOp = lastOp.length ? json0.transform(result, lastOp, 'right') : result;

this.taskRepository.save(result);

this.eventEmitter.emit('broadcast', userId, projectId, new TaskEventResponse(taskEvent));
this.eventEmitter.emit('broadcast', userId, projectId, TaskEventResponse.from(taskEvent));

lastOp = json0.type.compose(lastOp, transformedOp);
lastOp = json0.compose(lastOp, transformedOp);
}
}

Expand All @@ -89,7 +89,7 @@ export class TaskService {
const { event } = change;
const op = this.convertToShareDbOp(event, updateTitle);

const newTitle = json0.type.apply(existingTitle, op);
const newTitle = json0.apply(existingTitle, op);

return { ...existing, title: newTitle };
}
Expand All @@ -101,12 +101,7 @@ export class TaskService {
case EventType.INSERT_TITLE:
return [{ p: [position], si: content }];
case EventType.DELETE_TITLE:
return [
{
p: [position],
sd: content,
},
];
return [{ p: [position], sd: content }];
default:
throw new BadRequestException('Invalid event type');
}
Expand All @@ -129,7 +124,12 @@ export class TaskService {
section,
});

this.eventEmitter.emit('broadcast', userId, projectId, new TaskEventResponse(taskEvent));
this.eventEmitter.emit(
'broadcast',
userId,
projectId,
TaskEventResponse.of(task.id, taskEvent)
);
return new CreateTaskResponse(task);
}

Expand Down Expand Up @@ -180,7 +180,7 @@ export class TaskService {
task.section = section;
await this.taskRepository.save(task);

this.eventEmitter.emit('broadcast', userId, projectId, new TaskEventResponse(taskEvent));
this.eventEmitter.emit('broadcast', userId, projectId, TaskEventResponse.from(taskEvent));
return new MoveTaskResponse(task);
}

Expand Down Expand Up @@ -210,7 +210,7 @@ export class TaskService {
}
await this.taskRepository.delete(taskEvent.taskId);

this.eventEmitter.emit('broadcast', userId, projectId, new TaskEventResponse(taskEvent));
this.eventEmitter.emit('broadcast', userId, projectId, TaskEventResponse.from(taskEvent));
return new DeleteTaskResponse(taskEvent.taskId);
}

Expand Down
Loading