|
|
|
@ -1,62 +1,65 @@ |
|
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
|
import { |
|
|
|
|
debounceTime, |
|
|
|
|
switchMap, |
|
|
|
|
take, |
|
|
|
|
tap, |
|
|
|
|
catchError, |
|
|
|
|
} from 'rxjs/operators'; |
|
|
|
|
import { Subscription, Observable } from 'rxjs'; |
|
|
|
|
import { applyTransaction, ID, setLoading } from '@datorama/akita'; |
|
|
|
|
import { Observable } from 'rxjs'; |
|
|
|
|
import { ApiV3ListFilter } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface'; |
|
|
|
|
import { APIV3Service } from 'core-app/core/apiv3/api-v3.service'; |
|
|
|
|
import { map, switchMap, tap } from 'rxjs/operators'; |
|
|
|
|
import { NotificationsService } from 'core-app/shared/components/notifications/notifications.service'; |
|
|
|
|
import { InAppNotificationsQuery } from 'core-app/features/in-app-notifications/store/in-app-notifications.query'; |
|
|
|
|
import { take } from 'rxjs/internal/operators/take'; |
|
|
|
|
import { HalResource } from 'core-app/features/hal/resources/hal-resource'; |
|
|
|
|
import { InAppNotificationsStore } from './in-app-notifications.store'; |
|
|
|
|
import { InAppNotification, NOTIFICATIONS_MAX_SIZE } from './in-app-notification.model'; |
|
|
|
|
import { InAppNotification } from './in-app-notification.model'; |
|
|
|
|
import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type'; |
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' }) |
|
|
|
|
@Injectable() |
|
|
|
|
export class InAppNotificationsService { |
|
|
|
|
constructor( |
|
|
|
|
private store:InAppNotificationsStore, |
|
|
|
|
private query:InAppNotificationsQuery, |
|
|
|
|
public query:InAppNotificationsQuery, |
|
|
|
|
private apiV3Service:APIV3Service, |
|
|
|
|
private notifications:NotificationsService, |
|
|
|
|
) { |
|
|
|
|
this.query.activeFetchParameters$ |
|
|
|
|
.pipe( |
|
|
|
|
debounceTime(0), |
|
|
|
|
switchMap(() => this.fetchNotifications()), |
|
|
|
|
).subscribe(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get():void { |
|
|
|
|
fetchNotifications():Observable<IHALCollection<InAppNotification>> { |
|
|
|
|
this.store.setLoading(true); |
|
|
|
|
|
|
|
|
|
const facet = this.query.getValue().activeFacet; |
|
|
|
|
const { |
|
|
|
|
activeFacet, |
|
|
|
|
activeFilters, |
|
|
|
|
pageSize, |
|
|
|
|
} = this.query.getValue(); |
|
|
|
|
|
|
|
|
|
this |
|
|
|
|
return this |
|
|
|
|
.apiV3Service |
|
|
|
|
.notifications |
|
|
|
|
.facet(facet, { pageSize: NOTIFICATIONS_MAX_SIZE }) |
|
|
|
|
.facet(activeFacet, { |
|
|
|
|
pageSize, |
|
|
|
|
filters: activeFilters, |
|
|
|
|
}) |
|
|
|
|
.pipe( |
|
|
|
|
tap((events) => this.sideLoadInvolvedWorkPackages(events._embedded.elements)), |
|
|
|
|
) |
|
|
|
|
.subscribe( |
|
|
|
|
(events) => { |
|
|
|
|
tap((events) => { |
|
|
|
|
this.sideLoadInvolvedWorkPackages(events._embedded.elements); |
|
|
|
|
applyTransaction(() => { |
|
|
|
|
this.store.set(events._embedded.elements); |
|
|
|
|
this.store.update({ notShowing: events.total - events.count }); |
|
|
|
|
this.store.update({ notLoaded: events.total - events.count }); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
(error) => { |
|
|
|
|
this.store.setLoading(false); |
|
|
|
|
}), |
|
|
|
|
catchError((error) => { |
|
|
|
|
this.notifications.addError(error); |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
.add( |
|
|
|
|
() => this.store.setLoading(false), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
count$():Observable<number> { |
|
|
|
|
return this |
|
|
|
|
.apiV3Service |
|
|
|
|
.notifications |
|
|
|
|
.unread({ pageSize: 0 }) |
|
|
|
|
.pipe( |
|
|
|
|
map((events) => events.total), |
|
|
|
|
tap((unreadCount) => { |
|
|
|
|
this.store.update({ unreadCount }); |
|
|
|
|
throw error; |
|
|
|
|
}), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -65,12 +68,20 @@ export class InAppNotificationsService { |
|
|
|
|
this.store.update(id, inAppNotification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setPageSize(pageSize:number):void { |
|
|
|
|
this.store.update((state) => ({ ...state, pageSize })); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setActiveFacet(facet:string):void { |
|
|
|
|
this.store.update((state) => ({ ...state, activeFacet: facet })); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
markAllRead():void { |
|
|
|
|
this.query |
|
|
|
|
setActiveFilters(filters:ApiV3ListFilter[]):void { |
|
|
|
|
this.store.update((state) => ({ ...state, activeFilters: filters })); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
markAllRead():Subscription { |
|
|
|
|
return this.query |
|
|
|
|
.unread$ |
|
|
|
|
.pipe( |
|
|
|
|
take(1), |
|
|
|
@ -85,10 +96,10 @@ export class InAppNotificationsService { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
markAsRead(notifications:InAppNotification[], keep = false):void { |
|
|
|
|
markAsRead(notifications:InAppNotification[], keep = false):Subscription { |
|
|
|
|
const ids = notifications.map((n) => n.id); |
|
|
|
|
|
|
|
|
|
this |
|
|
|
|
return this |
|
|
|
|
.apiV3Service |
|
|
|
|
.notifications |
|
|
|
|
.markRead(ids) |
|
|
|
@ -105,7 +116,7 @@ export class InAppNotificationsService { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private sideLoadInvolvedWorkPackages(elements:InAppNotification[]) { |
|
|
|
|
private sideLoadInvolvedWorkPackages(elements:InAppNotification[]):void { |
|
|
|
|
const wpIds = elements.map((element) => { |
|
|
|
|
const href = element._links.resource?.href; |
|
|
|
|
return href && HalResource.matchFromLink(href, 'work_packages'); |
|
|
|
@ -116,22 +127,4 @@ export class InAppNotificationsService { |
|
|
|
|
.work_packages |
|
|
|
|
.requireAll(_.compact(wpIds)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
collapse(notification:InAppNotification):void { |
|
|
|
|
this.store.update( |
|
|
|
|
notification.id, |
|
|
|
|
{ |
|
|
|
|
expanded: false, |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expand(notification:InAppNotification):void { |
|
|
|
|
this.store.update( |
|
|
|
|
notification.id, |
|
|
|
|
{ |
|
|
|
|
expanded: true, |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|