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,9 +1,6 @@
import type { NextFunction, Request, Response } from "express";
import { createChildLogger } from "@bete/shared/logger";
import {
asyncHandler,
requireParam,
} from "../../shared/middlewares/index.js";
import type { NextFunction, Request, Response } from "express";
import { asyncHandler, requireParam } from "../../shared/middlewares/index.js";
import { messageQuerySchema } from "./messages.schema.js";
import { messagesService } from "./messages.service.js";
@@ -28,7 +25,11 @@ export function handleGetMessagesByChannel(
next: NextFunction,
) {
return asyncHandler(async (req: Request, res: Response) => {
const channelId = requireParam(req.params.channelId, "route parameter", "channelId");
const channelId = requireParam(
req.params.channelId,
"route parameter",
"channelId",
);
const query = messageQuerySchema.parse(req.query);
logger.debug({ channelId, query }, "Handling get messages by channel");
const result = await messagesService.getMessagesByChannel(channelId, query);
@@ -55,7 +56,11 @@ export function handleGetAttachmentsByChannel(
next: NextFunction,
) {
return asyncHandler(async (req: Request, res: Response) => {
const channelId = requireParam(req.params.channelId, "route parameter", "channelId");
const channelId = requireParam(
req.params.channelId,
"route parameter",
"channelId",
);
const query = messageQuerySchema.parse(req.query);
logger.debug({ channelId, query }, "Handling get attachments by channel");
const result = await messagesService.getAttachmentsByChannel(
@@ -1,5 +1,5 @@
import { getPool } from "../../shared/database/index.js";
import { createChildLogger } from "@bete/shared/logger";
import { getPool } from "../../shared/database/index.js";
import type {
MessageCreate,
MessageQuery,
@@ -61,7 +61,9 @@ function mapMessageRow(row: Record<string, unknown>) {
}
export class MessagesRepository {
async findMany(query: MessageQuery): Promise<PageResult<ReturnType<typeof mapMessageRow>>> {
async findMany(
query: MessageQuery,
): Promise<PageResult<ReturnType<typeof mapMessageRow>>> {
const pool = getPool();
const limit = query.limit ?? 50;
const clauses: string[] = [];
@@ -101,7 +103,8 @@ export class MessagesRepository {
);
const data = rows.slice(0, limit).map(mapMessageRow);
const nextCursor = rows.length > limit ? String(rows[limit].created_at) : null;
const nextCursor =
rows.length > limit ? String(rows[limit].created_at) : null;
logger.debug({ count: data.length, nextCursor }, "Found messages");
return { data, nextCursor };
@@ -109,10 +112,9 @@ export class MessagesRepository {
async findById(id: string) {
const pool = getPool();
const { rows } = await pool.query(
`SELECT * FROM messages WHERE id = $1`,
[id],
);
const { rows } = await pool.query(`SELECT * FROM messages WHERE id = $1`, [
id,
]);
if (rows.length === 0) return null;
return mapMessageRow(rows[0] as Record<string, unknown>);
@@ -140,7 +142,8 @@ export class MessagesRepository {
);
const data = rows.slice(0, limit).map(mapMessageRow);
const nextCursor = rows.length > limit ? String(rows[limit].created_at) : null;
const nextCursor =
rows.length > limit ? String(rows[limit].created_at) : null;
return { data, nextCursor };
}
@@ -260,6 +263,58 @@ export class MessagesRepository {
return rowCount ?? 0;
}
/**
* Mark a single message for re-analysis by resetting ai_status to 'pending'.
* Skips messages already in 'pending' state to avoid write amplification.
*/
async markForReanalysis(id: string): Promise<void> {
const pool = getPool();
await pool.query(
`UPDATE messages SET ai_status = 'pending'
WHERE id = $1 AND ai_status != 'pending'`,
[id],
);
logger.debug({ id }, "Message marked for re-analysis");
}
/**
* Retrieve messages flagged for review (ai_status IN ('warn', 'flagged')).
* Optionally filtered by channelId, with configurable limit.
*/
async getReviewMessages(
channelId?: string,
limit: number = 20,
): Promise<Record<string, unknown>[]> {
const pool = getPool();
if (channelId) {
const { rows } = await pool.query(
`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`,
[channelId, limit],
);
return rows;
}
const { rows } = await pool.query(
`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`,
[limit],
);
return rows;
}
async delete(id: string): Promise<boolean> {
const pool = getPool();
const { rowCount } = await pool.query(
@@ -308,7 +363,8 @@ export class MessagesRepository {
uploaded_at: (r.uploaded_at as number | null) ?? null,
}));
const nextCursor = data.length > limit ? String(data[limit].created_at) : null;
const nextCursor =
data.length > limit ? String(data[limit].created_at) : null;
const trimmed = data.slice(0, limit);
return { data: trimmed, nextCursor };
@@ -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(),
@@ -46,12 +46,33 @@ export class MessagesService {
return messagesRepository.getAttachmentsByChannel(channelId, query);
}
async markForReanalysis(id: string): Promise<void> {
if (!id) {
throw new ValidationError("message ID is required");
}
logger.debug({ id }, "Marking message for re-analysis");
await messagesRepository.markForReanalysis(id);
}
async getReviewMessages(
channelId?: string,
limit?: number,
): Promise<Record<string, unknown>[]> {
logger.debug({ channelId, limit }, "Getting review messages");
return messagesRepository.getReviewMessages(channelId, limit);
}
async reanalyzeErrorBatch(opts: {
guildId?: string;
channelId?: string;
messageIds?: string[];
}) {
if (!opts.guildId && !opts.channelId && (!opts.messageIds || opts.messageIds.length === 0)) {
if (
!opts.guildId &&
!opts.channelId &&
(!opts.messageIds || opts.messageIds.length === 0)
) {
throw new ValidationError(
"At least one of guildId, channelId, or messageIds[] is required",
);