From 40b840e10aa0cd0eb9861c2e596f0d3b1a5d6c8e Mon Sep 17 00:00:00 2001 From: luizy Date: Wed, 29 Nov 2023 15:21:17 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20log=20=EC=8A=A4=ED=82=A4=EB=A7=88=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#172] --- .../backend/src/quizzes/quizzes.controller.ts | 20 ++++++------- .../src/session/schema/session.schema.ts | 28 +++++++++++++------ .../backend/src/session/session.service.ts | 11 +++++--- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/backend/src/quizzes/quizzes.controller.ts b/packages/backend/src/quizzes/quizzes.controller.ts index b50a640..2b09fd0 100644 --- a/packages/backend/src/quizzes/quizzes.controller.ts +++ b/packages/backend/src/quizzes/quizzes.controller.ts @@ -10,6 +10,7 @@ import { Inject, Delete, UseGuards, + NotFoundException, } from '@nestjs/common'; import { ApiTags, @@ -136,10 +137,11 @@ export class QuizzesController { execCommandDto.message, )); } else if (execCommandDto.mode === MODE.EDITOR) { - const recentCommand = await this.sessionService.getRecentLog( - sessionId, - id, - ); + const { mode: recentMode, message: recentMessage } = + await this.sessionService.getRecentLog(sessionId, id); + if (recentMode === MODE.EDITOR) { + throw new NotFoundException('편집기 명령 차례가 아닙니다'); + } const bodyPreview = execCommandDto.message.length > 15 @@ -148,12 +150,12 @@ export class QuizzesController { this.logger.log( 'info', - `running editor command "${recentCommand}" for container ${containerId} with body starts with "${bodyPreview}"`, + `running editor command "${recentMessage}" for container ${containerId} with body starts with "${bodyPreview}"`, ); ({ message, result } = await this.containerService.runEditorCommand( containerId, - recentCommand, + recentMessage, execCommandDto.message, )); } else { @@ -163,11 +165,7 @@ export class QuizzesController { } // 일단 editor일 때도 message를 저장합니다. - this.sessionService.pushLogBySessionId( - execCommandDto.message, - sessionId, - id, - ); + this.sessionService.pushLogBySessionId(execCommandDto, sessionId, id); response.status(HttpStatus.OK).send({ message, diff --git a/packages/backend/src/session/schema/session.schema.ts b/packages/backend/src/session/schema/session.schema.ts index 55451f7..4b3889b 100644 --- a/packages/backend/src/session/schema/session.schema.ts +++ b/packages/backend/src/session/schema/session.schema.ts @@ -1,14 +1,15 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; +const Action = { + Command: 'command', + Editor: 'editor', +} as const; + +export type ActionType = (typeof Action)[keyof typeof Action]; + @Schema({ timestamps: true }) export class Session extends Document { - // @Prop({ required: true }) - // createdAt: Date; - // - // @Prop({ required: true }) - // updatedAt: Date; - @Prop() deletedAt: Date | null; @@ -17,7 +18,15 @@ export class Session extends Document { type: Map, of: { status: { type: String, required: true }, - logs: { type: [String], required: true }, + logs: { + type: [ + { + mode: { type: String, enum: Object.values(Action), required: true }, + message: { type: String, required: true }, + }, + ], + required: true, + }, containerId: { type: String, default: '' }, }, }) @@ -25,7 +34,10 @@ export class Session extends Document { number, { status: string; - logs: string[]; + logs: { + mode: ActionType; + message: string; + }[]; containerId: string; } >; diff --git a/packages/backend/src/session/session.service.ts b/packages/backend/src/session/session.service.ts index 2ef38ac..6de467b 100644 --- a/packages/backend/src/session/session.service.ts +++ b/packages/backend/src/session/session.service.ts @@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Logger } from 'winston'; import { Model } from 'mongoose'; -import { Session } from './schema/session.schema'; +import { ActionType, Session } from './schema/session.schema'; import { ObjectId } from 'typeorm'; @Injectable() @@ -71,7 +71,10 @@ export class SessionService { session.save(); } - async getRecentLog(sessionId: string, problemId: number): Promise { + async getRecentLog( + sessionId: string, + problemId: number, + ): Promise<{ mode: string; message: string }> { const session = await this.getSessionById(sessionId); const problemLogs = session?.problems.get(problemId)?.logs; @@ -83,7 +86,7 @@ export class SessionService { } async pushLogBySessionId( - command: string, + log: { mode: ActionType; message: string }, sessionId: string, problemId: number, ): Promise { @@ -91,7 +94,7 @@ export class SessionService { if (!session.problems.get(problemId)) { throw new Error('problem not found'); } - session.problems.get(problemId).logs.push(command); + session.problems.get(problemId).logs.push(log); session.save(); }