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

Feat/start selected question #52

Merged
merged 11 commits into from
Sep 20, 2023
30 changes: 30 additions & 0 deletions src/chatting/chatting.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Chatting, ChattingKey } from './entities/chatting.interface';
import { Injectable } from '@nestjs/common';
import { InjectModel, Model } from 'nestjs-dynamoose';
import { v4 as uuid } from 'uuid';

@Injectable()
export class ChattingRepository {
Expand Down Expand Up @@ -50,6 +51,22 @@ export class ChattingRepository {
}
}*/

async sendMessage(roomId: string, senderId: string, format: string, message) {
const chatting = await this.chattingModel.get({ id: roomId });
chatting.messages.push({
sender: senderId,
format: format,
body: JSON.stringify(message),
createdAt: new Date().toISOString(),
});
await this.chattingModel.update(
{ id: roomId },
{ messages: chatting.messages },
);

return chatting;
}

async getChatRoomInfo(roomId: string) {
return await this.chattingModel.get({
id: roomId,
Expand All @@ -60,6 +77,19 @@ export class ChattingRepository {
return await this.chattingModel.batchGet(roomIds);
}

async makeChatRoom(teacherId: string, studentId: string, questionId: string) {
const chattingRoomId = uuid();
const chatting: Chatting = {
id: chattingRoomId,
teacherId,
studentId,
questionId,
messages: [],
};
await this.chattingModel.create(chatting);
return chattingRoomId;
}

/*
async sendMessage(senderId: string, receiverId: string, message: string) {
try {
Expand Down
24 changes: 14 additions & 10 deletions src/chatting/chatting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,29 @@ export class ChattingService {
const questionInfo = await this.questionRepository.getInfo(
roomInfo.questionId,
);
console.log(questionInfo);
const { status, isSelect } = questionInfo;
const { schoolSubject, schoolLevel } = questionInfo.problem;
const { schoolSubject, schoolLevel, description } =
questionInfo.problem;

const item = {
roomImage: undefined,
id: roomInfo.id,
messages: roomInfo.messages,
messages: roomInfo.messages.map((message) => {
const { body, ...rest } = message;
return { body: JSON.parse(body), ...rest };
}),
opponentId: undefined,
questionState: status,
problemImages: questionInfo.problem.mainImage,
isSelect: isSelect,
isTeacherRoom: true,
questionId: roomInfo.questionId,
schoolSubject: schoolSubject,
schoolLevel: schoolLevel,
title: undefined,
status: status,
description: description,
};

if (userRole == 'student') {
Expand Down Expand Up @@ -100,28 +107,25 @@ export class ChattingService {
);
} else {
normalProposedGrouping[roomInfo.questionId] = {
...roomInfo,
teachers: [roomInfo],
isTeacherRoom: false,
questionImage: roomInfo.problemImages,
title: roomInfo.description,
subject: roomInfo.schoolSubject,
};
}
} else {
if (result.normalProposed == undefined) {
result.normalProposed = [];
}
result.normalProposed.push(roomInfo);
}
} else if (roomInfo.status === 'reserved') {
} else {
result.normalReserved.push(roomInfo);
}
}
});
}
if (Object.keys(normalProposedGrouping).length > 0) {
result.normalProposed = Object.values(result.normalProposed);
result.normalProposed = Object.values(normalProposedGrouping);
}

console.log(result);
return new Success('채팅방 목록을 불러왔습니다.', result);
} catch (error) {
return new Fail(error.message);
Expand Down
1 change: 1 addition & 0 deletions src/chatting/description/chatting.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const ChattingListEntity = {
questionState: 'pending',
opponentId: 'test-student-id',
isSelect: false,
isTeacherRoom: false,
questionId: '2397c9a0-9ea7-4584-8d02-e6509ea22aba',
schoolSubject: '기하',
schoolLevel: '고등학교',
Expand Down
10 changes: 3 additions & 7 deletions src/chatting/entities/chatting.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ export interface ChattingKey {
}

export interface Message {
type: string;
body: string;
}

export interface Chat {
sender: string;
message: Message;
format: string;
body: string;
createdAt: string;
}

export interface Chatting extends ChattingKey {
studentId: string;
teacherId: string;
questionId: string;
messages: Chat[];
messages: Message[];
}
5 changes: 4 additions & 1 deletion src/chatting/entities/chatting.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ export const ChattingMessageSchema = new Schema({
sender: {
type: String,
},
message: {
body: {
type: String,
},
createdAt: {
type: String,
},
format: {
type: String,
},
});

export const ChattingSchema = new Schema({
Expand Down
12 changes: 7 additions & 5 deletions src/offer/offer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { AccessToken } from '../auth/entities/auth.entity';
import { OfferOperations } from './descriptions/offer.operation';
import { OfferParam } from './descriptions/offer.param';
import { OfferResponse } from './descriptions/offer.response';
import { AcceptOfferDto } from './dto/accept-offer.dto';
import { OfferService } from './offer.service';
import { Body, Controller, Get, Headers, Param, Post } from '@nestjs/common';
import {
ApiBearerAuth,
ApiOperation,
ApiParam,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';

Expand All @@ -26,13 +24,16 @@ export class OfferController {
return this.offerService.append(AccessToken.userId(headers), questionId);
}

/*
@ApiTags('Teacher')
@Post('teacher/offer/remove/:questionId')
@ApiParam(OfferParam.questionId)
@ApiOperation(OfferOperations.remove)
remove(@Headers() headers: Headers, @Param('questionId') questionId: string) {
return this.offerService.remove(AccessToken.userId(headers), questionId);
}
}*/

/*

@ApiTags('Teacher')
@Get('teacher/offer/status/:questionId')
Expand All @@ -44,8 +45,9 @@ export class OfferController {
@Param('questionId') questionId: string,
) {
return this.offerService.getStatus(AccessToken.userId(headers), questionId);
}
}*/

/*
@ApiTags('Student')
@Get('student/offer/teachers/:questionId')
@ApiParam(OfferParam.questionId)
Expand All @@ -58,7 +60,7 @@ export class OfferController {
AccessToken.userId(headers),
questionId,
);
}
}*/

@ApiTags('Student')
@Post('student/offer/accept/:questionId')
Expand Down
2 changes: 2 additions & 0 deletions src/offer/offer.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AgoraModule } from '../agora/agora.module';
import { ChattingRepository } from '../chatting/chatting.repository';
import { dynamooseModule } from '../config.dynamoose';
import { QuestionRepository } from '../question/question.repository';
import { TutoringRepository } from '../tutoring/tutoring.repository';
Expand All @@ -19,6 +20,7 @@ import { Module } from '@nestjs/common';
QuestionRepository,
TutoringRepository,
UploadRepository,
ChattingRepository,
],
})
export class OfferModule {}
53 changes: 28 additions & 25 deletions src/offer/offer.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ export class OfferRepository {
private readonly tutoringRepository: TutoringRepository,
) {}

