|
| 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 { getFeedbackStats, getFeedbackTrend, getRecentFeedback } from '../../modules/aiFeedback.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( |
| 88 | + '/stats', |
| 89 | + feedbackRateLimit, |
| 90 | + requireGuildAdmin, |
| 91 | + validateGuild, |
| 92 | + async (req, res, next) => { |
| 93 | + try { |
| 94 | + const guildId = req.params.id; |
| 95 | + |
| 96 | + let days = 30; |
| 97 | + if (req.query.days !== undefined) { |
| 98 | + const parsed = Number.parseInt(req.query.days, 10); |
| 99 | + if (!Number.isNaN(parsed) && parsed >= 1 && parsed <= 90) { |
| 100 | + days = parsed; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + const [stats, trend] = await Promise.all([ |
| 105 | + getFeedbackStats(guildId), |
| 106 | + getFeedbackTrend(guildId, days), |
| 107 | + ]); |
| 108 | + |
| 109 | + res.json({ |
| 110 | + ...stats, |
| 111 | + trend, |
| 112 | + }); |
| 113 | + } catch (err) { |
| 114 | + next(err); |
| 115 | + } |
| 116 | + }, |
| 117 | +); |
| 118 | + |
| 119 | +// ── GET /recent ────────────────────────────────────────────────────────────── |
| 120 | + |
| 121 | +/** |
| 122 | + * @openapi |
| 123 | + * /guilds/{id}/ai-feedback/recent: |
| 124 | + * get: |
| 125 | + * tags: |
| 126 | + * - AI Feedback |
| 127 | + * summary: Get recent feedback entries |
| 128 | + * description: Returns the most recent feedback entries for a guild (newest first). |
| 129 | + * security: |
| 130 | + * - ApiKeyAuth: [] |
| 131 | + * - BearerAuth: [] |
| 132 | + * parameters: |
| 133 | + * - in: path |
| 134 | + * name: id |
| 135 | + * required: true |
| 136 | + * schema: |
| 137 | + * type: string |
| 138 | + * description: Guild ID |
| 139 | + * - in: query |
| 140 | + * name: limit |
| 141 | + * schema: |
| 142 | + * type: integer |
| 143 | + * default: 25 |
| 144 | + * maximum: 100 |
| 145 | + * responses: |
| 146 | + * "200": |
| 147 | + * description: Recent feedback entries |
| 148 | + * content: |
| 149 | + * application/json: |
| 150 | + * schema: |
| 151 | + * type: object |
| 152 | + * properties: |
| 153 | + * feedback: |
| 154 | + * type: array |
| 155 | + * items: |
| 156 | + * type: object |
| 157 | + * properties: |
| 158 | + * id: |
| 159 | + * type: integer |
| 160 | + * messageId: |
| 161 | + * type: string |
| 162 | + * channelId: |
| 163 | + * type: string |
| 164 | + * userId: |
| 165 | + * type: string |
| 166 | + * feedbackType: |
| 167 | + * type: string |
| 168 | + * enum: [positive, negative] |
| 169 | + * createdAt: |
| 170 | + * type: string |
| 171 | + * format: date-time |
| 172 | + * "401": |
| 173 | + * $ref: "#/components/responses/Unauthorized" |
| 174 | + * "403": |
| 175 | + * $ref: "#/components/responses/Forbidden" |
| 176 | + * "429": |
| 177 | + * $ref: "#/components/responses/RateLimited" |
| 178 | + * "500": |
| 179 | + * $ref: "#/components/responses/ServerError" |
| 180 | + * "503": |
| 181 | + * $ref: "#/components/responses/ServiceUnavailable" |
| 182 | + */ |
| 183 | +router.get( |
| 184 | + '/recent', |
| 185 | + feedbackRateLimit, |
| 186 | + requireGuildAdmin, |
| 187 | + validateGuild, |
| 188 | + async (req, res, next) => { |
| 189 | + try { |
| 190 | + const guildId = req.params.id; |
| 191 | + |
| 192 | + let limit = 25; |
| 193 | + if (req.query.limit !== undefined) { |
| 194 | + const parsed = Number.parseInt(req.query.limit, 10); |
| 195 | + if (!Number.isNaN(parsed) && parsed >= 1 && parsed <= 100) { |
| 196 | + limit = parsed; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + const feedback = await getRecentFeedback(guildId, limit); |
| 201 | + res.json({ feedback }); |
| 202 | + } catch (err) { |
| 203 | + next(err); |
| 204 | + } |
| 205 | + }, |
| 206 | +); |
| 207 | + |
| 208 | +export default router; |
0 commit comments