Yimaru-BackEnd/db/query/notification.sql
Samuel Tariku e49ff366d5 feat: Implement wallet notification system and refactor related services
- Added new notification handling in the wallet service to notify admins when wallet balances are low or insufficient.
- Created a new file for wallet notifications and moved relevant functions from the wallet service to this new file.
- Updated the wallet service to publish wallet events including wallet type.
- Refactored the client code to improve readability and maintainability.
- Enhanced the bet handler to support pagination and status filtering for bets.
- Updated routes and handlers for user search functionality to improve clarity and organization.
- Modified cron job scheduling to comment out unused jobs for clarity.
- Updated the WebSocket broadcast to include wallet type in notifications.
- Adjusted the makefile to include Kafka in the docker-compose setup for local development.
2025-09-25 21:26:24 +03:00

91 lines
1.8 KiB
SQL

-- name: CreateNotification :one
INSERT INTO notifications (
id,
recipient_id,
type,
level,
error_severity,
reciever,
is_read,
delivery_status,
delivery_channel,
payload,
priority,
timestamp,
metadata
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13
)
RETURNING *;
-- name: GetNotification :one
SELECT *
FROM notifications
WHERE id = $1
LIMIT 1;
-- name: GetAllNotifications :many
SELECT *
FROM notifications
ORDER BY timestamp DESC
LIMIT $1 OFFSET $2;
-- name: GetTotalNotificationCount :one
SELECT COUNT(*)
FROM notifications;
-- name: GetUserNotifications :many
SELECT *
FROM notifications
WHERE recipient_id = $1
ORDER BY timestamp DESC
LIMIT $2 OFFSET $3;
-- name: GetUserNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE recipient_id = $1;
-- name: CountUnreadNotifications :one
SELECT count(id)
FROM notifications
WHERE recipient_id = $1
AND is_read = false;
-- name: UpdateNotificationStatus :one
UPDATE notifications
SET delivery_status = $2,
is_read = $3,
metadata = $4
WHERE id = $1
RETURNING *;
-- name: ListFailedNotifications :many
SELECT *
FROM notifications
WHERE delivery_status = 'failed'
AND timestamp < NOW() - INTERVAL '1 hour'
ORDER BY timestamp ASC
LIMIT $1;
-- name: ListRecipientIDsByReceiver :many
SELECT recipient_id
FROM notifications
WHERE reciever = $1;
-- name: GetNotificationCounts :many
SELECT COUNT(*) as total,
COUNT(
CASE
WHEN is_read = true THEN 1
END
) as read,
COUNT(
CASE
WHEN is_read = false THEN 1
END
) as unread
FROM notifications;