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:
7 files changed
+460
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { getDatabase } from '../index.js';
|
||||
|
||||
export function createPoll({ guildId, channelId, creatorId, question, choices, endsAt = null }) {
|
||||
const { lastInsertRowid } = getDatabase()
|
||||
.prepare(
|
||||
`INSERT INTO polls (guild_id, channel_id, creator_id, question, choices, ends_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
)
|
||||
.run(guildId, channelId, creatorId, question, JSON.stringify(choices), endsAt);
|
||||
return Number(lastInsertRowid);
|
||||
}
|
||||
|
||||
export function attachPollMessage(id, messageId) {
|
||||
getDatabase().prepare('UPDATE polls SET message_id = ? WHERE id = ?').run(messageId, id);
|
||||
}
|
||||
|
||||
export function getPoll(id) {
|
||||
return getDatabase().prepare('SELECT * FROM polls WHERE id = ?').get(id) ?? null;
|
||||
}
|
||||
|
||||
export function getPollByMessage(messageId) {
|
||||
return getDatabase().prepare('SELECT * FROM polls WHERE message_id = ?').get(messageId) ?? null;
|
||||
}
|
||||
|
||||
export function listPolls({ guildId, status = null, limit = 10 }) {
|
||||
const clauses = ['guild_id = ?'];
|
||||
const params = [guildId];
|
||||
if (status) {
|
||||
clauses.push('status = ?');
|
||||
params.push(status);
|
||||
}
|
||||
params.push(limit);
|
||||
|
||||
return getDatabase()
|
||||
.prepare(`SELECT * FROM polls WHERE ${clauses.join(' AND ')} ORDER BY id DESC LIMIT ?`)
|
||||
.all(...params);
|
||||
}
|
||||
|
||||
/** Polls whose end date is reached and that are still open (M10-T05). */
|
||||
export function listDuePolls(limit = 10) {
|
||||
return getDatabase()
|
||||
.prepare(
|
||||
`SELECT * FROM polls
|
||||
WHERE status = 'open' AND ends_at IS NOT NULL AND ends_at <= datetime('now')
|
||||
ORDER BY ends_at ASC LIMIT ?`,
|
||||
)
|
||||
.all(limit);
|
||||
}
|
||||
|
||||
export function listOpenPolls(limit = 25) {
|
||||
return getDatabase()
|
||||
.prepare("SELECT * FROM polls WHERE status = 'open' ORDER BY id ASC LIMIT ?")
|
||||
.all(limit);
|
||||
}
|
||||
|
||||
export function listVotes(pollId) {
|
||||
return getDatabase().prepare('SELECT user_id, choice_index FROM poll_votes WHERE poll_id = ?').all(pollId);
|
||||
}
|
||||
|
||||
/** Counts votes per choice index: `[{ choice_index, count }]`. */
|
||||
export function countVotes(pollId) {
|
||||
return getDatabase()
|
||||
.prepare('SELECT choice_index, COUNT(*) AS count FROM poll_votes WHERE poll_id = ? GROUP BY choice_index')
|
||||
.all(pollId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates or updates a vote. Returns 'created', 'changed' or 'unchanged' (M10-T04).
|
||||
*/
|
||||
export function castVote(pollId, userId, choiceIndex) {
|
||||
const database = getDatabase();
|
||||
const previous = database
|
||||
.prepare('SELECT choice_index FROM poll_votes WHERE poll_id = ? AND user_id = ?')
|
||||
.get(pollId, userId);
|
||||
|
||||
if (previous && previous.choice_index === choiceIndex) return 'unchanged';
|
||||
|
||||
database
|
||||
.prepare(
|
||||
`INSERT INTO poll_votes (poll_id, user_id, choice_index) VALUES (?, ?, ?)
|
||||
ON CONFLICT (poll_id, user_id) DO UPDATE SET choice_index = excluded.choice_index,
|
||||
voted_at = datetime('now')`,
|
||||
)
|
||||
.run(pollId, userId, choiceIndex);
|
||||
|
||||
return previous ? 'changed' : 'created';
|
||||
}
|
||||
|
||||
export function closePoll(id) {
|
||||
return getDatabase().prepare("UPDATE polls SET status = 'closed' WHERE id = ? AND status = 'open'").run(id)
|
||||
.changes;
|
||||
}
|
||||
|
||||
export function countPolls(guildId) {
|
||||
const row = getDatabase().prepare('SELECT COUNT(*) AS count FROM polls WHERE guild_id = ?').get(guildId);
|
||||
return row.count;
|
||||
}
|
||||
Reference in new issue
Block a user