Skip to content

Commit 880bf9a

Browse files
authored
Chore: Log when admin updates/deletes a room (#167)
* Chore: Log when admin updates/deletes a room * Chore: Add detailed log
1 parent f249483 commit 880bf9a

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

src/room/room.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export class RoomController {
273273
description: '로그인이 되어 있지 않은 경우, 방장이나 관리자가 아닌 경우',
274274
})
275275
async remove(@User() user: JwtPayload, @Param('uuid') uuid: string) {
276-
return this.roomService.remove(uuid, user.uuid);
276+
return this.roomService.remove(uuid, user.uuid, user.userType);
277277
}
278278

279279
@Post('join/:uuid')

src/room/room.service.ts

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,19 @@ export class RoomService {
183183
}
184184

185185
async update(uuid: string, updateRoomDto: UpdateRoomDto, user: JwtPayload) {
186-
const room = await this.findOne(uuid);
186+
const originalRoom = await this.findOne(uuid);
187187

188188
if (
189-
room.status == RoomStatus.COMPLETED ||
190-
room.status == RoomStatus.DELETED
189+
originalRoom.status == RoomStatus.COMPLETED ||
190+
originalRoom.status == RoomStatus.DELETED
191191
) {
192192
throw new BadRequestException('이미 종료된 방입니다.');
193-
} else if (room.status == RoomStatus.IN_SETTLEMENT) {
193+
} else if (originalRoom.status == RoomStatus.IN_SETTLEMENT) {
194194
throw new BadRequestException('정산이 진행 중인 방입니다.');
195195
}
196-
if (!(user.userType == UserType.admin || room.ownerUuid == user.uuid)) {
196+
if (
197+
!(user.userType == UserType.admin || originalRoom.ownerUuid == user.uuid)
198+
) {
197199
throw new UnauthorizedException('방장 또는 관리자가 아닙니다.');
198200
}
199201

@@ -206,7 +208,7 @@ export class RoomService {
206208
}
207209

208210
// 출발 시간 변경 시 출발전 알림 여부 초기화
209-
let departureAlertSent = room.departureAlertSent;
211+
let departureAlertSent = originalRoom.departureAlertSent;
210212
if (updateRoomDto.departureTime) {
211213
departureAlertSent = false;
212214
}
@@ -216,10 +218,46 @@ export class RoomService {
216218
{ ...updateRoomDto, departureAlertSent: departureAlertSent },
217219
);
218220

219-
return await this.findOne(uuid);
221+
const updatedRoom = await this.findOne(uuid);
222+
223+
// 관리자가 수정하는 경우 변경사항을 포함한 로그 남기기
224+
if (user.userType == UserType.admin) {
225+
const roomDiff = this.getRoomDiff(originalRoom, updatedRoom);
226+
const changes = Object.keys(roomDiff)
227+
.map((key) => {
228+
const originalValue = originalRoom[key];
229+
const updatedValue = roomDiff[key];
230+
let originalStr = originalValue;
231+
let updatedStr = updatedValue;
232+
233+
// Date 타입 포맷팅
234+
if (originalValue instanceof Date) {
235+
originalStr = this.formatKst(originalValue);
236+
}
237+
if (updatedValue instanceof Date) {
238+
updatedStr = this.formatKst(updatedValue);
239+
}
240+
241+
// null/undefined 처리
242+
if (originalValue === null || originalValue === undefined) {
243+
originalStr = originalValue === null ? 'null' : 'undefined';
244+
}
245+
if (updatedValue === null || updatedValue === undefined) {
246+
updatedStr = updatedValue === null ? 'null' : 'undefined';
247+
}
248+
249+
return ` - ${key}: "${originalStr}" → "${updatedStr}"`;
250+
})
251+
.join('\n');
252+
253+
const logMessage = `[관리자 방 수정] 관리자 UUID: ${user.uuid}, 방 UUID: ${uuid}, 방 제목: ${originalRoom.title}\n변경사항:\n${changes || ' (변경사항 없음)'}`;
254+
this.logger.log(logMessage);
255+
}
256+
257+
return updatedRoom;
220258
}
221259

222-
async remove(uuid: string, userUuid: string) {
260+
async remove(uuid: string, userUuid: string, userType?: UserType) {
223261
const room = await this.findOne(uuid);
224262
if (!room) {
225263
throw new NotFoundException('방이 존재하지 않습니다.');
@@ -230,9 +268,27 @@ export class RoomService {
230268
if (room.status == RoomStatus.IN_SETTLEMENT) {
231269
throw new BadRequestException('이미 정산이 진행되고 있습니다.');
232270
}
233-
if (room.ownerUuid != userUuid) {
234-
throw new UnauthorizedException('방장이 아닙니다.');
271+
if (!(userType == UserType.admin || room.ownerUuid == userUuid)) {
272+
throw new UnauthorizedException('방장 또는 관리자가 아닙니다.');
273+
}
274+
275+
// 관리자가 삭제하는 경우 상세 로그 남기기
276+
if (userType == UserType.admin) {
277+
const roomInfo = [
278+
` - 방 제목: ${room.title}`,
279+
` - 출발지: ${room.departureLocation || '(없음)'}`,
280+
` - 도착지: ${room.destinationLocation || '(없음)'}`,
281+
` - 출발 시간: ${this.formatKst(room.departureTime)}`,
282+
` - 최대 인원: ${room.maxParticipant}명`,
283+
` - 현재 인원: ${room.currentParticipant}명`,
284+
` - 방장 UUID: ${room.ownerUuid}`,
285+
` - 방 상태: ${room.status}`,
286+
].join('\n');
287+
288+
const logMessage = `[관리자 방 삭제] 관리자 UUID: ${userUuid}, 방 UUID: ${uuid}\n방 정보:\n${roomInfo}`;
289+
this.logger.log(logMessage);
235290
}
291+
236292
await this.roomRepo.update({ uuid: uuid }, { status: RoomStatus.DELETED });
237293
return uuid;
238294
}

0 commit comments

Comments
 (0)