feat(backend): implement all missing endpoints, real analytics queries, and WebSocket server
- Replace stub analytics.repository.ts with real PostgreSQL queries using pg.Pool - Add /api/guilds, /api/config, /api/auth/login, /api/ui-state (GET/POST) - Add /api/review, /api/recordings, /api/analysis/search - Add /api/messages/:id/reanalyze endpoint - Add /api/analytics/heatmap and /api/analytics/topics - Implement media routes (stub responses, backend has no Discord voice client) - Add WebSocket server at /ws with heartbeat and broadcast functions - Fix analytics route paths to match frontend contract (dual paths for backward compat) - Export getPool() from database module for raw SQL queries - Register all new routers in app.ts Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
5a094c926a
commit
9b41eb9c12
@@ -0,0 +1,27 @@
|
||||
import type { Request, Response, Router } from "express";
|
||||
import express from "express";
|
||||
import { createChildLogger } from "../../shared/logger/index.js";
|
||||
import { asyncHandler } from "../../shared/middlewares/index.js";
|
||||
import { analysisService } from "./analysis.service.js";
|
||||
|
||||
const logger = createChildLogger("analysis.routes");
|
||||
|
||||
export function createAnalysisRouter(): Router {
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/analysis/search
|
||||
router.get(
|
||||
"/analysis/search",
|
||||
asyncHandler(async (req: Request, res: Response) => {
|
||||
const q = (req.query.q as string) || "";
|
||||
const channelId = (req.query.channelId as string) || undefined;
|
||||
const limit = Number(req.query.limit) || 20;
|
||||
|
||||
logger.debug({ q, channelId, limit }, "Analysis search requested");
|
||||
const result = await analysisService.search({ q, channelId, limit });
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { getDatabase } from "../../shared/database/index.js";
|
||||
import { config } from "../../shared/config/index.js";
|
||||
import { createChildLogger } from "../../shared/logger/index.js";
|
||||
|
||||
const logger = createChildLogger("analysis.service");
|
||||
|
||||
export interface AnalysisSearchQuery {
|
||||
q?: string;
|
||||
channelId?: string;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export class AnalysisService {
|
||||
async search(query: AnalysisSearchQuery) {
|
||||
const db = getDatabase();
|
||||
const { q = "", channelId, limit = 20 } = query;
|
||||
const guildId = config.MONITOR_GUILD_ID;
|
||||
|
||||
logger.debug({ q, channelId, limit, guildId }, "Searching analysis");
|
||||
|
||||
const searchPattern = `%${q}%`;
|
||||
const limitVal = limit;
|
||||
|
||||
let sqlQuery;
|
||||
if (channelId && guildId) {
|
||||
sqlQuery = sql`
|
||||
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
|
||||
content, type, created_at, ai_status, ai_severity, ai_confidence
|
||||
FROM messages
|
||||
WHERE guild_id = ${guildId}
|
||||
AND channel_id = ${channelId}
|
||||
AND content ILIKE ${searchPattern}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ${limitVal}
|
||||
`;
|
||||
} else if (channelId) {
|
||||
sqlQuery = sql`
|
||||
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
|
||||
content, type, created_at, ai_status, ai_severity, ai_confidence
|
||||
FROM messages
|
||||
WHERE channel_id = ${channelId}
|
||||
AND content ILIKE ${searchPattern}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ${limitVal}
|
||||
`;
|
||||
} else if (guildId) {
|
||||
sqlQuery = sql`
|
||||
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
|
||||
content, type, created_at, ai_status, ai_severity, ai_confidence
|
||||
FROM messages
|
||||
WHERE guild_id = ${guildId}
|
||||
AND content ILIKE ${searchPattern}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ${limitVal}
|
||||
`;
|
||||
} else {
|
||||
sqlQuery = sql`
|
||||
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
|
||||
content, type, created_at, ai_status, ai_severity, ai_confidence
|
||||
FROM messages
|
||||
WHERE content ILIKE ${searchPattern}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ${limitVal}
|
||||
`;
|
||||
}
|
||||
|
||||
const { rows } = await db.execute(sqlQuery);
|
||||
return { results: rows };
|
||||
}
|
||||
}
|
||||
|
||||
export const analysisService = new AnalysisService();
|
||||
Reference in New Issue
Block a user