File size: 6,256 Bytes
1cf0854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import express from 'express';
import { database } from '../db/database';
import { AuthenticatedRequest } from '../middleware/auth';

const router = express.Router();

// Get dashboard metrics
router.get('/metrics', async (req: AuthenticatedRequest, res) => {
  try {
    const { tenantId } = req.user!;
    
    // Get total conversations
    const totalConversations = await database.get(
      'SELECT COUNT(*) as count FROM chat_sessions WHERE tenant_id = ?',
      [tenantId]
    );

    // Get conversations this month
    const thisMonthConversations = await database.get(
      `SELECT COUNT(*) as count FROM chat_sessions 
       WHERE tenant_id = ? AND started_at >= date('now', 'start of month')`,
      [tenantId]
    );

    // Get average rating
    const avgRating = await database.get(
      'SELECT AVG(rating) as avg FROM chat_sessions WHERE tenant_id = ? AND rating IS NOT NULL',
      [tenantId]
    );

    // Get resolution rate
    const resolutionRate = await database.get(
      `SELECT 
         COUNT(CASE WHEN resolved = 1 THEN 1 END) * 100.0 / COUNT(*) as rate
       FROM chat_sessions WHERE tenant_id = ?`,
      [tenantId]
    );

    // Get knowledge base count
    const knowledgeBaseCount = await database.get(
      'SELECT COUNT(*) as count FROM knowledge_base WHERE tenant_id = ? AND status = "active"',
      [tenantId]
    );

    res.json({
      totalConversations: totalConversations.count || 0,
      thisMonthConversations: thisMonthConversations.count || 0,
      averageRating: parseFloat((avgRating.avg || 0).toFixed(1)),
      resolutionRate: parseFloat((resolutionRate.rate || 0).toFixed(1)),
      knowledgeBaseDocuments: knowledgeBaseCount.count || 0
    });
  } catch (error) {
    console.error('Get metrics error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get conversation trends
router.get('/conversations', async (req: AuthenticatedRequest, res) => {
  try {
    const { tenantId } = req.user!;
    const { period = '7d' } = req.query;

    let dateRange = '';
    switch (period) {
      case '1d':
        dateRange = "date('now', '-1 day')";
        break;
      case '7d':
        dateRange = "date('now', '-7 days')";
        break;
      case '30d':
        dateRange = "date('now', '-30 days')";
        break;
      default:
        dateRange = "date('now', '-7 days')";
    }

    const conversations = await database.query(
      `SELECT 
         date(started_at) as date,
         COUNT(*) as count,
         AVG(CASE WHEN rating IS NOT NULL THEN rating END) as avg_rating,
         COUNT(CASE WHEN resolved = 1 THEN 1 END) * 100.0 / COUNT(*) as resolution_rate
       FROM chat_sessions 
       WHERE tenant_id = ? AND started_at >= ${dateRange}
       GROUP BY date(started_at)
       ORDER BY date`,
      [tenantId]
    );

    res.json({ conversations });
  } catch (error) {
    console.error('Get conversations error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get chat history with filters
router.get('/chat-history', async (req: AuthenticatedRequest, res) => {
  try {
    const { tenantId } = req.user!;
    const { limit = 50, offset = 0, status, sentiment } = req.query;

    let whereClause = 'WHERE cs.tenant_id = ?';
    const params = [tenantId];

    if (status === 'resolved') {
      whereClause += ' AND cs.resolved = 1';
    } else if (status === 'unresolved') {
      whereClause += ' AND cs.resolved = 0';
    }

    const conversations = await database.query(
      `SELECT 
         cs.id, cs.session_token, cs.started_at, cs.ended_at, cs.resolved, cs.rating, cs.feedback,
         (SELECT message FROM chat_messages WHERE session_id = cs.id AND sender = 'user' ORDER BY timestamp LIMIT 1) as first_message,
         (SELECT COUNT(*) FROM chat_messages WHERE session_id = cs.id) as message_count
       FROM chat_sessions cs
       ${whereClause}
       ORDER BY cs.started_at DESC
       LIMIT ? OFFSET ?`,
      [...params, limit, offset]
    );

    res.json({ conversations });
  } catch (error) {
    console.error('Get chat history error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get top questions/issues
router.get('/top-questions', async (req: AuthenticatedRequest, res) => {
  try {
    const { tenantId } = req.user!;
    
    const topQuestions = await database.query(
      `SELECT 
         cm.message,
         COUNT(*) as frequency
       FROM chat_messages cm
       JOIN chat_sessions cs ON cm.session_id = cs.id
       WHERE cs.tenant_id = ? AND cm.sender = 'user'
       GROUP BY cm.message
       HAVING frequency > 1
       ORDER BY frequency DESC
       LIMIT 10`,
      [tenantId]
    );

    res.json({ topQuestions });
  } catch (error) {
    console.error('Get top questions error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get sentiment analysis
router.get('/sentiment', async (req: AuthenticatedRequest, res) => {
  try {
    const { tenantId } = req.user!;
    
    // For now, we'll simulate sentiment analysis
    // In a real app, you'd analyze message content
    const totalSessions = await database.get(
      'SELECT COUNT(*) as count FROM chat_sessions WHERE tenant_id = ?',
      [tenantId]
    );

    const resolvedSessions = await database.get(
      'SELECT COUNT(*) as count FROM chat_sessions WHERE tenant_id = ? AND resolved = 1',
      [tenantId]
    );

    const highRatingSessions = await database.get(
      'SELECT COUNT(*) as count FROM chat_sessions WHERE tenant_id = ? AND rating >= 4',
      [tenantId]
    );

    const total = totalSessions.count || 1;
    const positive = (resolvedSessions.count || 0) * 0.7 + (highRatingSessions.count || 0) * 0.3;
    const negative = total * 0.1; // Assume 10% negative
    const neutral = total - positive - negative;

    res.json({
      sentiment: {
        positive: Math.round((positive / total) * 100),
        neutral: Math.round((neutral / total) * 100),
        negative: Math.round((negative / total) * 100)
      }
    });
  } catch (error) {
    console.error('Get sentiment error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

export default router;