From c2269e5b197b5af4b7cf2e9f14a287505207f03b Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Thu, 14 Mar 2024 10:30:44 +0100
Subject: [PATCH] OZG-5009 select link depending what is present

---
 .../src/lib/+state/bescheid.facade.spec.ts    | 74 +++++++++++++++----
 .../src/lib/+state/bescheid.facade.ts         | 16 +++-
 2 files changed, 76 insertions(+), 14 deletions(-)

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 b07673efb6..743c77d19e 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 5d342a771e..ad7d1eb0a8 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({
-- 
GitLab