| 4 | import { cors, preflight } from 'utilities/cors'; |
| 5 | |
| 6 | async function create(request: NextApiRequest, response: NextApiResponse) { |
| 7 | const { |
| 8 | communityId, |
| 9 | messageId, |
| 10 | type, |
| 11 | action, |
| 12 | }: { |
| 13 | communityId?: string; |
| 14 | messageId?: string; |
| 15 | type?: string; |
| 16 | action?: string; |
| 17 | } = request.body; |
| 18 | if (!communityId || !messageId || !type || !action) { |
| 19 | return response.status(400).end(); |
| 20 | } |
| 21 | const permissions = await PermissionsService.get({ |
| 22 | request, |
| 23 | response, |
| 24 | params: { communityId }, |
| 25 | }); |
| 26 | if (!permissions.is_member || !permissions.user?.id) { |
| 27 | return response.status(401).end(); |
| 28 | } |
| 29 | const message = await prisma.messages.findUnique({ |
| 30 | where: { id: messageId }, |
| 31 | include: { |
| 32 | channel: true, |
| 33 | }, |
| 34 | }); |
| 35 | |
| 36 | if (!message) { |
| 37 | return response.status(404).json({}); |
| 38 | } |
| 39 | |
| 40 | if (message.channel.accountId !== communityId) { |
| 41 | return response.status(401).json({}); |
| 42 | } |
| 43 | |
| 44 | const reaction = await prisma.messageReactions.findFirst({ |
| 45 | where: { |
| 46 | messagesId: messageId, |
| 47 | name: type, |
| 48 | }, |
| 49 | }); |
| 50 | |
| 51 | const userId = permissions.user.id; |
| 52 | |
| 53 | if (!reaction) { |
| 54 | await prisma.messageReactions.create({ |
| 55 | data: { |
| 56 | messagesId: messageId, |
| 57 | name: type, |
| 58 | count: 1, |
| 59 | users: [userId as Prisma.JsonValue] as Prisma.JsonArray, |
| 60 | }, |
| 61 | }); |
| 62 | } else { |
| 63 | let { users } = reaction; |