diff --git a/apps/red-ui/src/app/services/notifications.service.ts b/apps/red-ui/src/app/services/notifications.service.ts index b4187e969..42cb282be 100644 --- a/apps/red-ui/src/app/services/notifications.service.ts +++ b/apps/red-ui/src/app/services/notifications.service.ts @@ -1,14 +1,15 @@ import { Inject, Injectable } from '@angular/core'; -import { BASE_HREF, EntitiesService, List, mapEach, QueryParam, RequiredParam, Validate } from '@iqser/common-ui'; +import { BASE_HREF, EntitiesService, getConfig, List, mapEach, QueryParam, RequiredParam, Validate } from '@iqser/common-ui'; import { TranslateService } from '@ngx-translate/core'; import { EMPTY, firstValueFrom, iif, Observable, of, timer } from 'rxjs'; -import { Dossier, INotification, Notification, NotificationTypes } from '@red/domain'; +import { AppConfig, Dossier, INotification, Notification, NotificationTypes } from '@red/domain'; import { map, switchMap, tap } from 'rxjs/operators'; import { notificationsTranslations } from '@translations/notifications-translations'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { UserService } from '@users/user.service'; import { CHANGED_CHECK_INTERVAL } from '@utils/constants'; import { DossiersCacheService } from './dossiers/dossiers-cache.service'; +import dayjs from 'dayjs'; const INCLUDE_SEEN = false; @@ -19,6 +20,8 @@ export class NotificationsService extends EntitiesService(); + constructor( @Inject(BASE_HREF) private readonly _baseHref: string, private readonly _translateService: TranslateService, @@ -58,12 +61,36 @@ export class NotificationsService extends EntitiesService(this._defaultModelPath, [queryParam]).pipe( - map(response => response.notifications.filter(n => n.notificationType in NotificationTypes)), + map(response => this.#filterNotifications(response.notifications)), mapEach(notification => this._new(notification)), tap(notifications => this.setEntities(notifications)), ); } + #filterNotifications(notifications: Notification[]): Notification[] { + const todayDate = dayjs(new Date()); + + notifications = notifications.filter(n => { + if (!(n.notificationType in NotificationTypes)) { + return false; + } + + const readDate = dayjs(n.readDate); + if (!readDate.isValid()) { + return true; + } + + const creationDate = dayjs(n.creationDate); + if (todayDate.diff(creationDate, 'day') <= this.#config.AVAILABLE_NOTIFICATIONS_DAYS) { + return true; + } + + return todayDate.diff(readDate, 'minute') <= this.#config.AVAILABLE_OLD_NOTIFICATIONS_MINUTES; + }); + + return notifications.slice(0, this.#config.NOTIFICATIONS_THRESHOLD); + } + #loadNotificationsIfChanged(): Observable { return this.hasChanges$().pipe(switchMap(changed => iif(() => changed, this.loadAll(), EMPTY))); } diff --git a/apps/red-ui/src/assets/config/config.json b/apps/red-ui/src/assets/config/config.json index f4b69417a..c81f4bc32 100644 --- a/apps/red-ui/src/assets/config/config.json +++ b/apps/red-ui/src/assets/config/config.json @@ -17,5 +17,8 @@ "MANUAL_BASE_URL": "https://docs.redactmanager.com/preview", "ANNOTATIONS_THRESHOLD": 1000, "THEME": "redact", - "BASE_TRANSLATIONS_DIRECTORY": "/assets/i18n/redact/" + "BASE_TRANSLATIONS_DIRECTORY": "/assets/i18n/redact/", + "AVAILABLE_NOTIFICATIONS_DAYS": 30, + "AVAILABLE_OLD_NOTIFICATIONS_MINUTES": 60, + "NOTIFICATIONS_THRESHOLD": 1000 } diff --git a/libs/red-domain/src/lib/shared/app-config.ts b/libs/red-domain/src/lib/shared/app-config.ts index 3ebb0c1d1..050a999a0 100644 --- a/libs/red-domain/src/lib/shared/app-config.ts +++ b/libs/red-domain/src/lib/shared/app-config.ts @@ -12,4 +12,7 @@ export interface AppConfig extends IqserAppConfig { readonly SELECTION_MODE: string; readonly ANNOTATIONS_THRESHOLD: number; readonly THEME: string; + readonly AVAILABLE_NOTIFICATIONS_DAYS: number; + readonly AVAILABLE_OLD_NOTIFICATIONS_MINUTES: number; + readonly NOTIFICATIONS_THRESHOLD: number; }