adminizer 4.4.0-build.153 → 4.4.0-build.154

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -161,4 +161,29 @@ export declare abstract class AbstractNotificationService extends EventEmitter {
161
161
  * @returns {Promise<void>} A promise that resolves when the operation is complete.
162
162
  */
163
163
  markAllAsRead(userId: number): Promise<void>;
164
+ /**
165
+ * Returns the count of user notifications: all notifications or only unread ones.
166
+ * If unreadOnly is true, returns the number of unread notifications.
167
+ * If unreadOnly is false, returns the total number of notifications (read + unread) associated with the user.
168
+ *
169
+ * @param {number} userId - The ID of the user whose notification count is requested.
170
+ * @param {boolean} [unreadOnly=false] - If true, counts only unread notifications; if false, counts all notifications.
171
+ * @returns {Promise<number>} A promise that resolves with the number of matching notifications.
172
+ *
173
+ * @example
174
+ * const total = await service.getNotificationsCount(123); // all notifications
175
+ * const unreadCount = await service.getNotificationsCount(123, true); // only unread
176
+ */
177
+ getNotificationsCount(userId: number, unreadOnly?: boolean): Promise<number>;
178
+ /**
179
+ * Convenience method to get the number of unread notifications for a user.
180
+ * Equivalent to calling getNotificationsCount(userId, true).
181
+ *
182
+ * @param {number} userId - The ID of the user.
183
+ * @returns {Promise<number>} A promise that resolves with the number of unread notifications.
184
+ *
185
+ * @example
186
+ * const unread = await service.getUnreadNotificationsCount(123);
187
+ */
188
+ getUnreadNotificationsCount(userId: number): Promise<number>;
164
189
  }
@@ -350,4 +350,61 @@ export class AbstractNotificationService extends EventEmitter {
350
350
  Adminizer.log.error('Error marking all notifications as read:', error);
351
351
  }
352
352
  }
353
+ /**
354
+ * Returns the count of user notifications: all notifications or only unread ones.
355
+ * If unreadOnly is true, returns the number of unread notifications.
356
+ * If unreadOnly is false, returns the total number of notifications (read + unread) associated with the user.
357
+ *
358
+ * @param {number} userId - The ID of the user whose notification count is requested.
359
+ * @param {boolean} [unreadOnly=false] - If true, counts only unread notifications; if false, counts all notifications.
360
+ * @returns {Promise<number>} A promise that resolves with the number of matching notifications.
361
+ *
362
+ * @example
363
+ * const total = await service.getNotificationsCount(123); // all notifications
364
+ * const unreadCount = await service.getNotificationsCount(123, true); // only unread
365
+ */
366
+ async getNotificationsCount(userId, unreadOnly = false) {
367
+ try {
368
+ // Формируем условие фильтрации по статусу прочтения
369
+ const whereClause = { userId: userId };
370
+ if (unreadOnly) {
371
+ whereClause.read = false; // только непрочитанные
372
+ }
373
+ // если unreadOnly = false — получаем ВСЕ записи (и прочитанные, и нет)
374
+ // Получаем все user-notification связи
375
+ const userNotifications = await this.adminizer.modelHandler.model.get('usernotificationap')["_find"]({
376
+ where: whereClause
377
+ }, { populate: [['notificationId', {}]] });
378
+ if (userNotifications.length === 0)
379
+ return 0;
380
+ // Фильтруем по классу уведомлений через notificationId
381
+ const validNotificationIds = userNotifications
382
+ .map((un) => un.notificationId.id)
383
+ .filter((id) => id != null);
384
+ if (validNotificationIds.length === 0)
385
+ return 0;
386
+ // Пересчитываем только те, у которых notificationClass совпадает
387
+ return await this.adminizer.modelHandler.model.get('notificationap')["_count"]({
388
+ id: validNotificationIds,
389
+ notificationClass: this.notificationClass
390
+ });
391
+ }
392
+ catch (error) {
393
+ Adminizer.log.error(`Error counting ${unreadOnly ? 'unread' : 'all'} notifications:`, error);
394
+ return 0;
395
+ }
396
+ }
397
+ /**
398
+ * Convenience method to get the number of unread notifications for a user.
399
+ * Equivalent to calling getNotificationsCount(userId, true).
400
+ *
401
+ * @param {number} userId - The ID of the user.
402
+ * @returns {Promise<number>} A promise that resolves with the number of unread notifications.
403
+ *
404
+ * @example
405
+ * const unread = await service.getUnreadNotificationsCount(123);
406
+ */
407
+ async getUnreadNotificationsCount(userId) {
408
+ return this.getNotificationsCount(userId, true);
409
+ }
353
410
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "adminizer",
3
3
  "type": "module",
4
- "version": "4.4.0-build.153",
4
+ "version": "4.4.0-build.154",
5
5
  "main": "index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",