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,7 @@
import { createChildLogger } from "@bete/shared/logger";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import { config } from "../config/index.js";
import { createChildLogger } from "@bete/shared/logger";
const logger = createChildLogger("database");
@@ -1,10 +1,10 @@
import type { NextFunction, Request, Response } from "express";
import {
AppError,
UnauthorizedError,
ValidationError,
} from "@bete/shared/errors";
import { createChildLogger } from "@bete/shared/logger";
import type { NextFunction, Request, Response } from "express";
const logger = createChildLogger("middleware");
@@ -54,7 +54,11 @@ export function asyncHandler(
* Validate that a value is a non-empty string, or throw a descriptive error.
* Use for both route params and query string values.
*/
export function requireParam(value: unknown, kind: string, name: string): string {
export function requireParam(
value: unknown,
kind: string,
name: string,
): string {
if (typeof value !== "string" || value.length === 0) {
throw new Error(`Missing ${kind}: ${name}`);
}
+56 -31
View File
@@ -1,7 +1,7 @@
import { randomUUID } from "node:crypto";
import { createChildLogger } from "@bete/shared/logger";
import Redis from "ioredis";
import { config } from "../config/index.js";
import { createChildLogger } from "@bete/shared/logger";
const logger = createChildLogger("redis.command-channel");
@@ -73,13 +73,21 @@ export async function publishCommand<T = unknown>(
timeoutMs = 5000,
): Promise<CommandReply<T> | null> {
if (!ensureRedisConfig()) {
logger.warn({ commandType }, "Redis not configured, skipping command publish");
logger.warn(
{ commandType },
"Redis not configured, skipping command publish",
);
return null;
}
const id = randomUUID();
const replyChannel = `backend:command:reply:${id}`;
const command: CommandMessage = { id, type: commandType, payload, replyChannel };
const command: CommandMessage = {
id,
type: commandType,
payload,
replyChannel,
};
return new Promise<CommandReply<T> | null>((resolve) => {
const pub = getPublisher();
@@ -88,7 +96,9 @@ export async function publishCommand<T = unknown>(
const timer = setTimeout(() => {
if (settled) return;
settled = true;
sub.unsubscribe(replyChannel).catch(() => {/* ignore */});
sub.unsubscribe(replyChannel).catch(() => {
/* ignore */
});
logger.warn({ id, commandType }, "Command timed out waiting for reply");
resolve(null);
}, timeoutMs);
@@ -99,11 +109,16 @@ export async function publishCommand<T = unknown>(
if (channel !== replyChannel || settled) return;
settled = true;
clearTimeout(timer);
sub.unsubscribe(replyChannel).catch(() => {/* ignore */});
sub.unsubscribe(replyChannel).catch(() => {
/* ignore */
});
try {
const reply: CommandReply<T> = JSON.parse(message);
logger.debug({ id, commandType, success: reply.success }, "Command reply received");
logger.debug(
{ id, commandType, success: reply.success },
"Command reply received",
);
resolve(reply);
} catch (err) {
logger.error({ id, err }, "Failed to parse command reply");
@@ -113,29 +128,34 @@ export async function publishCommand<T = unknown>(
sub.on("message", onMessage);
sub.subscribe(replyChannel).then(() => {
pub
.publish("backend:command", JSON.stringify(command))
.then(() => {
logger.debug({ id, commandType }, "Command published");
})
.catch((err: Error) => {
if (!settled) {
settled = true;
clearTimeout(timer);
sub.unsubscribe(replyChannel).catch(() => {/* ignore */});
logger.error({ err }, "Failed to publish command");
resolve(null);
}
});
}).catch((err: Error) => {
if (!settled) {
settled = true;
clearTimeout(timer);
logger.error({ err }, "Failed to subscribe to reply channel");
resolve(null);
}
});
sub
.subscribe(replyChannel)
.then(() => {
pub
.publish("backend:command", JSON.stringify(command))
.then(() => {
logger.debug({ id, commandType }, "Command published");
})
.catch((err: Error) => {
if (!settled) {
settled = true;
clearTimeout(timer);
sub.unsubscribe(replyChannel).catch(() => {
/* ignore */
});
logger.error({ err }, "Failed to publish command");
resolve(null);
}
});
})
.catch((err: Error) => {
if (!settled) {
settled = true;
clearTimeout(timer);
logger.error({ err }, "Failed to subscribe to reply channel");
resolve(null);
}
});
});
}
@@ -147,7 +167,10 @@ export async function publishCommandNoReply(
payload: Record<string, unknown> = {},
): Promise<void> {
if (!ensureRedisConfig()) {
logger.warn({ commandType }, "Redis not configured, skipping command publish");
logger.warn(
{ commandType },
"Redis not configured, skipping command publish",
);
return;
}
@@ -213,7 +236,9 @@ export function subscribe(
// Status helpers — read keys set by discord-gateway
// ---------------------------------------------------------------------------
export async function readRedisStatus(key: string): Promise<Record<string, unknown> | null> {
export async function readRedisStatus(
key: string,
): Promise<Record<string, unknown> | null> {
if (!ensureRedisConfig()) {
return null;
}