async append(userId: string, questionId: string) {
const teacher = await this.userRepository.get(userId);
if (teacher.role === 'student') {
throw new Error('선생님만 질문 대기열에 추가할 수 있습니다.');
}

/**
* 일반 질문에 선생님의 신청을 추가함
*/
async appendOfferTeacherRoom(questionId: string, chattingId: string) {
const question = await this.questionModel.get({ id: questionId });
if (question.teacherIds.includes(userId)) {
if (question.offerTeacherRooms.includes(chattingId)) {
throw new Error('이미 질문 대기열에 추가되었습니다.');
}

question.teacherIds.push(userId);
question.offerTeacherRooms.push(chattingId);
await this.questionModel.update(
{ id: questionId },
{ teacherIds: question.teacherIds },
{ offerTeacherRooms: question.offerTeacherRooms },
);
}

/*
async getTeachers(userId: string, questionId: string) {
const user = await this.userRepository.get(userId);

Expand All @@ -46,20 +45,24 @@ export class OfferRepository {
return await this.userRepository.getOther(teacherId);
});
return await Promise.all(teachers);
}

async remove(userId: string, questionId: string) {
const question = await this.questionModel.get({ id: questionId });
if (!question.teacherIds.includes(userId)) {
throw new Error('질문 대기열에 존재하지 않습니다.');
}*/

/*
async remove(chattingId: string, questionId: string) {
const question = await this.questionModel.get({ id: questionId });
if (!question.offerTeacherRooms.includes(userId)) {
throw new Error('질문 대기열에 존재하지 않습니다.');
}

question.teacherIds = question.teacherIds.filter((id) => id !== userId);
await this.offerTeacherRooms.update(
{ id: questionId },
{ teacherIds: question.teacherIds },
);
}
]*/

question.teacherIds = question.teacherIds.filter((id) => id !== userId);
await this.questionModel.update(
{ id: questionId },
{ teacherIds: question.teacherIds },
);
}
/*

async getStatus(userId: string, questionId: string) {
const question = await this.questionModel.get({ id: questionId });
Expand Down Expand Up @@ -88,9 +91,9 @@ export class OfferRepository {
status: 'rejected',
};
}
}
}*/

async accept(userId: string, questionId: string, teacherId: string) {
async accept(userId: string, questionId: string, chattingId: string) {
const user: User = await this.userRepository.get(userId);
if (user.role === 'teacher') {
throw new Error('선생님은 질문을 수락할 수 없습니다.');
Expand All @@ -101,7 +104,7 @@ export class OfferRepository {
throw new Error('질문을 찾을 수 없습니다.');
} else if (question.studentId !== userId) {
throw new Error('본인의 질문이 아닙니다.');
} else if (!question.teacherIds.includes(teacherId)) {
} else if (!question.offerTeacherRooms.includes(chattingId)) {
throw new Error('질문 대기열에 존재하지 않는 선생님입니다.');
}

Expand All @@ -117,7 +120,7 @@ export class OfferRepository {
{ id: questionId },
{
status: 'matched',
selectedTeacherId: teacherId,
selectedTeacherId: chattingId,
tutoringId,
},
);
Expand Down
Loading