|
| 1 | +/** |
| 2 | + * AI Feedback Routes |
| 3 | + * Endpoints for reading AI response feedback (π/π) stats. |
| 4 | + * |
| 5 | + * Mounted at /api/v1/guilds/:id/ai-feedback |
| 6 | + */ |
| 7 | + |
| 8 | +import { Router } from 'express'; |
| 9 | +import { error as logError } from '../../logger.js'; |
| 10 | +import { rateLimit } from '../middleware/rateLimit.js'; |
| 11 | +import { requireGuildAdmin, validateGuild } from './guilds.js'; |
| 12 | + |
| 13 | +const router = Router({ mergeParams: true }); |
| 14 | + |
| 15 | +/** Rate limiter: 60 requests / 1 min per IP */ |
| 16 | +const feedbackRateLimit = rateLimit({ windowMs: 60 * 1000, max: 60 }); |
| 17 | + |
| 18 | +// ββ GET /stats βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 19 | + |
| 20 | +/** |
| 21 | + * @openapi |
| 22 | + * /guilds/{id}/ai-feedback/stats: |
| 23 | + * get: |
| 24 | + * tags: |
| 25 | + * - AI Feedback |
| 26 | + * summary: Get AI feedback statistics |
| 27 | + * description: Returns aggregate π/π feedback counts and daily trend for a guild. |
| 28 | + * security: |
| 29 | + * - ApiKeyAuth: [] |
| 30 | + * - BearerAuth: [] |
| 31 | + * parameters: |
| 32 | + * - in: path |
| 33 | + * name: id |
| 34 | + * required: true |
| 35 | + * schema: |
| 36 | + * type: string |
| 37 | + * description: Guild ID |
| 38 | + * - in: query |
| 39 | + * name: days |
| 40 | + * schema: |
| 41 | + * type: integer |
| 42 | + * default: 30 |
| 43 | + * minimum: 1 |
| 44 | + * maximum: 90 |
| 45 | + * description: Number of days for the trend window |
| 46 | + * responses: |
| 47 | + * "200": |
| 48 | + * description: Feedback statistics |
| 49 | + * content: |
| 50 | + * application/json: |
| 51 | + * schema: |
| 52 | + * type: object |
| 53 | + * properties: |
| 54 | + * positive: |
| 55 | + * type: integer |
| 56 | + * negative: |
| 57 | + * type: integer |
| 58 | + * total: |
| 59 | + * type: integer |
| 60 | + * ratio: |
| 61 | + * type: integer |
| 62 | + * nullable: true |
| 63 | + * description: Positive feedback percentage (0β100), or null if no feedback |
| 64 | + * trend: |
| 65 | + * type: array |
| 66 | + * items: |
| 67 | + * type: object |
| 68 | + * properties: |
| 69 | + * date: |
| 70 | + * type: string |
| 71 | + * format: date |
| 72 | + * positive: |
| 73 | + * type: integer |
| 74 | + * negative: |
| 75 | + * type: integer |
| 76 | + * "401": |
| 77 | + * $ref: "#/components/responses/Unauthorized" |
| 78 | + * "403": |
| 79 | + * $ref: "#/components/responses/Forbidden" |
| 80 | + * "429": |
| 81 | + * $ref: "#/components/responses/RateLimited" |
| 82 | + * "500": |
| 83 | + * $ref: "#/components/responses/ServerError" |
| 84 | + * "503": |
| 85 | + * $ref: "#/components/responses/ServiceUnavailable" |
| 86 | + */ |
| 87 | +router.get('/stats', feedbackRateLimit, requireGuildAdmin, validateGuild, async (req, res) => { |
| 88 | + const { dbPool } = req.app.locals; |
| 89 | + if (!dbPool) { |
| 90 | + return res.status(503).json({ error: 'Database not available' }); |
| 91 | + } |
| 92 | + |
| 93 | + const guildId = req.params.id; |
| 94 | + |
| 95 | + let days = 30; |
| 96 | + if (req.query.days !== undefined) { |
| 97 | + const parsed = Number.parseInt(req.query.days, 10); |
| 98 | + if (!Number.isNaN(parsed) && parsed >= 1 && parsed <= 90) { |
| 99 | + days = parsed; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + const [statsResult, trendResult] = await Promise.all([ |
| 105 | + dbPool.query( |
| 106 | + `SELECT |
| 107 | + COUNT(*) FILTER (WHERE feedback_type = 'positive')::int AS positive, |
| 108 | + COUNT(*) FILTER (WHERE feedback_type = 'negative')::int AS negative, |
| 109 | + COUNT(*)::int AS total |
| 110 | + FROM ai_feedback |
| 111 | + WHERE guild_id = $1`, |
| 112 | + [guildId], |
| 113 | + ), |
| 114 | + dbPool.query( |
| 115 | + `SELECT |
| 116 | + DATE(created_at) AS date, |
| 117 | + COUNT(*) FILTER (WHERE feedback_type = 'positive')::int AS positive, |
| 118 | + COUNT(*) FILTER (WHERE feedback_type = 'negative')::int AS negative |
| 119 | + FROM ai_feedback |
| 120 | + WHERE guild_id = $1 |
| 121 | + AND created_at >= NOW() - ($2 * interval '1 day') |
| 122 | + GROUP BY DATE(created_at) |
| 123 | + ORDER BY date ASC`, |
| 124 | + [guildId, days], |
| 125 | + ), |
| 126 | + ]); |
| 127 | + |
| 128 | + const row = statsResult.rows[0]; |
| 129 | + const positive = row?.positive || 0; |
| 130 | + const negative = row?.negative || 0; |
| 131 | + const total = row?.total || 0; |
| 132 | + const ratio = total > 0 ? Math.round((positive / total) * 100) : null; |
| 133 | + |
| 134 | + res.json({ |
| 135 | + positive, |
| 136 | + negative, |
| 137 | + total, |
| 138 | + ratio, |
| 139 | + trend: trendResult.rows.map((r) => ({ |
| 140 | + date: r.date, |
| 141 | + positive: r.positive, |
| 142 | + negative: r.negative, |
| 143 | + })), |
| 144 | + }); |
| 145 | + } catch (err) { |
| 146 | + logError('Failed to fetch AI feedback stats', { error: err.message, guild: guildId }); |
| 147 | + res.status(500).json({ error: 'Failed to fetch AI feedback stats' }); |
| 148 | + } |
| 149 | +}); |
| 150 | + |
| 151 | +// ββ GET /recent ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 152 | + |
| 153 | +/** |
| 154 | + * @openapi |
| 155 | + * /guilds/{id}/ai-feedback/recent: |
| 156 | + * get: |
| 157 | + * tags: |
| 158 | + * - AI Feedback |
| 159 | + * summary: Get recent feedback entries |
| 160 | + * description: Returns the most recent feedback entries for a guild (newest first). |
| 161 | + * security: |
| 162 | + * - ApiKeyAuth: [] |
| 163 | + * - BearerAuth: [] |
| 164 | + * parameters: |
| 165 | + * - in: path |
| 166 | + * name: id |
| 167 | + * required: true |
| 168 | + * schema: |
| 169 | + * type: string |
| 170 | + * description: Guild ID |
| 171 | + * - in: query |
| 172 | + * name: limit |
| 173 | + * schema: |
| 174 | + * type: integer |
| 175 | + * default: 25 |
| 176 | + * maximum: 100 |
| 177 | + * responses: |
| 178 | + * "200": |
| 179 | + * description: Recent feedback entries |
| 180 | + * content: |
| 181 | + * application/json: |
| 182 | + * schema: |
| 183 | + * type: object |
| 184 | + * properties: |
| 185 | + * feedback: |
| 186 | + * type: array |
| 187 | + * items: |
| 188 | + * type: object |
| 189 | + * properties: |
| 190 | + * id: |
| 191 | + * type: integer |
| 192 | + * messageId: |
| 193 | + * type: string |
| 194 | + * channelId: |
| 195 | + * type: string |
| 196 | + * userId: |
| 197 | + * type: string |
| 198 | + * feedbackType: |
| 199 | + * type: string |
| 200 | + * enum: [positive, negative] |
| 201 | + * createdAt: |
| 202 | + * type: string |
| 203 | + * format: date-time |
| 204 | + * "401": |
| 205 | + * $ref: "#/components/responses/Unauthorized" |
| 206 | + * "403": |
| 207 | + * $ref: "#/components/responses/Forbidden" |
| 208 | + * "429": |
| 209 | + * $ref: "#/components/responses/RateLimited" |
| 210 | + * "500": |
| 211 | + * $ref: "#/components/responses/ServerError" |
| 212 | + * "503": |
| 213 | + * $ref: "#/components/responses/ServiceUnavailable" |
| 214 | + */ |
| 215 | +router.get('/recent', feedbackRateLimit, requireGuildAdmin, validateGuild, async (req, res) => { |
| 216 | + const { dbPool } = req.app.locals; |
| 217 | + if (!dbPool) { |
| 218 | + return res.status(503).json({ error: 'Database not available' }); |
| 219 | + } |
| 220 | + |
| 221 | + const guildId = req.params.id; |
| 222 | + |
| 223 | + let limit = 25; |
| 224 | + if (req.query.limit !== undefined) { |
| 225 | + const parsed = Number.parseInt(req.query.limit, 10); |
| 226 | + if (!Number.isNaN(parsed) && parsed >= 1 && parsed <= 100) { |
| 227 | + limit = parsed; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + try { |
| 232 | + const result = await dbPool.query( |
| 233 | + `SELECT id, message_id, channel_id, user_id, feedback_type, created_at |
| 234 | + FROM ai_feedback |
| 235 | + WHERE guild_id = $1 |
| 236 | + ORDER BY created_at DESC |
| 237 | + LIMIT $2`, |
| 238 | + [guildId, limit], |
| 239 | + ); |
| 240 | + |
| 241 | + res.json({ |
| 242 | + feedback: result.rows.map((r) => ({ |
| 243 | + id: r.id, |
| 244 | + messageId: r.message_id, |
| 245 | + channelId: r.channel_id, |
| 246 | + userId: r.user_id, |
| 247 | + feedbackType: r.feedback_type, |
| 248 | + createdAt: r.created_at, |
| 249 | + })), |
| 250 | + }); |
| 251 | + } catch (err) { |
| 252 | + logError('Failed to fetch recent AI feedback', { error: err.message, guild: guildId }); |
| 253 | + res.status(500).json({ error: 'Failed to fetch recent AI feedback' }); |
| 254 | + } |
| 255 | +}); |
| 256 | + |
| 257 | +export default router; |
0 commit comments