LP1888723 Improve copy default status lookup
authorBill Erickson <berickxx@gmail.com>
Tue, 15 Dec 2020 15:40:45 +0000 (07:40 -0800)
committerGalen Charlton <gmc@equinoxOLI.org>
Sun, 15 Aug 2021 23:55:49 +0000 (19:55 -0400)
Fixes an issue where a) default copy statuses were not getting correctly
applied and b) the copy status org setting lookup was not correctly
serialized, which can lead to actor drone exhaustion on the server (see
also bug 1896285).

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Ruth Frasur <rfrasur@library.in.gov>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts
Open-ILS/src/eg2/src/app/staff/cat/volcopy/volcopy.component.ts
Open-ILS/src/eg2/src/app/staff/cat/volcopy/volcopy.service.ts

index 7dddc63..12668bf 100644 (file)
@@ -142,6 +142,7 @@ export class VolEditComponent implements OnInit {
     }
 
     createCopies(volNode: HoldingsTreeNode, count: number) {
+        const copies = [];
         for (let i = 0; i < count; i++) {
 
             // Our context assumes copies are fleshed with volumes
@@ -149,7 +150,10 @@ export class VolEditComponent implements OnInit {
             const copy = this.volcopy.createStubCopy(vol);
             copy.call_number(vol);
             this.context.findOrCreateCopyNode(copy);
+            copies.push(copy);
         }
+
+        this.volcopy.setCopyStatus(copies);
     }
 
     createCopiesFromPopover(volNode: HoldingsTreeNode, popover: any) {
@@ -166,6 +170,7 @@ export class VolEditComponent implements OnInit {
 
     createVols(orgNode: HoldingsTreeNode, count: number) {
         const vols = [];
+        const copies = [];
         for (let i = 0; i < count; i++) {
 
             // This will vivify the volNode if needed.
@@ -177,9 +182,11 @@ export class VolEditComponent implements OnInit {
             // Our context assumes copies are fleshed with volumes
             const copy = this.volcopy.createStubCopy(vol);
             copy.call_number(vol);
+            copies.push(copy);
             this.context.findOrCreateCopyNode(copy);
         }
 
+        this.volcopy.setCopyStatus(copies);
         this.volcopy.setVolClassLabels(vols);
     }
 
@@ -200,14 +207,18 @@ export class VolEditComponent implements OnInit {
     addStubCopies(volNode?: HoldingsTreeNode) {
         const nodes = volNode ? [volNode] : this.context.volNodes();
 
+        const copies = [];
         nodes.forEach(vNode => {
             if (vNode.children.length === 0) {
                 const vol = vNode.target;
                 const copy = this.volcopy.createStubCopy(vol);
                 copy.call_number(vol);
+                copies.push(copy);
                 this.context.findOrCreateCopyNode(copy);
             }
         });
+
+        this.volcopy.setCopyStatus(copies);
     }
 
     applyVolValue(vol: IdlObject, key: string, value: any) {
index 9250cc2..0d8b797 100644 (file)
@@ -281,7 +281,7 @@ export class VolCopyComponent implements OnInit {
             copies.push(copy);
         });
 
-        return this.volcopy.setCopyStatus(copies, this.context.fastAdd);
+        return this.volcopy.setCopyStatus(copies);
     }
 
     fetchCopies(copyIds: number | number[]): Promise<any> {
index 85b16e4..1396a8d 100644 (file)
@@ -349,40 +349,39 @@ export class VolCopyService {
     }
 
     // Sets the default copy status for a batch of copies.
-    setCopyStatus(copies: IdlObject[], fastAdd: boolean): Promise<any> {
+    setCopyStatus(copies: IdlObject[]): Promise<any> {
+
+        const fastAdd = this.currentContext.fastAdd;
 
         const setting = fastAdd ?
             'cat.default_copy_status_fast' :
             'cat.default_copy_status_normal';
 
-        let promise = Promise.resolve(); // Seralize
-
-        copies.forEach(copy => {
-
-            // Avoid unnecessary lookups.  Copy may have been modified
-            // during a previous iteration of this loop.
-            if (!isNaN(copy.status())) { return; }
+        const orgs: any = {};
+        copies.forEach(copy => orgs[copy.circ_lib()] = 1);
 
-            promise = promise.then(_ =>
-                this.org.settings(setting, copy.circ_lib())
-
-            ).then(sets => {
-
-                // 0 == Available; 5 == In Process
-                const stat = sets[setting] || (fastAdd ? 0 : 5);
+        let promise = Promise.resolve(); // Seralize
 
-                copies.forEach(copy2 => {
-                    if (copy2.circ_lib() === copy.circ_lib()) {
-                        copy2.status(stat);
-                    }
+        // Pre-fetch needed org settings
+        Object.keys(orgs).forEach(org => {
+            promise = promise.then(_ => {
+                return this.org.settings(setting, +org)
+                .then(sets => {
+                    orgs[org] = sets[setting] || (fastAdd ? 0 : 5);
                 });
             });
         });
 
+        promise.then(_ => {
+            Object.keys(orgs).forEach(org => {
+                copies.filter(copy => copy.circ_lib() === +org)
+                .forEach(copy => copy.status(orgs[org]));
+            });
+        });
+
         return promise;
     }
 
-
     saveDefaults(): Promise<any> {
 
         // Scrub unnecessary content before storing.