File size: 3,087 Bytes
3d52d95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const prisma = require('../lib/prisma');
const wsManager = require('../lib/ws');

const sendMessage = async (req, res) => {
  const { threadId, messageText } = req.body;
  if (!threadId || !messageText) {
    return res.status(400).json({ error: 'threadId and messageText are required' });
  }

  try {
    const member = await prisma.teamMember.findFirst({
      where: { userId: req.user.userId },
    });
    if (!member) return res.status(404).json({ error: 'No workspace found' });

    const thread = await prisma.exchangeThread.findUnique({ 
      where: { id: threadId },
      include: { giverWorkspace: true, receiverWorkspace: true }
    });
    if (!thread) return res.status(404).json({ error: 'Thread not found' });

    if (thread.status === 'REJECTED') {
      return res.status(403).json({ error: 'This connection was rejected. You cannot send messages.' });
    }

    if (thread.giverWorkspaceId !== member.workspaceId && thread.receiverWorkspaceId !== member.workspaceId) {
      return res.status(403).json({ error: 'Access denied' });
    }

    const message = await prisma.chatMessage.create({
      data: {
        threadId,
        senderUserId: req.user.userId,
        messageText,
      },
      include: {
        sender: { select: { id: true, name: true, email: true } },
      },
    });

    wsManager.broadcastToThread(threadId, message);

    const recipientWorkspaceId = thread.giverWorkspaceId === member.workspaceId ? thread.receiverWorkspaceId : thread.giverWorkspaceId;
    const senderWorkspace = thread.giverWorkspaceId === member.workspaceId ? thread.giverWorkspace : thread.receiverWorkspace;
    wsManager.sendNotification(recipientWorkspaceId, {
      type: 'new_message',
      threadId,
      messageId: message.id,
      senderName: req.user.name || 'Partner',
      senderWorkspaceDomain: senderWorkspace?.domain || 'Partner',
      messageText: message.messageText
    });

    res.status(201).json({ message });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: 'Server error' });
  }
};

const getMessages = async (req, res) => {
  try {
    const member = await prisma.teamMember.findFirst({
      where: { userId: req.user.userId },
    });
    if (!member) return res.status(404).json({ error: 'No workspace found' });

    const thread = await prisma.exchangeThread.findUnique({ where: { id: req.params.threadId } });
    if (!thread) return res.status(404).json({ error: 'Thread not found' });

    if (thread.giverWorkspaceId !== member.workspaceId && thread.receiverWorkspaceId !== member.workspaceId) {
      return res.status(403).json({ error: 'Access denied' });
    }

    const messages = await prisma.chatMessage.findMany({
      where: { threadId: req.params.threadId },
      orderBy: { timestamp: 'asc' },
      include: {
        sender: { select: { id: true, name: true, email: true } },
      },
    });

    res.json({ messages });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: 'Server error' });
  }
};

module.exports = {
  sendMessage,
  getMessages,
};