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,78 +1,26 @@
import { sql } from "drizzle-orm";
import { config } from "../../shared/config/index.js";
import { getDatabase } from "../../shared/database/index.js";
import { createChildLogger } from "@bete/shared/logger";
import { config } from "../../shared/config/index.js";
import type { AnalysisSearchQuery } from "./analysis.repository.js";
import { analysisRepository } from "./analysis.repository.js";
const logger = createChildLogger("analysis.service");
export interface AnalysisSearchQuery {
q?: string;
channelId?: string;
limit?: number;
}
/** Full message columns for search results — matches MessageRecord from client.ts */
const FULL_COLUMNS = sql.raw(`
id, guild_id, channel_id, thread_id,
user_id, username, avatar_url,
content, edited_content, created_at, edited_at, deleted_at,
type, metadata,
ai_status, ai_moderation_flags, ai_moderation_score,
ai_analysis, ai_categories, ai_severity, ai_confidence,
ai_recommended_action, ai_analyzed_at, ai_error
`);
export type { AnalysisSearchQuery };
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;
const rows = await analysisRepository.search({
q,
channelId,
guildId,
limit,
});
let sqlQuery;
if (channelId && guildId) {
sqlQuery = sql`
SELECT ${FULL_COLUMNS}
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 ${FULL_COLUMNS}
FROM messages
WHERE channel_id = ${channelId}
AND content ILIKE ${searchPattern}
ORDER BY created_at DESC
LIMIT ${limitVal}
`;
} else if (guildId) {
sqlQuery = sql`
SELECT ${FULL_COLUMNS}
FROM messages
WHERE guild_id = ${guildId}
AND content ILIKE ${searchPattern}
ORDER BY created_at DESC
LIMIT ${limitVal}
`;
} else {
sqlQuery = sql`
SELECT ${FULL_COLUMNS}
FROM messages
WHERE content ILIKE ${searchPattern}
ORDER BY created_at DESC
LIMIT ${limitVal}
`;
}
const { rows } = await db.execute(sqlQuery);
return { results: rows };
}
}