sdy

update newMessage

type Mutation {
newMessage(receiverId: Int, message: String!, roomId: Int): Message!
newMessage(receiverEmail: String, message: String!, roomId: Int): Message!
}
......
import { isAuthenticated, prisma } from "../../../utils";
import { ONE_TO_ONE_MESSAGE } from "../../../topics";
// newMessage 에서는 1:1, 1:n 메시지 방만 가정하고 있음
// 나머지 랜덤채팅은 아직 구상중 (5/4) 기준
export default {
Mutation: {
newMessage: async (_, args, { request, pubsub }) => {
isAuthenticated(request);
const { user } = request;
const { receiverId, message, roomId } = args;
let room = await prisma.room.findOne({
const { receiverEmail, message, roomId } = args;
const receiver = await prisma.user.findOne({
where: {
id: roomId,
email: receiverEmail,
},
});
room = await prisma.room.update({
let room = await prisma.room.findOne({
where: {
id: roomId,
},
data: {
participants: {
connect: [{ id: user.id }, { id: receiverId }],
},
},
});
// 방이 없는 경우
if (room === undefined || room === null) {
// 보내는 사람과 받는 사람이 다른 경우
if (user.id !== receiverId) {
if (user.id !== receiver.id) {
room = await prisma.room.create({
data: {
participants: {
connect: [{ id: receiverId }, { id: user.id }],
connect: [{ id: receiver.id }, { id: user.id }],
},
},
});
......@@ -43,23 +42,32 @@ export default {
},
});
}
} else {
// 방이 원래 있던 경우 업데이트
room = await prisma.room.update({
where: {
id: roomId,
},
data: {
participants: {
connect: [{ id: user.id }, { id: receiver.id }],
},
},
});
}
if (!room) {
throw new Error("There is no room");
}
const toUser = await prisma.user.findOne({
where: {
id: receiverId,
},
});
const subMessage = await prisma.message.create({
data: {
text: message,
to: {
connect: [{ id: toUser.id }],
connect: { id: receiver.id },
},
from: {
connect: [{ id: user.id }],
connect: { id: user.id },
},
room: {
connect: {
......