From 099eabb17082a3d3a0a0d411dd591ba1ed40fcd7 Mon Sep 17 00:00:00 2001
From: Bill Erickson <berickxx@gmail.com>
Date: Tue, 26 Mar 2019 13:58:46 -0400
Subject: [PATCH] LP1821382 Conjoined linking repairs

Modify existing copy->record conjoined links where necessary instead of
create duplicates.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Dan Wells <dbw2@calvin.edu>
---
 .../holdings/conjoined-items-dialog.component.ts    | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts b/Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts
index 51000a77cd..69ff7e79bb 100644
--- a/Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts
+++ b/Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts
@@ -30,6 +30,7 @@ export class ConjoinedItemsDialogComponent
     numFailed: number;
     peerTypes: ComboboxEntry[];
     peerRecord: number;
+    existingMaps: any;
 
     onOpenSub: Subscription;
 
@@ -64,6 +65,8 @@ export class ConjoinedItemsDialogComponent
             if (this.peerTypes.length === 0) {
                 this.getPeerTypes();
             }
+
+            this.fetchExisting();
         });
     }
 
@@ -71,6 +74,13 @@ export class ConjoinedItemsDialogComponent
         this.onOpenSub.unsubscribe();
     }
 
+    fetchExisting() {
+        this.existingMaps = {};
+        this.pcrud.search('bpbcm',
+            {target_copy: this.copyIds, peer_record: this.peerRecord})
+        .subscribe(map => this.existingMaps[map.target_copy()] = map);
+    }
+
     getPeerTypes(): Promise<any> {
         return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
         .then(types =>
@@ -95,12 +105,19 @@ export class ConjoinedItemsDialogComponent
         }
 
         const id = this.ids.pop();
-        const map = this.idl.create('bpbcm');
+        const map = this.existingMaps[id] || this.idl.create('bpbcm');
         map.peer_record(this.peerRecord);
         map.target_copy(id);
         map.peer_type(this.peerType);
 
-        return this.pcrud.create(map).toPromise().then(
+        let promise: Promise<any>;
+        if (this.existingMaps[id]) {
+            promise = this.pcrud.update(map).toPromise();
+        } else {
+            promise = this.pcrud.create(map).toPromise();
+        }
+
+        return promise.then(
             ok => {
                 this.successMsg.current().then(msg => this.toast.success(msg));
                 this.numSucceeded++;
-- 
2.11.0