|
|
@ -1,17 +1,15 @@ |
|
|
|
import { EventEmitter, Injectable } from '@angular/core'; |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
|
|
|
|
import { map, switchMap, tap, take, debounceTime } from 'rxjs/operators'; |
|
|
|
import { applyTransaction, ID, setLoading } from '@datorama/akita'; |
|
|
|
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 { 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 { NotificationsService } from 'core-app/shared/components/notifications/notifications.service'; |
|
|
|
import { InAppNotificationsQuery } from 'core-app/features/in-app-notifications/store/in-app-notifications.query'; |
|
|
|
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 { HalResource } from 'core-app/features/hal/resources/hal-resource'; |
|
|
|
import { InAppNotificationsStore } from './in-app-notifications.store'; |
|
|
|
import { InAppNotificationsStore } from './in-app-notifications.store'; |
|
|
|
import { InAppNotification, NOTIFICATIONS_MAX_SIZE } from './in-app-notification.model'; |
|
|
|
import { InAppNotification, NOTIFICATIONS_MAX_SIZE } from './in-app-notification.model'; |
|
|
|
import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' }) |
|
|
|
@Injectable() |
|
|
|
export class InAppNotificationsService { |
|
|
|
export class InAppNotificationsService { |
|
|
|
constructor( |
|
|
|
constructor( |
|
|
|
private store:InAppNotificationsStore, |
|
|
|
private store:InAppNotificationsStore, |
|
|
@ -19,43 +17,50 @@ export class InAppNotificationsService { |
|
|
|
private apiV3Service:APIV3Service, |
|
|
|
private apiV3Service:APIV3Service, |
|
|
|
private notifications:NotificationsService, |
|
|
|
private notifications:NotificationsService, |
|
|
|
) { |
|
|
|
) { |
|
|
|
|
|
|
|
this.query.activeFetchParameters$ |
|
|
|
|
|
|
|
.pipe(debounceTime(0)) |
|
|
|
|
|
|
|
.subscribe(() => { |
|
|
|
|
|
|
|
this.fetchNotifications(); |
|
|
|
|
|
|
|
this.fetchCount(); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public notificationsOfWpLoaded:EventEmitter<IHALCollection<InAppNotification>> = new EventEmitter<IHALCollection<InAppNotification>>(); |
|
|
|
fetchNotifications() { |
|
|
|
|
|
|
|
|
|
|
|
get():void { |
|
|
|
|
|
|
|
this.store.setLoading(true); |
|
|
|
this.store.setLoading(true); |
|
|
|
|
|
|
|
|
|
|
|
const facet = this.query.getValue().activeFacet; |
|
|
|
const { activeFacet, activeFilters } = this.query.getValue(); |
|
|
|
|
|
|
|
|
|
|
|
this |
|
|
|
const call = this |
|
|
|
.apiV3Service |
|
|
|
.apiV3Service |
|
|
|
.notifications |
|
|
|
.notifications |
|
|
|
.facet(facet, { pageSize: NOTIFICATIONS_MAX_SIZE }) |
|
|
|
.facet(activeFacet, { |
|
|
|
|
|
|
|
pageSize: NOTIFICATIONS_MAX_SIZE, |
|
|
|
|
|
|
|
filters: activeFilters, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call |
|
|
|
.pipe( |
|
|
|
.pipe( |
|
|
|
tap((events) => this.sideLoadInvolvedWorkPackages(events._embedded.elements)), |
|
|
|
tap((events) => this.sideLoadInvolvedWorkPackages(events._embedded.elements)), |
|
|
|
) |
|
|
|
) |
|
|
|
.subscribe( |
|
|
|
.subscribe( |
|
|
|
(events) => { |
|
|
|
(events) => applyTransaction(() => { |
|
|
|
applyTransaction(() => { |
|
|
|
|
|
|
|
this.store.set(events._embedded.elements); |
|
|
|
this.store.set(events._embedded.elements); |
|
|
|
this.store.update({ notShowing: events.total - events.count }); |
|
|
|
this.store.update({ notShowing: events.total - events.count }); |
|
|
|
}); |
|
|
|
}), |
|
|
|
}, |
|
|
|
(error) => this.notifications.addError(error), |
|
|
|
(error) => { |
|
|
|
|
|
|
|
this.notifications.addError(error); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
.add( |
|
|
|
.add(() => this.store.setLoading(false)); |
|
|
|
() => this.store.setLoading(false), |
|
|
|
|
|
|
|
); |
|
|
|
return call; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
count$():Observable<number> { |
|
|
|
fetchCount() { |
|
|
|
|
|
|
|
const { activeFilters } = this.query.getValue(); |
|
|
|
|
|
|
|
|
|
|
|
return this |
|
|
|
return this |
|
|
|
.apiV3Service |
|
|
|
.apiV3Service |
|
|
|
.notifications |
|
|
|
.notifications |
|
|
|
.unread({ pageSize: 0 }) |
|
|
|
.unread({ pageSize: 0, filters: activeFilters }) |
|
|
|
.pipe( |
|
|
|
.pipe( |
|
|
|
map((events) => events.total), |
|
|
|
map((events) => events.total), |
|
|
|
tap((unreadCount) => { |
|
|
|
tap((unreadCount) => { |
|
|
@ -72,8 +77,12 @@ export class InAppNotificationsService { |
|
|
|
this.store.update((state) => ({ ...state, activeFacet: facet })); |
|
|
|
this.store.update((state) => ({ ...state, activeFacet: facet })); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
markAllRead():void { |
|
|
|
setActiveFilters(filters:ApiV3ListFilter[]):void { |
|
|
|
this.query |
|
|
|
this.store.update((state) => ({ ...state, activeFilters: filters })); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
markAllRead() { |
|
|
|
|
|
|
|
return this.query |
|
|
|
.unread$ |
|
|
|
.unread$ |
|
|
|
.pipe( |
|
|
|
.pipe( |
|
|
|
take(1), |
|
|
|
take(1), |
|
|
@ -88,10 +97,10 @@ export class InAppNotificationsService { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
markAsRead(notifications:InAppNotification[], keep = false):void { |
|
|
|
markAsRead(notifications:InAppNotification[], keep = false) { |
|
|
|
const ids = notifications.map((n) => n.id); |
|
|
|
const ids = notifications.map((n) => n.id); |
|
|
|
|
|
|
|
|
|
|
|
this |
|
|
|
return this |
|
|
|
.apiV3Service |
|
|
|
.apiV3Service |
|
|
|
.notifications |
|
|
|
.notifications |
|
|
|
.markRead(ids) |
|
|
|
.markRead(ids) |
|
|
@ -108,24 +117,6 @@ export class InAppNotificationsService { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
loadNotificationsOfWorkPackage(workPackageId:string):void { |
|
|
|
|
|
|
|
this |
|
|
|
|
|
|
|
.apiV3Service |
|
|
|
|
|
|
|
.notifications |
|
|
|
|
|
|
|
.facet( |
|
|
|
|
|
|
|
'unread', |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
pageSize: NOTIFICATIONS_MAX_SIZE, |
|
|
|
|
|
|
|
filters: [ |
|
|
|
|
|
|
|
['resourceId', '=', [workPackageId]], |
|
|
|
|
|
|
|
['resourceType', '=', ['WorkPackage']], |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
).subscribe((notificationCollection) => { |
|
|
|
|
|
|
|
this.notificationsOfWpLoaded.emit(notificationCollection); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private sideLoadInvolvedWorkPackages(elements:InAppNotification[]) { |
|
|
|
private sideLoadInvolvedWorkPackages(elements:InAppNotification[]) { |
|
|
|
const wpIds = elements.map((element) => { |
|
|
|
const wpIds = elements.map((element) => { |
|
|
|
const href = element._links.resource?.href; |
|
|
|
const href = element._links.resource?.href; |
|
|
@ -137,22 +128,4 @@ export class InAppNotificationsService { |
|
|
|
.work_packages |
|
|
|
.work_packages |
|
|
|
.requireAll(_.compact(wpIds)); |
|
|
|
.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, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|