72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
import { getDatabase } from '../index.js';
|
|
|
|
export function createTicket({ guildId, channelId, categoryId, authorId }) {
|
|
const { lastInsertRowid } = getDatabase()
|
|
.prepare('INSERT INTO tickets (guild_id, channel_id, category_id, author_id) VALUES (?, ?, ?, ?)')
|
|
.run(guildId, channelId, categoryId, authorId);
|
|
return Number(lastInsertRowid);
|
|
}
|
|
|
|
export function getTicket(id) {
|
|
return getDatabase().prepare('SELECT * FROM tickets WHERE id = ?').get(id) ?? null;
|
|
}
|
|
|
|
export function getTicketByChannel(channelId) {
|
|
return getDatabase().prepare('SELECT * FROM tickets WHERE channel_id = ?').get(channelId) ?? null;
|
|
}
|
|
|
|
/** Returns the open ticket of an author for a category, if any (M6-T08). */
|
|
export function getOpenTicket({ guildId, authorId, categoryId }) {
|
|
return (
|
|
getDatabase()
|
|
.prepare(
|
|
`SELECT * FROM tickets
|
|
WHERE guild_id = ? AND author_id = ? AND category_id = ? AND status = 'open'`,
|
|
)
|
|
.get(guildId, authorId, categoryId) ?? null
|
|
);
|
|
}
|
|
|
|
export function listTickets({ guildId, status = null, authorId = null, limit = 20 }) {
|
|
const clauses = ['guild_id = ?'];
|
|
const params = [guildId];
|
|
if (status) {
|
|
clauses.push('status = ?');
|
|
params.push(status);
|
|
}
|
|
if (authorId) {
|
|
clauses.push('author_id = ?');
|
|
params.push(authorId);
|
|
}
|
|
params.push(limit);
|
|
|
|
return getDatabase()
|
|
.prepare(`SELECT * FROM tickets WHERE ${clauses.join(' AND ')} ORDER BY id DESC LIMIT ?`)
|
|
.all(...params);
|
|
}
|
|
|
|
export function claimTicket(id, userId) {
|
|
return getDatabase()
|
|
.prepare(
|
|
"UPDATE tickets SET claimed_by = ?, claimed_at = datetime('now') WHERE id = ? AND status = 'open'",
|
|
)
|
|
.run(userId, id).changes;
|
|
}
|
|
|
|
export function closeTicket(id, { closedBy, reason = null }) {
|
|
return getDatabase()
|
|
.prepare(
|
|
`UPDATE tickets
|
|
SET status = 'closed', closed_at = datetime('now'), closed_by = ?, close_reason = ?
|
|
WHERE id = ? AND status = 'open'`,
|
|
)
|
|
.run(closedBy, reason, id).changes;
|
|
}
|
|
|
|
export function countOpenTickets(guildId) {
|
|
const row = getDatabase()
|
|
.prepare("SELECT COUNT(*) AS count FROM tickets WHERE guild_id = ? AND status = 'open'")
|
|
.get(guildId);
|
|
return row.count;
|
|
}
|