RezinaLMS-BackEnd/db/query/notification.sql
Yared Yemane 10babe06f6 feat: add in-app notification delete APIs and fix FAQ RBAC access
- Add DELETE /notifications/:id and POST /notifications/delete-in-app for owned in-app rows

- Seed FAQ permissions for ADMIN/INSTRUCTOR, migration 000084, and improve RBAC sync

- Document FAQ admin panel integration

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 02:15:00 -07:00

125 lines
3.1 KiB
SQL

-- name: CreateNotification :one
INSERT INTO notifications (
user_id,
receiver_type,
type,
level,
channel,
title,
message,
payload
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
RETURNING *;
-- name: GetNotification :one
SELECT *
FROM notifications
WHERE id = $1
LIMIT 1;
-- name: GetAllNotifications :many
SELECT *
FROM notifications
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;
-- name: GetTotalNotificationCount :one
SELECT COUNT(*)
FROM notifications;
-- name: GetUserNotifications :many
SELECT *
FROM notifications
WHERE user_id = $1
ORDER BY created_at DESC
LIMIT $2 OFFSET $3;
-- name: GetUserNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1;
-- name: CountUnreadNotifications :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1
AND is_read = FALSE;
-- name: MarkNotificationAsRead :one
UPDATE notifications
SET is_read = TRUE,
read_at = NOW()
WHERE id = $1
RETURNING *;
-- name: MarkAllUserNotificationsAsRead :exec
UPDATE notifications
SET is_read = TRUE,
read_at = NOW()
WHERE user_id = $1
AND is_read = FALSE;
-- name: MarkNotificationAsUnread :one
UPDATE notifications
SET is_read = FALSE,
read_at = NULL
WHERE id = $1
RETURNING *;
-- name: MarkAllUserNotificationsAsUnread :exec
UPDATE notifications
SET is_read = FALSE,
read_at = NULL
WHERE user_id = $1
AND is_read = TRUE;
-- name: DeleteUserNotifications :exec
DELETE FROM notifications
WHERE user_id = $1;
-- name: DeleteUserInAppNotification :execrows
DELETE FROM notifications
WHERE id = $1
AND user_id = $2
AND channel = 'in_app';
-- name: DeleteUserInAppNotificationsByIDs :execrows
DELETE FROM notifications
WHERE user_id = $1
AND channel = 'in_app'
AND id = ANY($2::BIGINT[]);
-- name: GetFilteredNotifications :many
SELECT *
FROM notifications
WHERE
(sqlc.narg('filter_channel')::text IS NULL OR channel = sqlc.narg('filter_channel'))
AND (sqlc.narg('filter_type')::text IS NULL OR type = sqlc.narg('filter_type'))
AND (sqlc.narg('filter_user_id')::bigint IS NULL OR user_id = sqlc.narg('filter_user_id'))
AND (sqlc.narg('filter_is_read')::boolean IS NULL OR is_read = sqlc.narg('filter_is_read'))
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR created_at >= sqlc.narg('filter_after'))
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR created_at <= sqlc.narg('filter_before'))
ORDER BY created_at DESC
LIMIT @page_limit OFFSET @page_offset;
-- name: GetFilteredNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE
(sqlc.narg('filter_channel')::text IS NULL OR channel = sqlc.narg('filter_channel'))
AND (sqlc.narg('filter_type')::text IS NULL OR type = sqlc.narg('filter_type'))
AND (sqlc.narg('filter_user_id')::bigint IS NULL OR user_id = sqlc.narg('filter_user_id'))
AND (sqlc.narg('filter_is_read')::boolean IS NULL OR is_read = sqlc.narg('filter_is_read'))
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR created_at >= sqlc.narg('filter_after'))
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR created_at <= sqlc.narg('filter_before'));