LP1904036 Group display tree repairs
authorBill Erickson <berickxx@gmail.com>
Tue, 27 Apr 2021 20:24:40 +0000 (16:24 -0400)
committerGalen Charlton <gmc@equinoxOLI.org>
Fri, 28 Oct 2022 00:13:33 +0000 (20:13 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jane Sandberg <js7389@princeton.edu>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>

Open-ILS/src/eg2/src/app/staff/share/patron/profile-select.component.ts

index 6ee2c04..399da34 100644 (file)
@@ -57,6 +57,7 @@ export class ProfileSelectComponent implements ControlValueAccessor, OnInit {
 
     constructor(
         private org: OrgService,
+        private idl: IdlService,
         private auth: AuthService,
         private pcrud: PcrudService) {
         this.profileChange = new EventEmitter<IdlObject>();
@@ -64,9 +65,28 @@ export class ProfileSelectComponent implements ControlValueAccessor, OnInit {
 
     ngOnInit() {
         this.collectGroups().then(grps => this.sortGroups(grps))
+        .then(_ => this.fetchInitialGroup())
         .then(_ => this.cbox.selectedId = this.initialGroupId);
     }
 
+    // If the initial group is not included in our set of groups because
+    // we are using a custom display tree, fetch the group so we can
+    // add it to our tree.
+    fetchInitialGroup(): Promise<any> {
+        if (!this.initialGroupId || this.profiles[this.initialGroupId]) {
+            return Promise.resolve();
+        }
+
+        return this.pcrud.retrieve('pgt', this.initialGroupId).toPromise()
+        .then(grp => {
+            this.profiles[grp.id()] = grp;
+            grp.parent(null);
+            this.cboxEntries.push(
+                {id: grp.id(), label: this.grpLabel([], grp)});
+        });
+
+    }
+
     collectGroups(): Promise<IdlObject[]> {
 
         if (!this.useDisplayEntries) {
@@ -93,17 +113,21 @@ export class ProfileSelectComponent implements ControlValueAccessor, OnInit {
                     closestOrg = org;
                 }
             });
+
             groups = groups.filter(g => g.org() === closestOrg.id());
 
-            // Link the display entry to its pgt.
-            const pgtList = [];
+            // Translate the display entries into a 'pgt' tree
+
+            const tree: IdlObject[] = [];
+
             groups.forEach(display => {
-                const pgt = display.grp();
-                pgt._display = display;
-                pgtList.push(pgt);
+                const grp = display.grp();
+                const displayParent = groups.filter(g => g.id() === display.parent())[0];
+                grp.parent(displayParent ? displayParent.grp().id() : null);
+                tree.push(grp);
             });
 
-            return pgtList;
+            return tree;
         });
     }
 
@@ -116,7 +140,7 @@ export class ProfileSelectComponent implements ControlValueAccessor, OnInit {
         let depth = 0;
 
         do {
-            const pid = tmp._display ? tmp._display.parent() : tmp.parent();
+            const pid = tmp.parent();
             if (!pid) { break; } // top of the tree
 
             // Should always produce a value unless a perm group