diff --git a/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.spec.ts b/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.spec.ts index b07673efb637d2565358c33723a9953ee187923b..743c77d19e68c2f9486ddd0dc2a16fce69eb8ff0 100644 --- a/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.spec.ts +++ b/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.spec.ts @@ -37,19 +37,67 @@ describe('BescheidFacade', () => { }); describe('createBescheid', () => { - it('should dispatch "createCommand"', () => { - const vorgangWithEingang: VorgangWithEingangResource = createVorgangWithEingangResource(); - const createCommand: CreateCommand = createCreateCommand(CommandOrder.CREATE_BESCHEID); - - facade.createBescheid(vorgangWithEingang, createCommand); - - expect(store.dispatch).toHaveBeenCalledWith( - CommandActions.createCommand({ - resource: vorgangWithEingang, - linkRel: VorgangWithEingangLinkRel.CREATE_BESCHEID, - command: createCommand, - }), - ); + const vorgangWithEingang: VorgangWithEingangResource = createVorgangWithEingangResource(); + const createCommand: CreateCommand = createCreateCommand(CommandOrder.CREATE_BESCHEID); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + describe('with both Links', () => { + it('should dispatch "createCommand" to CREATE_BESCHEID', () => { + var vorgang = createVorgangWithEingangResource([ + VorgangWithEingangLinkRel.CREATE_BESCHEID, + VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT, + ]); + facade.createBescheid(vorgang, createCommand); + + expect(store.dispatch).toHaveBeenCalledWith( + CommandActions.createCommand({ + resource: vorgang, + linkRel: VorgangWithEingangLinkRel.CREATE_BESCHEID, + command: createCommand, + }), + ); + }); + }); + + describe('with CREATE_BESCHEID_DRAFT link', () => { + it('should dispatch "createCommand" to CREATE_BESCHEID_DRAFT', () => { + var vorgang = createVorgangWithEingangResource([ + VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT, + ]); + facade.createBescheid(vorgang, createCommand); + + expect(store.dispatch).toHaveBeenCalledWith( + CommandActions.createCommand({ + resource: vorgang, + linkRel: VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT, + command: createCommand, + }), + ); + }); + }); + + describe('with CREATE_BESCHEID link', () => { + it('should dispatch "createCommand" to CREATE_BESCHEID', () => { + var vorgang = createVorgangWithEingangResource([VorgangWithEingangLinkRel.CREATE_BESCHEID]); + + facade.createBescheid(vorgang, createCommand); + + expect(store.dispatch).toHaveBeenCalledWith( + CommandActions.createCommand({ + resource: vorgang, + linkRel: VorgangWithEingangLinkRel.CREATE_BESCHEID, + command: createCommand, + }), + ); + }); + }); + it('should emit error if link is missing()', () => { + var vorgang = createVorgangWithEingangResource(); + + // expect(facade.createBescheid(vorgang, createCommand)).toThrowError(); }); }); }); diff --git a/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.ts b/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.ts index 5d342a771e1852899d02df39b62cfe626590fd65..ad7d1eb0a886c1a5f648825eb356d4370622438e 100644 --- a/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.ts +++ b/alfa-client/libs/bescheid-shared/src/lib/+state/bescheid.facade.ts @@ -3,10 +3,11 @@ import { StateResource } from '@alfa-client/tech-shared'; import { VorgangWithEingangLinkRel, VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import { Injectable } from '@angular/core'; import { Store } from '@ngrx/store'; -import { Observable } from 'rxjs'; +import { Observable, throwError } from 'rxjs'; import * as CommandActions from '../../../../command-shared/src/lib/+state/command.actions'; import * as BescheidSelectors from './bescheid.selectors'; +import { hasLink } from '@ngxp/rest'; @Injectable({ providedIn: 'root' }) export class BescheidFacade { @@ -19,6 +20,19 @@ export class BescheidFacade { public createBescheid( vorgangWithEingang: VorgangWithEingangResource, command: CreateCommand, + ): void { + if (hasLink(vorgangWithEingang, VorgangWithEingangLinkRel.CREATE_BESCHEID)) { + return this.createBescheidKiel(vorgangWithEingang, command); + } + if (hasLink(vorgangWithEingang, VorgangWithEingangLinkRel.CREATE_BESCHEID_DRAFT)) { + return this.createBescheidDraft(vorgangWithEingang, command); + } + // throwError(() => 'missng Link CREATE_BESCHEID or CREATE_BESCHEID_DRAFT'); + } + + public createBescheidKiel( + vorgangWithEingang: VorgangWithEingangResource, + command: CreateCommand, ): void { this.store.dispatch( CommandActions.createCommand({