refactor: comprehensive codebase cleanup and architecture hardening

- Sprint 1 (Quick Wins): Remove dead analytics modules, fix 4 unresolved
  imports, replace 3 console.warn with logger, remove mock-crc import
- Sprint 2 (Architecture): Create MascotChatRepository, AnalysisRepository,
  3 Zod schemas (mascot-chat, analysis, voice), deduplicate error classes,
  move 3 SQL queries from routes to repository
- Sprint 3 (Complexity): Replace 7 any types with proper interfaces,
  extract 6 helpers from prepareMediaMessage (CC 85 -> ~15)
- Sprint 4 (Config): Remove 22 dead env vars from .env, add 30 missing
  vars to .env.example, standardize naming

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-09 10:16:04 +07:00
co-authored by Claude Opus 4.8
parent d0d9e1669e
commit 4becf0d6f1
89 changed files with 1260 additions and 5499 deletions
@@ -1,7 +1,6 @@
import { createChildLogger } from "@bete/shared/logger";
import type { Request, Response, Router } from "express";
import express from "express";
import { getPool } from "../../shared/database/index.js";
import { createChildLogger } from "@bete/shared/logger";
import { asyncHandler } from "../../shared/middlewares/index.js";
import {
handleGetAttachmentsByChannel,
@@ -85,7 +84,6 @@ export function createMessagesRouter(): Router {
}),
);
// POST /api/messages/:id/reanalyze - Mark single message for re-analysis
router.post(
"/messages/:id/reanalyze",
@@ -104,20 +102,11 @@ export function createMessagesRouter(): Router {
reanalyzeInFlight.add(id);
try {
const pool = getPool();
await pool.query(
// Only revert to pending if the message is not currently being
// processed (pending) already — prevents write amplification when
// the recovery worker already picked it up between UI clicks.
`UPDATE messages SET ai_status = 'pending'
WHERE id = $1 AND ai_status != 'pending'`,
[id],
);
await messagesService.markForReanalysis(id);
} finally {
reanalyzeInFlight.delete(id);
}
logger.debug({ id }, "Message marked for re-analysis");
res.status(200).json({ ok: true });
}),
);
@@ -129,36 +118,7 @@ export function createMessagesRouter(): Router {
const limit = Number(req.query.limit) || 20;
const channelId = (req.query.channelId as string) || undefined;
const pool = getPool();
let sqlQuery: string;
let params: (string | number)[];
if (channelId) {
sqlQuery = `
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
content, type, created_at, ai_status, ai_severity,
ai_confidence, ai_analysis
FROM messages
WHERE ai_status IN ('warn', 'flagged')
AND channel_id = $1
ORDER BY created_at DESC
LIMIT $2
`;
params = [channelId, limit];
} else {
sqlQuery = `
SELECT id, guild_id, channel_id, user_id, username, avatar_url,
content, type, created_at, ai_status, ai_severity,
ai_confidence, ai_analysis
FROM messages
WHERE ai_status IN ('warn', 'flagged')
ORDER BY created_at DESC
LIMIT $1
`;
params = [limit];
}
const { rows } = await pool.query(sqlQuery, params);
const rows = await messagesService.getReviewMessages(channelId, limit);
logger.debug({ limit, channelId }, "Review query executed");
res.json({ results: rows, limit, cursor: null });
}),
@@ -168,7 +128,7 @@ export function createMessagesRouter(): Router {
router.post(
"/messages/:id/moderate",
asyncHandler(async (req: Request, res: Response) => {
const id = req.params.id;
const id = String(req.params.id ?? "");
if (!id) {
res.status(400).json({ error: "MISSING_ID" });
return;
@@ -196,20 +156,12 @@ export function createMessagesRouter(): Router {
}
// Fetch the message to get guild/user context
const pool = getPool();
const { rows } = await pool.query(
`SELECT id, guild_id, channel_id, thread_id, user_id, content
FROM messages WHERE id = $1`,
[id],
);
if (rows.length === 0) {
const msg = await messagesService.getMessageById(id).catch(() => null);
if (!msg) {
res.status(404).json({ error: "MESSAGE_NOT_FOUND" });
return;
}
const msg = rows[0] as Record<string, unknown>;
// Publish command to DG via Redis
const { publishCommand } = await import("../../ws/redis-bridge.js");
await publishCommand({
@@ -217,9 +169,9 @@ export function createMessagesRouter(): Router {
type: "moderation:action",
payload: {
messageId: id,
guildId: String(msg.guild_id ?? ""),
channelId: (msg.thread_id as string) || String(msg.channel_id ?? ""),
userId: String(msg.user_id ?? ""),
guildId: msg.guild_id,
channelId: msg.thread_id || msg.channel_id,
userId: msg.user_id,
actionType,
reason: reason ?? "Manual moderation from dashboard",
requestedAt: Date.now(),