import { EventEmitter } from 'events'; import { Adminizer } from '../Adminizer'; import { INotification, INotificationEvent } from '../../interfaces/types'; import { NotificationAPModel } from "../../models/NotificationAP"; import { UserAP } from "../../models/UserAP"; /** * ░█████╗░██████╗░░██████╗████████╗██████╗░░█████╗░░█████╗░████████╗ * ██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝ * ███████║██████╦╝╚█████╗░░░░██║░░░██████╔╝███████║██║░░╚═╝░░░██║░░░ * ██╔══██║██╔══██╗░╚═══██╗░░░██║░░░██╔══██╗██╔══██║██║░░██╗░░░██║░░░ * ██║░░██║██████╦╝██████╔╝░░░██║░░░██║░░██║██║░░██║╚█████╔╝░░░██║░░░ * ╚═╝░░╚═╝╚═════╝░╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░ * * ███╗░░██╗░█████╗░████████╗██╗███████╗██╗░█████╗░░█████╗░████████╗██╗░█████╗░███╗░░██╗ * ████╗░██║██╔══██╗╚══██╔══╝██║██╔════╝██║██╔══██╗██╔══██╗╚══██╔══╝██║██╔══██╗████╗░██║ * ██╔██╗██║██║░░██║░░░██║░░░██║█████╗░░██║██║░░╚═╝███████║░░░██║░░░██║██║░░██║██╔██╗██║ * ██║╚████║██║░░██║░░░██║░░░██║██╔══╝░░██║██║░░██╗██╔══██║░░░██║░░░██║██║░░██║██║╚████║ * ██║░╚███║╚█████╔╝░░░██║░░░██║██║░░░░░██║╚█████╔╝██║░░██║░░░██║░░░██║╚█████╔╝██║░╚███║ * ╚═╝░░╚══╝░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░░░░╚═╝░╚════╝░╚═╝░░╚═╝░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝ * * Class: AbstractNotificationService * * Description: * The `AbstractNotificationService` is a base class that provides the fundamental structure and functionality * for implementing notification services in the application. It manages client connections, dispatches notifications, * tracks user notification status, and handles database interactions for storing and retrieving notification data. * This abstract class is intended to be extended by concrete notification service implementations. * * Key Features: * - Manages a registry of clients associated with users using a nested Map structure. * - Provides methods to add, remove, and retrieve clients. * - Dispatches notifications to connected clients via an abstract method to be implemented by subclasses. * - Sends heartbeat (ping) messages to keep client connections alive. * - Creates and retrieves user notification records in the database. * - Fetches paginated and filtered notification lists for a specific user. * - Supports searching notifications by message content. * - Marks notifications as read or all notifications as read for a user. * - Emits events using Node.js EventEmitter for extensibility and integration. * * Usage Example: * Subclasses should override the `dispatchNotification` method to implement custom notification logic. * * @abstract * @extends EventEmitter * @property {Map>} clients - A map of user IDs to their client maps. * @property {Adminizer} adminizer - Reference to the Adminizer instance for accessing helpers and models. * @property {string} notificationClass - Abstract property representing the class name of the notification. * @property {string} icon - Abstract property for the notification icon. * @property {string} iconColor - Abstract property for the notification icon color. */ export declare abstract class AbstractNotificationService extends EventEmitter { protected clients: Map void>>; protected adminizer: Adminizer; abstract readonly notificationClass: string; abstract readonly displayName: string; abstract readonly icon: string; abstract readonly iconColor: string; constructor(adminizer: Adminizer); private _bindAccessRight; /** * Registers a client for a user and associates it with a send function. * If the user does not yet have a Map of clients, one is created. * The client is then added to the user's Map with the provided send function. * @param {string} clientId - The unique identifier of the client to be registered. * @param {(event: INotificationEvent) => void} sendFn - The function used to send notifications to the client. * @param {UserAP} user - The user associated with the client. */ addClient(clientId: string, sendFn: (event: INotificationEvent) => void, user: UserAP): void; /** * Removes a client from the internal tracking structure. * If the user associated with the client has no more clients after removal, the user's Map is also removed. * @param {string} clientId - The unique identifier of the client to be removed. */ removeClient(clientId: string): void; /** * Retrieves the Map of clients associated with a specific user. * If the user has no clients, an empty Map is returned. * @param {number} userId - The unique identifier of the user whose clients should be retrieved. * @returns {Map void>} A Map where the keys are client IDs and the values are send functions. */ getUserClients(userId: number): Map void>; /** * Returns the total number of users who have at least one connected client. * @returns {number} The count of users with active clients. */ getClientCount(): number; /** * Aggregates all clients from all users into a single Map for backward compatibility. * Each client is uniquely identified by its ID and mapped to its corresponding send function. * @returns {Map void>} A combined Map of all client IDs and their send functions. */ getAllClients(): Map void>; /** * Abstract method that must be implemented by subclasses to dispatch a notification. * @param {Omit} notification - The notification data to be dispatched (excluding id, createdAt, notificationClass, and icon). * @returns {Promise} A promise that resolves to a boolean indicating whether the notification was successfully dispatched. */ abstract dispatchNotification(notification: Omit): Promise; /** * Sends a notification event to all connected clients. * Iterates through each user's client Map and invokes the send function for each client. * If sending fails, logs an error and removes the client. * @param {INotificationEvent} event - The notification event to broadcast. */ protected broadcast(event: INotificationEvent): void; /** * Sends a heartbeat (ping) message to a specific client. * Searches for the client across all user client Maps and sends a heartbeat event if found. * @param {string} clientId - The unique identifier of the client to receive the heartbeat. */ sendHeartbeat(clientId: string): void; /** * Creates a new user notification record in the 'usernotificationap' model. * This method is used to associate a notification with a specific user and mark it as unread. * @param {string} notificationId - The unique identifier of the notification. * @param {number} [userId] - The ID of the user to associate the notification with. Optional. * @returns {Promise} A promise that resolves when the record is created or an error is logged. */ protected createUserNotification(notificationId: string, userId?: number): Promise; /** * Retrieves a user notification record from the 'usernotificationap' model. * The record is fetched based on the provided notification ID and user ID. * @param {string} notificationId - The unique identifier of the notification. * @param {number} userId - The ID of the user associated with the notification. * @returns {Promise} A promise that resolves with the found record or null if not found or an error occurs. */ protected getUserNotification(notificationId: string, userId: number): Promise; /** * Retrieves a paginated list of notifications for a specific user, optionally filtered by read status. * @param {number} userId - The ID of the user whose notifications should be fetched. * @param {number} [limit=20] - The maximum number of notifications to return. * @param {number} [skip=0] - The number of notifications to skip (used for pagination). * @param {boolean} [unreadOnly=false] - Whether to return only unread notifications. * @returns {Promise} A promise that resolves with an array of notification objects. */ getNotifications(userId: number, limit?: number, skip?: number, unreadOnly?: boolean): Promise; /** * Prepares notification data by enriching it with user-specific read status and icon information. * @param {NotificationAPModel[]} notificationsDB - An array of raw notification records from the database. * @param {number} userId - The ID of the user for whom the read status is determined. * @returns {Promise} A promise that resolves with an array of enriched notification objects. */ protected prepareNotification(notificationsDB: NotificationAPModel[], userId: number): Promise; /** * Searches for notifications for a specific user based on a message keyword. * @param {string} s - The search string used to filter notifications by message content. * @param {number} userId - The ID of the user whose notifications should be searched. * @returns {Promise} A promise that resolves with an array of matching notification objects. */ search(s: string, userId: number): Promise; /** * Marks a specific notification as read for a given user. * @param {number} userId - The ID of the user for whom the notification should be marked as read. * @param {string} id - The ID of the notification to mark as read. * @returns {Promise} A promise that resolves when the operation is complete. */ markAsRead(userId: number, id: string): Promise; /** * Marks all notifications as read for a given user. * @param {number} userId - The ID of the user for whom all notifications should be marked as read. * @returns {Promise} A promise that resolves when the operation is complete. */ markAllAsRead(userId: number): Promise; /** * Returns the count of user notifications: all notifications or only unread ones. * If unreadOnly is true, returns the number of unread notifications. * If unreadOnly is false, returns the total number of notifications (read + unread) associated with the user. * * @param {number} userId - The ID of the user whose notification count is requested. * @param {boolean} [unreadOnly=false] - If true, counts only unread notifications; if false, counts all notifications. * @returns {Promise} A promise that resolves with the number of matching notifications. * * @example * const total = await service.getNotificationsCount(123); // all notifications * const unreadCount = await service.getNotificationsCount(123, true); // only unread */ getNotificationsCount(userId: number, unreadOnly?: boolean): Promise; /** * Convenience method to get the number of unread notifications for a user. * Equivalent to calling getNotificationsCount(userId, true). * * @param {number} userId - The ID of the user. * @returns {Promise} A promise that resolves with the number of unread notifications. * * @example * const unread = await service.getUnreadNotificationsCount(123); */ getUnreadNotificationsCount(userId: number): Promise; }