refactor: atomic, DRY, and logging improvements
- Shared Redis channel constants as single source of truth (redis-channels.ts) - commandHandler.ts split into VoiceHandler, MediaHandler, GuildHandler, ModerationHandler with handler-registry.ts dispatch - messageStore.ts (1322 lines) split into domain-specific DB files: messages.db.ts, attachments.db.ts, reviews.db.ts, moderation-actions.db.ts, retention.db.ts - recorder.ts startSpeaking callback extracted into speakingHandler.ts, streamSetup.ts, segmentFinalizer.ts - autoDeleteManager.ts split into autoDeleteEligibility.ts, autoDeleteNotify.ts, autoDeleteLogger.ts - Added createChildLogger() logging across 8 service files - Backend messages.repository.ts migrated from raw SQL to Drizzle ORM - Fixed biome.json to exclude packages/**/dist/* from lint - Fixed config.ts GUILD_ID pre-existing type error Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
7108f6bb47
commit
b68789fffc
@@ -0,0 +1,88 @@
|
||||
import { createChildLogger, type Logger } from "@bete/shared/logger";
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import type * as schema from "../../shared/database/schema.js";
|
||||
import { retentionPoliciesTable } from "../../shared/database/schema.js";
|
||||
import type { RetentionPolicy } from "../message-capture/types.js";
|
||||
|
||||
// ─── RetentionDb Class ──────────────────────────────────────────────────────
|
||||
|
||||
export class RetentionDb {
|
||||
private logger: Logger;
|
||||
|
||||
constructor(
|
||||
private db: NodePgDatabase<typeof schema>,
|
||||
_parentLogger?: Logger,
|
||||
) {
|
||||
this.logger = createChildLogger("retention-db");
|
||||
}
|
||||
|
||||
async getRetentionPolicy(guildId: string): Promise<RetentionPolicy | null> {
|
||||
this.logger.debug({ guildId }, "getRetentionPolicy entry");
|
||||
try {
|
||||
const rows = await this.db
|
||||
.select()
|
||||
.from(retentionPoliciesTable)
|
||||
.where(eq(retentionPoliciesTable.guild_id, guildId));
|
||||
|
||||
return (rows[0] as RetentionPolicy) || null;
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
{
|
||||
guildId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Failed to get retention policy",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async upsertRetentionPolicy(
|
||||
policy: Omit<RetentionPolicy, "created_at" | "updated_at">,
|
||||
): Promise<RetentionPolicy> {
|
||||
this.logger.debug(
|
||||
{ guildId: policy.guild_id },
|
||||
"upsertRetentionPolicy entry",
|
||||
);
|
||||
try {
|
||||
const now = Date.now();
|
||||
const existing = await this.getRetentionPolicy(policy.guild_id);
|
||||
|
||||
if (existing) {
|
||||
const rows = (await this.db
|
||||
.update(retentionPoliciesTable)
|
||||
.set({
|
||||
...policy,
|
||||
updated_at: now,
|
||||
})
|
||||
.where(eq(retentionPoliciesTable.id, existing.id))
|
||||
.returning()) as RetentionPolicy[];
|
||||
|
||||
return rows[0] as RetentionPolicy;
|
||||
}
|
||||
|
||||
const id = `policy-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
||||
const rows = (await this.db
|
||||
.insert(retentionPoliciesTable)
|
||||
.values({
|
||||
...policy,
|
||||
id,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})
|
||||
.returning()) as RetentionPolicy[];
|
||||
|
||||
return rows[0] as RetentionPolicy;
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
{
|
||||
guildId: policy.guild_id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Failed to upsert retention policy",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user