| 6 | import { cors, preflight } from 'utilities/cors'; |
| 7 | |
| 8 | export async function create({ |
| 9 | messageId, |
| 10 | threadId, |
| 11 | communityId, |
| 12 | permissions, |
| 13 | }: { |
| 14 | messageId: string; |
| 15 | threadId: string; |
| 16 | communityId: string; |
| 17 | permissions: PermissionsType; |
| 18 | }) { |
| 19 | if (!messageId || !threadId) { |
| 20 | return { status: 400 }; |
| 21 | } |
| 22 | |
| 23 | const community = await CommunityService.find({ communityId }); |
| 24 | |
| 25 | if (!community) { |
| 26 | return { status: 404 }; |
| 27 | } |
| 28 | |
| 29 | const message = await prisma.messages.findUnique({ |
| 30 | where: { id: messageId }, |
| 31 | include: { channel: true, threads: true }, |
| 32 | }); |
| 33 | const thread = await prisma.threads.findUnique({ |
| 34 | where: { id: threadId }, |
| 35 | include: { messages: true, channel: true }, |
| 36 | }); |
| 37 | if (!thread || !message) { |
| 38 | return { |
| 39 | status: 404, |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | const channel = await prisma.channels.findUnique({ |
| 44 | where: { id: message.channelId }, |
| 45 | }); |
| 46 | |
| 47 | if (!channel) { |
| 48 | return { status: 404 }; |
| 49 | } |
| 50 | |
| 51 | if ( |
| 52 | channel.accountId !== communityId || |
| 53 | thread.channel.accountId !== communityId |
| 54 | ) { |
| 55 | return { status: 404 }; |
| 56 | } |
| 57 | |
| 58 | if (!message.threadId) { |
| 59 | return { status: 404 }; |
| 60 | } |
| 61 | |
| 62 | if (!permissions.user) { |
| 63 | return { status: 404 }; |
| 64 | } |
| 65 | |