47 lines
2.3 KiB
JavaScript
47 lines
2.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createUserSecurityNotification = createUserSecurityNotification;
|
|
exports.listForUser = listForUser;
|
|
exports.markRead = markRead;
|
|
exports.markAllRead = markAllRead;
|
|
const connection_1 = require("../db/connection");
|
|
async function createUserSecurityNotification(data) {
|
|
await (0, connection_1.query)(`INSERT INTO user_security_notifications (user_id, event_type, title, body, metadata)
|
|
VALUES ($1, $2, $3, $4, $5)`, [data.userId, data.eventType, data.title, data.body ?? null, data.metadata ? JSON.stringify(data.metadata) : null]);
|
|
}
|
|
async function listForUser(userId, opts) {
|
|
const limit = Math.min(Math.max(opts?.limit ?? 30, 1), 100);
|
|
const offset = Math.max(opts?.offset ?? 0, 0);
|
|
const unreadOnly = opts?.unreadOnly ?? false;
|
|
const conditions = ['user_id = $1'];
|
|
const params = [userId];
|
|
if (unreadOnly)
|
|
conditions.push('read_at IS NULL');
|
|
const where = `WHERE ${conditions.join(' AND ')}`;
|
|
const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count,
|
|
COUNT(*) FILTER (WHERE read_at IS NULL)::text AS unread
|
|
FROM user_security_notifications
|
|
WHERE user_id = $1`, [userId]);
|
|
const total = parseInt(countRows[0]?.count ?? '0', 10);
|
|
const unreadCount = parseInt(countRows[0]?.unread ?? '0', 10);
|
|
const items = await (0, connection_1.query)(`SELECT id, user_id, event_type, title, body, read_at, metadata, created_at
|
|
FROM user_security_notifications ${where}
|
|
ORDER BY created_at DESC
|
|
LIMIT $${params.length + 1} OFFSET $${params.length + 2}`, [...params, limit, offset]);
|
|
return { items, total, unreadCount };
|
|
}
|
|
async function markRead(userId, notificationId) {
|
|
const rows = await (0, connection_1.query)(`UPDATE user_security_notifications
|
|
SET read_at = NOW()
|
|
WHERE id = $1 AND user_id = $2 AND read_at IS NULL
|
|
RETURNING id`, [notificationId, userId]);
|
|
return rows.length > 0;
|
|
}
|
|
async function markAllRead(userId) {
|
|
const rows = await (0, connection_1.query)(`UPDATE user_security_notifications
|
|
SET read_at = NOW()
|
|
WHERE user_id = $1 AND read_at IS NULL
|
|
RETURNING id`, [userId]);
|
|
return rows.length;
|
|
}
|
|
//# sourceMappingURL=userSecurityNotificationRepository.js.map
|