From a95ee8263221b7a68c8c77bc787839eee4129f06 Mon Sep 17 00:00:00 2001 From: Seongyun Lee <79950005+seongyunlee@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:24:44 +0900 Subject: [PATCH] [ST-625] Feat/normal question (#66) --- src/chatting/chatting.service.ts | 7 ++++- src/offer/dto/accept-offer.dto.ts | 17 +++++++++++ src/offer/offer.controller.ts | 2 ++ src/offer/offer.repository.ts | 19 +++++------- src/offer/offer.service.ts | 50 +++++++++++++++++++++++++------ 5 files changed, 73 insertions(+), 22 deletions(-) diff --git a/src/chatting/chatting.service.ts b/src/chatting/chatting.service.ts index 6981371..e522ea7 100644 --- a/src/chatting/chatting.service.ts +++ b/src/chatting/chatting.service.ts @@ -101,11 +101,16 @@ export class ChattingService { let status: ChattingStatus; if (questionInfo.status == 'pending') { status = ChattingStatus.pending; - } else if (questionInfo.status == 'reserved') { + } else if ( + questionInfo.status == 'reserved' && + userInfo.role == 'teacher' + ) { status = roomInfo.teacherId == questionInfo.selectedTeacherId ? ChattingStatus.reserved : ChattingStatus.pending; + } else { + status = ChattingStatus.reserved; } let roomImage: string; let opponentInfo: User | undefined; diff --git a/src/offer/dto/accept-offer.dto.ts b/src/offer/dto/accept-offer.dto.ts index 14c073a..dffb64f 100644 --- a/src/offer/dto/accept-offer.dto.ts +++ b/src/offer/dto/accept-offer.dto.ts @@ -1,4 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; +import { IsDate } from 'class-validator'; export class AcceptOfferDto { @ApiProperty({ @@ -7,4 +9,19 @@ export class AcceptOfferDto { '질문의 `teacherIds`에 포함되어 있어야 합니다.', }) chattingId: string; + @Type(() => Date) + @IsDate() + @ApiProperty({ + description: '수업 시작 날짜,시간', + example: '2023-11-30T18:00:00+09:00', + }) + startTime: Date; + + @Type(() => Date) + @IsDate() + @ApiProperty({ + description: '수업 종료 날짜,시간', + example: '2023-11-30T19:00:00+09:00', + }) + endTime: Date; } diff --git a/src/offer/offer.controller.ts b/src/offer/offer.controller.ts index 638e708..cb21619 100644 --- a/src/offer/offer.controller.ts +++ b/src/offer/offer.controller.ts @@ -75,6 +75,8 @@ export class OfferController { AccessToken.userId(headers), acceptOfferDto.chattingId, questionId, + acceptOfferDto.startTime, + acceptOfferDto.endTime, ); } } diff --git a/src/offer/offer.repository.ts b/src/offer/offer.repository.ts index 7157e38..dacbfa4 100644 --- a/src/offer/offer.repository.ts +++ b/src/offer/offer.repository.ts @@ -93,7 +93,12 @@ export class OfferRepository { } }*/ - async accept(userId: string, questionId: string, teacherId: string) { + async accept( + userId: string, + questionId: string, + teacherId: string, + tutoringId: string, + ) { const user: User = await this.userRepository.get(userId); if (user.role === 'teacher') { throw new Error('선생님은 질문을 수락할 수 없습니다.'); @@ -108,15 +113,7 @@ export class OfferRepository { throw new Error('질문 대기열에 존재하지 않는 선생님입니다.'); } - const tutoring = await this.tutoringRepository.create( - questionId, - userId, - question.selectedTeacherId, - ); - - const tutoringId = tutoring.id; - - await this.questionModel.update( + return await this.questionModel.update( { id: questionId }, { status: 'reserved', @@ -124,7 +121,5 @@ export class OfferRepository { tutoringId, }, ); - - return tutoring; } } diff --git a/src/offer/offer.service.ts b/src/offer/offer.service.ts index 1eda952..2c6ebda 100644 --- a/src/offer/offer.service.ts +++ b/src/offer/offer.service.ts @@ -2,6 +2,7 @@ import { ChattingRepository } from '../chatting/chatting.repository'; import { QuestionRepository } from '../question/question.repository'; import { Fail, Success } from '../response'; import { SocketGateway } from '../socket/socket.gateway'; +import { TutoringRepository } from '../tutoring/tutoring.repository'; import { UserRepository } from '../user/user.repository'; import { OfferRepository } from './offer.repository'; import { Injectable } from '@nestjs/common'; @@ -13,6 +14,7 @@ export class OfferService { private readonly userRepository: UserRepository, private readonly chattingRepository: ChattingRepository, private readonly questionRepository: QuestionRepository, + private readonly tutoringRepository: TutoringRepository, private readonly socketGateway: SocketGateway, ) {} @@ -51,13 +53,13 @@ export class OfferService { const requestMessage = { text: '안녕하세요 선생님! 언제 수업 가능하신가요?', }; - console.log( - 'chatRoomId', + + //TODO: redis pub/sub으로 변경 + await this.chattingRepository.sendMessage( chatRoomId, - 'studentId', studentId, - 'userId', - userId, + 'problem-image', + problemMessage, ); //TODO: redis pub/sub으로 변경 @@ -103,21 +105,51 @@ export class OfferService { } }*/ - async accept(userId: string, chattingId: string, questionId: string) { + async accept( + userId: string, + chattingId: string, + questionId: string, + startTime: Date, + endTime: Date, + ) { try { const chatting = await this.chattingRepository.getChatRoomInfo( chattingId, ); - const tutoring = await this.offerRepository.accept( - userId, + const tutoring = await this.tutoringRepository.create( questionId, + userId, chatting.teacherId, ); + await this.tutoringRepository.reserveTutoring( + tutoring.id, + startTime, + endTime, + ); + const question = await this.questionRepository.getInfo(questionId); const offerTeacherIds = question.offerTeachers; + await this.questionRepository.changeStatus(questionId, 'reserved'); + + const confirmMessage = { + text: `선생님과 수업이 확정되었습니다.\n수업 시간은 ${startTime} ~ ${endTime} 입니다.`, + }; + + await this.socketGateway.sendMessageToBothUser( + userId, + chatting.teacherId, + chattingId, + 'text', + JSON.stringify(confirmMessage), + ); + + const rejectMessage = { + text: '죄송합니다.\n다른 선생님과 수업을 진행하기로 했습니다.', + }; + for (const offerTeacherId of offerTeacherIds) { if (offerTeacherId != chatting.teacherId) { const teacherChatId = @@ -131,7 +163,7 @@ export class OfferService { offerTeacherId, teacherChatId, 'text', - '죄송합니다.\n다른 선생님과 수업을 진행하기로 했습니다.', + JSON.stringify(rejectMessage), ); } }