RED-6174 - The user should only see relevant notifications
This commit is contained in:
parent
f43fa76ce1
commit
19d04e63f6
@ -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<INotification, Notific
|
||||
protected readonly _defaultModelPath = 'notification';
|
||||
protected readonly _entityClass = Notification;
|
||||
|
||||
readonly #config = getConfig<AppConfig>();
|
||||
|
||||
constructor(
|
||||
@Inject(BASE_HREF) private readonly _baseHref: string,
|
||||
private readonly _translateService: TranslateService,
|
||||
@ -58,12 +61,36 @@ export class NotificationsService extends EntitiesService<INotification, Notific
|
||||
const queryParam: QueryParam = { key: 'includeSeen', value: includeSeen };
|
||||
|
||||
return this.getAll<{ notifications: Notification[] }>(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<Notification[]> {
|
||||
return this.hasChanges$().pipe(switchMap(changed => iif(() => changed, this.loadAll(), EMPTY)));
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user