feat(database): add repositories for tickets, moderation, polls and notifications

Keep every SQL statement out of the command layer so each feature can be built and tested without a Discord dependency.
This commit is contained in:
BlackAngelTV committed 2026-09-15 14:50:34 +02:00
1 parent abf6701bb0
commit 03a217c775
7 files changed
+460

No files matched your search

@@ -0,0 +1,50 @@
import { getDatabase } from '../index.js';
export function recordAction({
guildId,
action,
moderatorId,
targetId = null,
reason = null,
durationMs = null,
}) {
getDatabase()
.prepare(
`INSERT INTO moderation_actions (guild_id, action, target_id, moderator_id, reason, duration_ms)
VALUES (?, ?, ?, ?, ?, ?)`,
)
.run(guildId, action, targetId, moderatorId, reason, durationMs);
}
export function listActions({ guildId, targetId = null, action = null, limit = 20 }) {
const clauses = ['guild_id = ?'];
const params = [guildId];
if (targetId) {
clauses.push('target_id = ?');
params.push(targetId);
}
if (action) {
clauses.push('action = ?');
params.push(action);
}
params.push(limit);
return getDatabase()
.prepare(`SELECT * FROM moderation_actions WHERE ${clauses.join(' AND ')} ORDER BY id DESC LIMIT ?`)
.all(...params);
}
export function countActions({ guildId, targetId, action }) {
const row = getDatabase()
.prepare(
'SELECT COUNT(*) AS count FROM moderation_actions WHERE guild_id = ? AND target_id = ? AND action = ?',
)
.get(guildId, targetId, action);
return row.count;
}
export function countByAction(guildId) {
return getDatabase()
.prepare('SELECT action, COUNT(*) AS count FROM moderation_actions WHERE guild_id = ? GROUP BY action')
.all(guildId);
}