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

[ST-625] Feat/normal question #66

Merged
merged 5 commits into from
Sep 28, 2023
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
7 changes: 6 additions & 1 deletion src/chatting/chatting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/offer/dto/accept-offer.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsDate } from 'class-validator';

export class AcceptOfferDto {
@ApiProperty({
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions src/offer/offer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export class OfferController {
AccessToken.userId(headers),
acceptOfferDto.chattingId,
questionId,
acceptOfferDto.startTime,
acceptOfferDto.endTime,
);
}
}
19 changes: 7 additions & 12 deletions src/offer/offer.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('선생님은 질문을 수락할 수 없습니다.');
Expand All @@ -108,23 +113,13 @@ 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',
selectedTeacherId: teacherId,
tutoringId,
},
);

return tutoring;
}
}
50 changes: 41 additions & 9 deletions src/offer/offer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
) {}

Expand Down Expand Up @@ -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으로 변경
Expand Down Expand Up @@ -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 =
Expand All @@ -131,7 +163,7 @@ export class OfferService {
offerTeacherId,
teacherChatId,
'text',
'죄송합니다.\n다른 선생님과 수업을 진행하기로 했습니다.',
JSON.stringify(rejectMessage),
);
}
}
Expand Down