diff --git a/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.spec.ts b/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.spec.ts index 1384bb09502c49a9f034cb437339201a3354cb96..c472d34086585418d4950f4e303de08cb7c02738 100644 --- a/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.spec.ts +++ b/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.spec.ts @@ -26,7 +26,7 @@ import { CommandListResource, CommandResource, CommandService } from '@goofy-cli import { createEmptyStateResource, createStateResource, NavigationService, StateResource } from '@goofy-client/tech-shared'; import { Mock, mock, useFromMock } from '@goofy-client/test-utils'; import { getUrl, ResourceUri } from '@ngxp/rest'; -import { cold, hot } from 'jest-marbles'; +import { cold } from 'jest-marbles'; import { createBinaryFileListResource } from 'libs/binary-file-shared/test/binary-file'; import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel'; import { createCommandListResource, createCommandResource } from 'libs/command-shared/test/command'; @@ -350,49 +350,46 @@ describe('VorgangService', () => { const vorgangWithEingangResource: VorgangWithEingangResource = createVorgangWithEingangResource(); const vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource> = createStateResource(vorgangWithEingangResource); - describe('init and next value', () => { + beforeEach(() => { + vorgangFacade.getVorgangWithEingang.mockReturnValue(of(vorgangWithEingangStateResource)); + commandService.createCommand.mockReturnValue(of(commandStateResource)); - beforeEach(() => { - vorgangFacade.getVorgangWithEingang.mockReturnValue(hot('-a', { a: vorgangWithEingangStateResource })); - commandService.createCommand.mockReturnValue(hot('-a', { a: commandStateResource })); - service.reloadVorgangOnDone = jest.fn(); - }) + service.setAssignUserCommandLoading = jest.fn(); + service.setAssignUserCommand = jest.fn(); + }) - it('should be return', () => { - const result = service.assignUser(userProfileUri); + it('should set assign user command loading', () => { + service.assignUser(userProfileUri); - expect(result).toBeObservable(cold('ab', { a: createEmptyStateResource(true), b: commandStateResource })); - }) + expect(service.setAssignUserCommandLoading).toHaveBeenCalled(); }) - describe('further process', () => { + it('should call facade get vorgangWithEingang', () => { + service.assignUser(userProfileUri); - beforeEach(() => { - vorgangFacade.getVorgangWithEingang.mockReturnValue(of(vorgangWithEingangStateResource)); - commandService.createCommand.mockReturnValue(of(commandStateResource)); - }) + expect(vorgangFacade.getVorgangWithEingang).toHaveBeenCalled(); + }) - it('should call facade get vorgangWithEingang', () => { - service.assignUser(userProfileUri); + it('should call command service', () => { + const order: CreateAssignUserCommand = { order: VorgangOrder.ASSIGN_USER, body: { assignedTo: userProfileUri } }; - expect(vorgangFacade.getVorgangWithEingang).toHaveBeenCalled(); - }) + service.assignUser(userProfileUri).subscribe(); - it('should call command service', () => { - const order: CreateAssignUserCommand = { order: VorgangOrder.ASSIGN_USER, body: { assignedTo: userProfileUri } }; + expect(commandService.createCommand).toHaveBeenCalledWith(vorgangWithEingangResource, VorgangWithEingangLinkRel.ASSIGN, order); + }) - service.assignUser(userProfileUri).subscribe(); + it('should reload vorgang on command is done', () => { + service.reloadVorgang = jest.fn(); - expect(commandService.createCommand).toHaveBeenCalledWith(vorgangWithEingangResource, VorgangWithEingangLinkRel.ASSIGN, order); - }) + service.assignUser(userProfileUri).subscribe(); - it('should reload vorgang on command is done', () => { - service.reloadVorgang = jest.fn(); + expect(service.reloadVorgang).toHaveBeenCalled(); + }) - service.assignUser(userProfileUri).subscribe(); + it('should set assign user command on command is done', () => { + service.assignUser(userProfileUri); - expect(service.reloadVorgang).toHaveBeenCalled(); - }) + expect(service.setAssignUserCommand).toHaveBeenCalledWith(commandStateResource); }) }) }) diff --git a/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.ts b/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.ts index b3a76e1bd90f5158c4d01f29a9aa1f8bb540bdcb..23c8e58bfbe829eaee005d4473cbb7c77dad53eb 100644 --- a/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.ts +++ b/goofy-client/libs/vorgang-shared/src/lib/vorgang.service.ts @@ -26,11 +26,11 @@ import { Params } from '@angular/router'; import { BinaryFileListResource } from '@goofy-client/binary-file-shared'; import { CommandListResource, CommandResource, CommandService, getPendingCommandByOrder, hasError, isDone } from '@goofy-client/command-shared'; import { PostfachOrder } from '@goofy-client/postfach-shared'; -import { createEmptyStateResource, createStateResource, doIfLoadingRequired, isNotUndefined, NavigationService, StateResource } from '@goofy-client/tech-shared'; +import { createEmptyStateResource, createStateResource, doIfLoadingRequired, isNotNil, isNotUndefined, NavigationService, StateResource } from '@goofy-client/tech-shared'; import { getUrl, hasLink, ResourceUri } from '@ngxp/rest'; import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel'; import { BehaviorSubject, Observable, Subscription } from 'rxjs'; -import { first, mergeMap, startWith, tap } from 'rxjs/operators'; +import { first, startWith, tap } from 'rxjs/operators'; import { VorgangFacade } from './+state/vorgang.facade'; import { VorgangRepository } from './+state/vorgang.repository'; import { VorgangWithEingangLinkRel } from './vorgang.linkrel'; @@ -45,7 +45,10 @@ export class VorgangService { private readonly attachments$: BehaviorSubject<StateResource<BinaryFileListResource>> = new BehaviorSubject<StateResource<BinaryFileListResource>>(createEmptyStateResource<BinaryFileListResource>()); private readonly representations$: BehaviorSubject<StateResource<BinaryFileListResource>> = new BehaviorSubject<StateResource<BinaryFileListResource>>(createEmptyStateResource<BinaryFileListResource>()); + private readonly assignUserCommand$: BehaviorSubject<StateResource<CommandResource>> = new BehaviorSubject<StateResource<CommandResource>>(createEmptyStateResource<CommandResource>()); + private navigationSubscription: Subscription; + private assignUserSubscription: Subscription; public static readonly VORGANG_WITH_EINGANG_URL: string = 'vorgangWithEingangUrl'; @@ -212,11 +215,19 @@ export class VorgangService { } public assignUser(userUri: ResourceUri): Observable<StateResource<CommandResource>> { - return this.vorgangFacade.getVorgangWithEingang().pipe( - mergeMap((vorgangWithEingang) => this.commandService.createCommand(vorgangWithEingang.resource, VorgangWithEingangLinkRel.ASSIGN, this.createAssignUserOrder(userUri))), - tap(commandStateResource => this.reloadVorgangOnDone(commandStateResource)), - startWith(createEmptyStateResource<CommandResource>(true)) - ) + this.setAssignUserCommandLoading(); + + this.vorgangFacade.getVorgangWithEingang().pipe(first()).subscribe(vorgangWithEingang => { + this.assignUserSubscription = this.commandService.createCommand(vorgangWithEingang.resource, VorgangWithEingangLinkRel.ASSIGN, this.createAssignUserOrder(userUri)).subscribe(commandStateResource => { + this.reloadVorgangOnDone(commandStateResource); + }) + }); + + return this.assignUserCommand$.asObservable(); + } + + setAssignUserCommandLoading(): void { + this.assignUserCommand$.next({ ...this.assignUserCommand$.value, loading: true }); } private createAssignUserOrder(assignedTo: ResourceUri): CreateAssignUserCommand { @@ -225,10 +236,16 @@ export class VorgangService { reloadVorgangOnDone(commandResource: StateResource<CommandResource>): void { if (isDone(commandResource.resource) && !hasError(commandResource.resource)) { + this.setAssignUserCommand(commandResource); this.reloadVorgang(commandResource.resource); + if(isNotNil(this.assignUserSubscription)) this.assignUserSubscription.unsubscribe(); } } + setAssignUserCommand(commandStateResource: StateResource<CommandResource>): void { + this.assignUserCommand$.next(commandStateResource); + } + public reloadVorgang(commandResource: CommandResource): void { this.loadVorgangWithEingang(getUrl(commandResource, CommandLinkRel.EFFECTED_RESOURCE)); }