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:
MythEclipse
2026-06-02 00:11:29 +07:00
co-authored by Claude Opus 4.8
parent 5a094c926a
commit 9b41eb9c12
24 changed files with 1205 additions and 87 deletions
@@ -0,0 +1,34 @@
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 { uiStateService } from "./ui-state.service.js";
const logger = createChildLogger("ui-state.routes");
export function createUiStateRouter(): Router {
const router = express.Router();
// GET /api/ui-state
router.get(
"/ui-state",
asyncHandler(async (_req: Request, res: Response) => {
logger.debug("Fetching UI state");
const state = await uiStateService.getState();
res.json(state);
}),
);
// POST /api/ui-state
router.post(
"/ui-state",
asyncHandler(async (req: Request, res: Response) => {
const updates = req.body as Record<string, unknown>;
logger.debug({ keys: Object.keys(updates) }, "Updating UI state");
const result = await uiStateService.updateState(updates);
res.json(result);
}),
);
return router;
}
@@ -0,0 +1,51 @@
import { sql } from "drizzle-orm";
import { getDatabase } from "../../shared/database/index.js";
import { createChildLogger } from "../../shared/logger/index.js";
const logger = createChildLogger("ui-state.service");
export class UiStateService {
async getState() {
const db = getDatabase();
logger.debug("Fetching UI state");
const { rows } = await db.execute(
sql`SELECT key, value, updated_at FROM ui_state ORDER BY key`,
);
const result: Record<string, unknown> = {};
for (const row of rows) {
try {
result[row.key as string] = JSON.parse(row.value as string);
} catch {
result[row.key as string] = row.value;
}
}
return result;
}
async updateState(updates: Record<string, unknown>) {
const db = getDatabase();
const now = Date.now();
logger.debug({ keys: Object.keys(updates) }, "Updating UI state");
for (const [key, value] of Object.entries(updates)) {
const serialized =
typeof value === "string" ? value : JSON.stringify(value);
await db.execute(sql`
INSERT INTO ui_state (key, value, updated_at)
VALUES (${key}, ${serialized}, ${now})
ON CONFLICT (key) DO UPDATE SET
value = EXCLUDED.value,
updated_at = EXCLUDED.updated_at
`);
}
return await this.getState();
}
}
export const uiStateService = new UiStateService();