2026-07-27 21:54:31 +07:00
|
|
|
import { pgMessagesTable } from "@bete/shared";
|
2026-06-09 10:16:04 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-07-27 21:54:31 +07:00
|
|
|
import { and, desc, eq, ilike, type SQL } from "drizzle-orm";
|
|
|
|
|
import { getDatabase } from "../../shared/database/index.js";
|
2026-06-09 19:46:08 +07:00
|
|
|
import {
|
|
|
|
|
type MappedMessage,
|
|
|
|
|
mapMessageRow,
|
|
|
|
|
} from "../../shared/utils/messageMapper.js";
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("analysis.repository");
|
|
|
|
|
|
|
|
|
|
export interface AnalysisSearchQuery {
|
|
|
|
|
q?: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
guildId?: string;
|
|
|
|
|
limit?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
// AnalysisSearchResult is identical to MappedMessage — reuse the shared mapper
|
|
|
|
|
export type AnalysisSearchResult = MappedMessage;
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
export class AnalysisRepository {
|
|
|
|
|
async search(query: AnalysisSearchQuery): Promise<AnalysisSearchResult[]> {
|
2026-07-27 21:54:31 +07:00
|
|
|
const db = getDatabase();
|
2026-06-09 10:16:04 +07:00
|
|
|
const { q = "", channelId, guildId, limit = 20 } = query;
|
|
|
|
|
|
|
|
|
|
logger.debug({ q, channelId, guildId, limit }, "Searching analysis");
|
|
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const conditions: SQL[] = [ilike(pgMessagesTable.content, `%${q}%`)];
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
if (guildId) {
|
2026-07-27 21:54:31 +07:00
|
|
|
conditions.push(eq(pgMessagesTable.guild_id, guildId));
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (channelId) {
|
2026-07-27 21:54:31 +07:00
|
|
|
conditions.push(eq(pgMessagesTable.channel_id, channelId));
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(pgMessagesTable)
|
|
|
|
|
.where(and(...conditions))
|
|
|
|
|
.orderBy(desc(pgMessagesTable.created_at))
|
|
|
|
|
.limit(limit);
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
return rows.map((r) => mapMessageRow(r as Record<string, unknown>));
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const analysisRepository = new AnalysisRepository();
|