From 8fc1f85c59a81c1df62f8782dc3bdbfa5ba277e6 Mon Sep 17 00:00:00 2001 From: Martin <git@mail.de> Date: Thu, 20 Mar 2025 14:21:13 +0100 Subject: [PATCH] OZG-7501 refresh postfach nachricht list on vorgang change --- .../src/lib/kommentar.service.ts | 2 +- .../src/lib/postfach.service.spec.ts | 83 ++++++++++++++++++- .../src/lib/postfach.service.ts | 66 ++++++++++++++- .../src/lib/wiedervorlage.service.ts | 2 +- 4 files changed, 145 insertions(+), 8 deletions(-) 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 483e35721f..c5ef0dd2f8 100644 --- a/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts +++ b/alfa-client/libs/kommentar-shared/src/lib/kommentar.service.ts @@ -105,7 +105,7 @@ export class KommentarService { this._listenToVorgangChange(); } - _listenToVorgangChange() { + _listenToVorgangChange(): void { this.vorgangSubscription = this.vorgangService .getVorgangWithEingang() .subscribe((vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>) => diff --git a/alfa-client/libs/postfach-shared/src/lib/postfach.service.spec.ts b/alfa-client/libs/postfach-shared/src/lib/postfach.service.spec.ts index 678219f735..e2fde67bc1 100644 --- a/alfa-client/libs/postfach-shared/src/lib/postfach.service.spec.ts +++ b/alfa-client/libs/postfach-shared/src/lib/postfach.service.spec.ts @@ -35,7 +35,13 @@ import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel'; import { createCommandErrorResource, createCommandResource } from 'libs/command-shared/test/command'; import { createVorgangWithEingangResource } from 'libs/vorgang-shared/test/vorgang'; import { BehaviorSubject, of } from 'rxjs'; -import { createPostfachFeatures, createPostfachMail, createPostfachMailListResource, createPostfachMailResource, createPostfachSettings, } from '../../test/postfach'; +import { + createPostfachFeatures, + createPostfachMail, + createPostfachMailListResource, + createPostfachMailResource, + createPostfachSettings, +} from '../../test/postfach'; import { PostfachFacade } from './+state/postfach.facade'; import { PostfachMailLinkRel, PostfachMailListLinkRel } from './postfach.linkrel'; import { PostfachMessages } from './postfach.message'; @@ -50,7 +56,7 @@ describe('PostfachService', () => { let repository: Mock<PostfachRepository>; const commandService: Mock<CommandService> = mock(CommandService); const navigationService: Mock<NavigationService> = mock(NavigationService); - const vorgangService: Mock<VorgangService> = mock(VorgangService); + let vorgangService: Mock<VorgangService>; const snackbarService: Mock<SnackBarService> = mock(SnackBarService); const dialog: Mock<MatDialog> = <Mock<MatDialog>>{ closeAll: jest.fn() }; const postfachFacade: Mock<PostfachFacade> = mock(PostfachFacade); @@ -58,7 +64,14 @@ describe('PostfachService', () => { const urlChangedParams = {}; + const vorgang: VorgangWithEingangResource = createVorgangWithEingangResource(); + const vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource> = createStateResource(vorgang); + beforeEach(() => { + vorgangService = { + ...mock(VorgangService), + getVorgangWithEingang: jest.fn().mockReturnValue(of(vorgangWithEingangStateResource)), + }; navigationService.urlChanged = jest.fn(); navigationService.urlChanged.mockReturnValue(of(urlChangedParams)); @@ -423,6 +436,13 @@ describe('PostfachService', () => { service.setPostfachMailList = jest.fn(); vorgangService.getVorgangWithEingang.mockReturnValue(of(createStateResource(createVorgangWithEingangResource()))); service.postfachMailList$.next(createEmptyStateResource()); + service._refreshVorgangSubscription = jest.fn(); + }); + + it('should call refresh vorgang subscription', () => { + service.getPostfachMailListByVorgang(); + + expect(service._refreshVorgangSubscription).toHaveBeenCalled(); }); it('should set loading to true', () => { @@ -553,4 +573,63 @@ describe('PostfachService', () => { expect(service.clearUploadedFiles).toHaveBeenCalled(); }); }); + + 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.postfachMailList$.next(createStateResource(createPostfachMailListResource())); + + service._handleVorgangChange({ ...vorgangWithEingangStateResource, loading: true }); + + expect(service.shouldReload).toBeTruthy(); + }); + + it('should set reload flag if list is loaded and state resource is reloading', () => { + service.postfachMailList$.next(createStateResource(createPostfachMailListResource())); + service.shouldReload = false; + + service._handleVorgangChange({ ...vorgangWithEingangStateResource, reload: true }); + + expect(service.shouldReload).toBeTruthy(); + }); + + describe('on setted reload flag', () => { + beforeEach(() => { + service.setPostfachMailOnReload = jest.fn(); + service.shouldReload = true; + }); + + describe('and loaded vorgang resource', () => { + it('should call set kommentar list reload', () => { + service._handleVorgangChange(vorgangWithEingangStateResource); + + expect(service.setPostfachMailOnReload).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/postfach-shared/src/lib/postfach.service.ts b/alfa-client/libs/postfach-shared/src/lib/postfach.service.ts index e39019766d..2eef75c2cf 100644 --- a/alfa-client/libs/postfach-shared/src/lib/postfach.service.ts +++ b/alfa-client/libs/postfach-shared/src/lib/postfach.service.ts @@ -23,11 +23,26 @@ */ import { POSTFACH_NACHRICHT_UPLOADED_ATTACHMENTS } from '@admin-client/postfach-shared'; import { BinaryFileService } from '@alfa-client/binary-file-shared'; -import { CommandResource, CommandService, doIfCommandIsDone, hasCommandError, isDone, isPending, } from '@alfa-client/command-shared'; +import { + CommandResource, + CommandService, + doIfCommandIsDone, + hasCommandError, + isDone, + isPending, +} from '@alfa-client/command-shared'; import { NavigationService } from '@alfa-client/navigation-shared'; -import { createEmptyStateResource, createStateResource, doIfLoadingRequired, isNotNull, isNotUndefined, StateResource, } from '@alfa-client/tech-shared'; +import { + createEmptyStateResource, + createStateResource, + doIfLoadingRequired, + isLoaded, + isNotNull, + isNotUndefined, + StateResource, +} from '@alfa-client/tech-shared'; import { SnackBarService } from '@alfa-client/ui'; -import { VorgangResource, VorgangService } from '@alfa-client/vorgang-shared'; +import { VorgangResource, VorgangService, VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import { inject, Injectable } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { Params } from '@angular/router'; @@ -38,7 +53,14 @@ import { first, map, take, tap } from 'rxjs/operators'; import { PostfachFacade } from './+state/postfach.facade'; import { PostfachMailLinkRel, PostfachMailListLinkRel } from './postfach.linkrel'; import { PostfachMessages } from './postfach.message'; -import { CreatePostfachMailCommand, PostfachFeatures, PostfachMail, PostfachMailListResource, PostfachMailResource, PostfachSettings, } from './postfach.model'; +import { + CreatePostfachMailCommand, + PostfachFeatures, + PostfachMail, + PostfachMailListResource, + PostfachMailResource, + PostfachSettings, +} from './postfach.model'; import { PostfachRepository } from './postfach.repository'; import { createResendPostfachMailCommand, createSendPostfachMailCommand } from './postfach.util'; @@ -65,8 +87,13 @@ export class PostfachService { private vorgangSubscription: Subscription; private postfachNachrichtenListSubscription: Subscription; + private vorgangChangeSubscription: Subscription; + + shouldReload: boolean = false; + constructor() { this.listenToNavigation(); + this._listenToVorgangChange(); } public sendMail(postfachMail: PostfachMail): Observable<StateResource<CommandResource>> { @@ -245,6 +272,7 @@ export class PostfachService { } public getPostfachMailListByVorgang(): Observable<StateResource<PostfachMailListResource>> { + this._refreshVorgangSubscription(); doIfLoadingRequired(this.postfachMailList$.value, () => { this.setPostfachMailListLoading(); this.vorgangSubscription = this.vorgangService.getVorgangWithEingang().subscribe((vorgangWithEingangStateResource) => { @@ -257,6 +285,36 @@ export class PostfachService { return this.postfachMailList$.asObservable(); } + _refreshVorgangSubscription(): void { + this.vorgangChangeSubscription.unsubscribe(); + this._listenToVorgangChange(); + } + + _listenToVorgangChange(): void { + this.vorgangChangeSubscription = 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.setPostfachMailOnReload(); + this.shouldReload = false; + } + } + + private shouldReloadList(vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>): boolean { + return ( + (vorgangWithEingangStateResource.loading || vorgangWithEingangStateResource.reload) && + isNotNull(this.postfachMailList$.value.resource) + ); + } + setPostfachMailListLoading(): void { this.postfachMailList$.next({ ...this.postfachMailList$.value, loading: true }); } diff --git a/alfa-client/libs/wiedervorlage-shared/src/lib/wiedervorlage.service.ts b/alfa-client/libs/wiedervorlage-shared/src/lib/wiedervorlage.service.ts index a33839c5dc..a39e1a9ab7 100644 --- a/alfa-client/libs/wiedervorlage-shared/src/lib/wiedervorlage.service.ts +++ b/alfa-client/libs/wiedervorlage-shared/src/lib/wiedervorlage.service.ts @@ -120,7 +120,7 @@ export class WiedervorlageService implements OnDestroy { this._listenToVorgangChange(); } - _listenToVorgangChange() { + _listenToVorgangChange(): void { this.vorgangSubscription = this.vorgangService .getVorgangWithEingang() .subscribe((vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource>) => -- GitLab