diff --git a/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.spec.ts b/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.spec.ts index 522ed1f6ce6a4ae086d1226253f1d1236eb3eb9d..31a2db7ad13ea25fc4d11f2d815978e6a9c31795 100644 --- a/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.spec.ts +++ b/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.spec.ts @@ -35,7 +35,7 @@ import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel'; import { createCommandResource } from 'libs/command-shared/test/command'; import { createKommentar, createKommentarListResource, createKommentarResource } from 'libs/kommentar-shared/test/kommentar'; import { createVorgangWithEingangResource } from 'libs/vorgang-shared/test/vorgang'; -import { of } from 'rxjs'; +import { of, Subscription } from 'rxjs'; import { singleCold } from '../../../tech-shared/test/marbles'; import { KommentarLinkRel, KommentarListLinkRel } from './kommentar.linkrel'; import { Kommentar, KOMMENTAR_UPLOADED_ATTACHMENTS, KommentarListResource, KommentarResource } from './kommentar.model'; @@ -51,9 +51,12 @@ describe('KommentarService', () => { let binaryFileService: Mock<BinaryFileService>; const vorgang: VorgangWithEingangResource = createVorgangWithEingangResource(); + const vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource> = createStateResource(vorgang); + const kommentarList: KommentarListResource = createKommentarListResource(); const kommentar: Kommentar = createKommentar(); const kommentarResource: KommentarResource = createKommentarResource(); + const command: CommandResource = createCommandResource(); const commandStateResource: StateResource<CommandResource> = createStateResource(command); @@ -61,7 +64,10 @@ describe('KommentarService', () => { repository = mock(KommentarRepository); commandService = mock(CommandService); navigationService = mock(NavigationService); - vorgangService = mock(VorgangService); + vorgangService = { + ...mock(VorgangService), + getVorgangWithEingang: jest.fn().mockReturnValue(of(vorgangWithEingangStateResource)), + }; binaryFileService = mock(BinaryFileService); navigationService.urlChanged.mockReturnValue(of({})); @@ -81,6 +87,12 @@ describe('KommentarService', () => { service.setListLoadingTrue = jest.fn(); service.setKommentarList = jest.fn(); + service._refreshVorgangSubscription = jest.fn(); + }); + + it('should call refresh vorgang subscription', () => { + service.getKommentareByVorgang(vorgang); + expect(service._refreshVorgangSubscription).toHaveBeenCalled(); }); it('should set loading to true', () => { @@ -102,6 +114,24 @@ describe('KommentarService', () => { }); }); + describe('refresh vorgang subscription', () => { + beforeEach(() => { + service.vorgangSubscription = <any>mock(Subscription); + service._listenToVorgangChange = jest.fn(); + }); + + it('should unsubscribe existing subscription', () => { + service._refreshVorgangSubscription(); + + expect(service.vorgangSubscription.unsubscribe).toHaveBeenCalled(); + }); + it('should call listen to vorgang change', () => { + service._refreshVorgangSubscription(); + + expect(service._listenToVorgangChange).toHaveBeenCalled(); + }); + }); + describe('createKommentar', () => { describe('init and next value', () => { beforeEach(() => { @@ -421,4 +451,63 @@ describe('KommentarService', () => { expect(service._currentlyEdited$).toBeObservable(singleCold(resourceUri)); }); }); + + describe('listen to vorgang change', () => { + beforeEach(() => { + service._handleVorgangChange = jest.fn(); + }); + + it('should call vorgang service to get vorgang with eingang', () => { + service._listenToVorgangChange(); + + expect(vorgangService.getVorgangWithEingang).toHaveBeenCalled(); + }); + + it('should call handle vorgang change', () => { + service._listenToVorgangChange(); + + expect(service._handleVorgangChange).toHaveBeenCalledWith(vorgangWithEingangStateResource); + }); + }); + + describe('handle vorgang change', () => { + it('should set reload flag if list is loaded and state resource is loading', () => { + service.shouldReload = false; + service.kommentarList$.next(createStateResource(createKommentarListResource())); + + service._handleVorgangChange({ ...vorgangWithEingangStateResource, loading: true }); + + expect(service.shouldReload).toBeTruthy(); + }); + + it('should set reload flag if list is loaded and state resource is reloading', () => { + service.kommentarList$.next(createStateResource(createKommentarListResource())); + service.shouldReload = false; + + service._handleVorgangChange({ ...vorgangWithEingangStateResource, reload: true }); + + expect(service.shouldReload).toBeTruthy(); + }); + + describe('on setted reload flag', () => { + beforeEach(() => { + service.setKommentarListReload = jest.fn(); + service.shouldReload = true; + }); + + describe('and loaded vorgang resource', () => { + it('should call set kommentar list reload', () => { + service._handleVorgangChange(vorgangWithEingangStateResource); + + expect(service.setKommentarListReload).toHaveBeenCalled(); + }); + + it('and loaded vorgang resource should call set kommentar list reload', () => { + service._handleVorgangChange(vorgangWithEingangStateResource); + + expect(service.shouldReload).toBeFalsy(); + }); + }); + }); + }); }); diff --git a/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts b/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts index 0055824e34836c3ad1195ce85d01ff4f2b772a35..483e35721f552696b72600b843a123702f20145f 100644 --- a/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts +++ b/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts @@ -24,8 +24,17 @@ import { BinaryFileListResource, BinaryFileService } from '@alfa-client/binary-file-shared'; import { CommandOrder, CommandResource, CommandService, CreateCommand, isDone } from '@alfa-client/command-shared'; import { NavigationService } from '@alfa-client/navigation-shared'; -import { createEmptyStateResource, createStateResource, doIfLoadingRequired, EMPTY_STRING, isNotEmpty, StateResource, } from '@alfa-client/tech-shared'; -import { VorgangResource, VorgangService } from '@alfa-client/vorgang-shared'; +import { + createEmptyStateResource, + createStateResource, + doIfLoadingRequired, + EMPTY_STRING, + isLoaded, + isNotEmpty, + isNotNull, + StateResource, +} from '@alfa-client/tech-shared'; +import { VorgangResource, VorgangService, VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import { Injectable } from '@angular/core'; import { Params } from '@angular/router'; import { hasLink, Resource, ResourceUri } from '@ngxp/rest'; @@ -46,6 +55,10 @@ export class KommentarService { private navigationSub: Subscription; + vorgangSubscription: Subscription; + + shouldReload: boolean = false; + constructor( private repository: KommentarRepository, private commandService: CommandService, @@ -54,6 +67,7 @@ export class KommentarService { private binaryFileService: BinaryFileService, ) { this.listenToNavigation(); + this._listenToVorgangChange(); } private listenToNavigation(): void { @@ -81,10 +95,41 @@ export class KommentarService { } public getKommentareByVorgang(vorgang: VorgangResource): Observable<StateResource<KommentarListResource>> { + this._refreshVorgangSubscription(); doIfLoadingRequired(this.kommentarList$.value, () => this.loadKommentare(vorgang)); return this.kommentarList$.asObservable(); } + _refreshVorgangSubscription(): void { + this.vorgangSubscription.unsubscribe(); + this._listenToVorgangChange(); + } + + _listenToVorgangChange() { + this.vorgangSubscription = this.vorgangService + .getVorgangWithEingang() + .subscribe((vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>) => + this._handleVorgangChange(vorgangWithEingangStateResource), + ); + } + + _handleVorgangChange(vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>): void { + if (this.shouldReloadList(vorgangWithEingangStateResource)) { + this.shouldReload = true; + } + if (isLoaded(vorgangWithEingangStateResource) && this.shouldReload) { + this.setKommentarListReload(); + this.shouldReload = false; + } + } + + private shouldReloadList(vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>): boolean { + return ( + (vorgangWithEingangStateResource.loading || vorgangWithEingangStateResource.reload) && + isNotNull(this.kommentarList$.value.resource) + ); + } + private loadKommentare(vorgang: VorgangResource): void { this.setListLoadingTrue();