games_app/backend/dist/repositories/adminNotificationRepository.js
2026-06-29 15:01:23 +02:00

44 lines
2.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNotification = createNotification;
exports.listNotifications = listNotifications;
exports.markRead = markRead;
exports.markAllRead = markAllRead;
const connection_1 = require("../db/connection");
const crypto_1 = require("../utils/crypto");
async function createNotification(input) {
const rows = await (0, connection_1.query)(`INSERT INTO admin_notifications (title, body_encrypted, severity, alert_id)
VALUES ($1, $2, $3, $4)
RETURNING *`, [
input.title,
input.body ? (0, crypto_1.encryptSensitive)(input.body) : null,
input.severity ?? 'info',
input.alertId ?? null,
]);
return rows[0];
}
async function listNotifications(opts) {
const limit = Math.min(opts.limit ?? 50, 100);
const offset = Math.max(opts.offset ?? 0, 0);
const countRows = await (0, connection_1.query)(opts.unreadOnly
? 'SELECT COUNT(*)::text AS count FROM admin_notifications WHERE read_at IS NULL'
: 'SELECT COUNT(*)::text AS count FROM admin_notifications');
const unreadRows = await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM admin_notifications WHERE read_at IS NULL');
const items = opts.unreadOnly
? await (0, connection_1.query)('SELECT * FROM admin_notifications WHERE read_at IS NULL ORDER BY created_at DESC LIMIT $1 OFFSET $2', [limit, offset])
: await (0, connection_1.query)('SELECT * FROM admin_notifications ORDER BY created_at DESC LIMIT $1 OFFSET $2', [limit, offset]);
return {
items,
total: Number(countRows[0]?.count ?? 0),
unreadCount: Number(unreadRows[0]?.count ?? 0),
};
}
async function markRead(id) {
await (0, connection_1.query)('UPDATE admin_notifications SET read_at = NOW() WHERE id = $1 AND read_at IS NULL', [
id,
]);
}
async function markAllRead() {
await (0, connection_1.query)('UPDATE admin_notifications SET read_at = NOW() WHERE read_at IS NULL');
}
//# sourceMappingURL=adminNotificationRepository.js.map