refactor: large codebase cleanup - consolidate schemas, migrate to Drizzle ORM, extract frontend components, modernize Docker builds
Build & Deploy / build-and-push (discord-gateway) (push) Failing after 2m22s
Build & Deploy / build-and-push (backend) (push) Failing after 3m22s
Build & Deploy / build-and-push (proxy) (push) Successful in 1m36s
Build & Deploy / deploy (push) Skipped
Build & Deploy / build-and-push (discord-gateway) (push) Failing after 2m22s
Build & Deploy / build-and-push (backend) (push) Failing after 3m22s
Build & Deploy / build-and-push (proxy) (push) Successful in 1m36s
Build & Deploy / deploy (push) Skipped
- Consolidate all DB schema definitions into packages/shared as single source of truth - Migrate backend from raw SQL to Drizzle ORM across all modules - Extract frontend inline UI into separate component files - Refactor discord-gateway circuitBreaker into conversationState + moderationState - Convert messageStore to Proxy singleton pattern - Add validateBody/validateQuery middleware + Zod schemas for API endpoints - Modernize Docker builds with multi-stage + pnpm deploy - Migrate CI/CD from deployment to image-based pipeline - Remove 60+ unused/dead files (~15K lines) - Update color scheme from sky-blue to teal-cyan - Move DB connection management to @bete/shared/database Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
63f21513bd
commit
5802d02e29
@@ -0,0 +1,107 @@
|
||||
import { buildCursorCondition, pageResult } from "@bete/shared";
|
||||
import { createChildLogger, type Logger } from "@bete/shared/logger";
|
||||
import { and, desc, eq, inArray, type SQL, sql } from "drizzle-orm";
|
||||
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import type * as schema from "../../shared/database/schema.js";
|
||||
import { messagesTable } from "../../shared/database/schema.js";
|
||||
import type {
|
||||
MessageQuery,
|
||||
MessageRecord,
|
||||
PageResult,
|
||||
} from "../message-capture/types.js";
|
||||
import { channelOrThreadCondition } from "./messagesCrud.js";
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export function buildListMessageConditions(query: MessageQuery): SQL[] {
|
||||
const conditions: SQL[] = [];
|
||||
|
||||
if (query.guildId) {
|
||||
conditions.push(eq(messagesTable.guild_id, query.guildId));
|
||||
}
|
||||
|
||||
if (query.channelId) {
|
||||
conditions.push(channelOrThreadCondition(query.channelId));
|
||||
}
|
||||
|
||||
if (query.threadId) {
|
||||
conditions.push(eq(messagesTable.thread_id, query.threadId));
|
||||
}
|
||||
|
||||
if (query.userId) {
|
||||
conditions.push(eq(messagesTable.user_id, query.userId));
|
||||
}
|
||||
|
||||
if (query.status && query.status.length > 0) {
|
||||
conditions.push(
|
||||
inArray(
|
||||
messagesTable.ai_status,
|
||||
query.status as Array<
|
||||
"pending" | "processing" | "clean" | "warn" | "flagged" | "error"
|
||||
>,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (query.q) {
|
||||
const pattern = `%${query.q.toLowerCase()}%`;
|
||||
conditions.push(sql`lower(${messagesTable.content}) like ${pattern}`);
|
||||
}
|
||||
|
||||
const cursorCondition = buildCursorCondition(
|
||||
messagesTable.created_at,
|
||||
messagesTable.id,
|
||||
query.cursor,
|
||||
);
|
||||
if (cursorCondition) {
|
||||
conditions.push(cursorCondition);
|
||||
}
|
||||
|
||||
return conditions;
|
||||
}
|
||||
|
||||
// ─── MessagesPagination Class ────────────────────────────────────────────────
|
||||
|
||||
export class MessagesPagination {
|
||||
private logger: Logger;
|
||||
|
||||
constructor(
|
||||
private db: NodePgDatabase<typeof schema>,
|
||||
_parentLogger?: Logger,
|
||||
) {
|
||||
this.logger = createChildLogger("messages-pagination");
|
||||
}
|
||||
|
||||
async listMessages(query: MessageQuery): Promise<PageResult<MessageRecord>> {
|
||||
this.logger.debug({ query }, "listMessages entry");
|
||||
try {
|
||||
const conditions = buildListMessageConditions(query);
|
||||
const rows = await this.db
|
||||
.select()
|
||||
.from(messagesTable)
|
||||
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
||||
.orderBy(desc(messagesTable.created_at), desc(messagesTable.id))
|
||||
.limit(query.limit + 1);
|
||||
|
||||
return pageResult<MessageRecord>(rows, query.limit);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
{
|
||||
query,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Failed to list messages",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async listReviewMessages(
|
||||
query: Omit<MessageQuery, "status">,
|
||||
): Promise<PageResult<MessageRecord>> {
|
||||
return this.listMessages({
|
||||
...query,
|
||||
status: ["warn", "flagged", "error"],
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user