Showing
2 changed files
with
0 additions
and
84 deletions
1 | -import { isAuthenticated, prisma } from "../../../utils"; | ||
2 | -import { ONE_TO_ONE_MESSAGE } from "../../../topics"; | ||
3 | - | ||
4 | -export default { | ||
5 | - Mutation: { | ||
6 | - newMessage: async (_, args, { request, pubsub }) => { | ||
7 | - isAuthenticated(request); | ||
8 | - const { user } = request; | ||
9 | - const { receiverEmail, message, roomId } = args; | ||
10 | - const receiver = await prisma.user.findOne({ | ||
11 | - where: { | ||
12 | - email: receiverEmail, | ||
13 | - }, | ||
14 | - }); | ||
15 | - | ||
16 | - let room = await prisma.room.findOne({ | ||
17 | - where: { | ||
18 | - id: roomId, | ||
19 | - }, | ||
20 | - }); | ||
21 | - | ||
22 | - // 방이 없는 경우 | ||
23 | - if (room === undefined || room === null) { | ||
24 | - // 보내는 사람과 받는 사람이 다른 경우 | ||
25 | - if (user.id !== receiver.id) { | ||
26 | - room = await prisma.room.create({ | ||
27 | - data: { | ||
28 | - participants: { | ||
29 | - connect: [{ id: receiver.id }, { id: user.id }], | ||
30 | - }, | ||
31 | - }, | ||
32 | - }); | ||
33 | - } else { | ||
34 | - // 자기 자신에게 보내는 경우 | ||
35 | - room = await prisma.room.create({ | ||
36 | - data: { | ||
37 | - participants: { | ||
38 | - connect: [{ id: user.id }], | ||
39 | - }, | ||
40 | - }, | ||
41 | - }); | ||
42 | - } | ||
43 | - } else { | ||
44 | - // 방이 원래 있던 경우 업데이트 | ||
45 | - room = await prisma.room.update({ | ||
46 | - where: { | ||
47 | - id: roomId, | ||
48 | - }, | ||
49 | - data: { | ||
50 | - participants: { | ||
51 | - connect: [{ id: user.id }, { id: receiver.id }], | ||
52 | - }, | ||
53 | - }, | ||
54 | - }); | ||
55 | - } | ||
56 | - | ||
57 | - if (!room) { | ||
58 | - throw new Error("There is no room"); | ||
59 | - } | ||
60 | - | ||
61 | - const subMessage = await prisma.message.create({ | ||
62 | - data: { | ||
63 | - text: message, | ||
64 | - to: { | ||
65 | - connect: { id: receiver.id }, | ||
66 | - }, | ||
67 | - from: { | ||
68 | - connect: { id: user.id }, | ||
69 | - }, | ||
70 | - room: { | ||
71 | - connect: { | ||
72 | - id: room.id, | ||
73 | - }, | ||
74 | - }, | ||
75 | - }, | ||
76 | - }); | ||
77 | - pubsub.publish(ONE_TO_ONE_MESSAGE, subMessage); | ||
78 | - return subMessage; | ||
79 | - }, | ||
80 | - }, | ||
81 | -}; |
-
Please register or login to post a comment