LP1904036 Staged users
authorBill Erickson <berickxx@gmail.com>
Thu, 1 Apr 2021 20:10:23 +0000 (16:10 -0400)
committerGalen Charlton <gmc@equinoxOLI.org>
Fri, 28 Oct 2022 00:13:29 +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/circ/patron/edit.component.html
Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts

index 39ec61a..b37b546 100644 (file)
@@ -10,6 +10,8 @@
   i18n-text text="Group Link Succeeded"></eg-string>
 <eg-string key="circ.patron.edit.grplink.fail" 
   i18n-text text="Group Link Failed"></eg-string>
+<eg-string key="circ.patron.edit.default_addr_type"
+  i18n-text text="Mailing"></eg-string>
 
 <eg-patron-secondary-groups [secondaryGroups]="secondaryGroups" #secondaryGroupsDialog>
 </eg-patron-secondary-groups>
index 31635ab..722ed34 100644 (file)
@@ -155,6 +155,8 @@ export class EditComponent implements OnInit, AfterViewInit {
     dupeBarcode = false;
     dupeUsername = false;
     origUsername: string;
+    stageUser: IdlObject;
+    stageUserRequestor: IdlObject;
 
     fieldPatterns: {[cls: string]: {[field: string]: RegExp}} = {
         au: {},
@@ -217,6 +219,7 @@ export class EditComponent implements OnInit, AfterViewInit {
         .then(_ => this.setSurveys())
         .then(_ => this.loadPatron())
         .then(_ => this.getCloneUser())
+        .then(_ => this.getStageUser())
         .then(_ => this.getSecondaryGroups())
         .then(_ => this.applyPerms())
         .then(_ => this.setEditProfiles())
@@ -279,6 +282,119 @@ export class EditComponent implements OnInit, AfterViewInit {
         });
     }
 
+    getStageUser(): Promise<any> {
+        if (!this.stageUsername) { return Promise.resolve(); }
+
+        return this.net.request(
+            'open-ils.actor',
+            'open-ils.actor.user.stage.retrieve.by_username',
+            this.auth.token(), this.stageUsername).toPromise()
+
+        .then(suser => {
+            const evt = this.evt.parse(suser);
+            if (evt) {
+                alert(evt);
+                return Promise.reject(evt);
+            } else {
+                this.stageUser = suser;
+            }
+        })
+        .then(_ => {
+
+            const requestor = this.stageUser.user.requesting_usr();
+            if (!requestor) return;
+
+            return this.pcrud.retrieve('au', requestor).toPromise();
+
+        })
+        .then(reqr => this.stageUserRequestor = reqr)
+        .then(_ => this.copyStageData());
+    }
+
+    copyStageData() {
+        const cuser = this.stageUser;
+        const user = this.patron;
+
+        for (let key in this.idl.classes.stgu.field_map) {
+            const field = this.idl.classes.au.field_map[key];
+            if (field && !field.virtual) {
+                const value = cuser.user[key]();
+                if (value !== null) {
+                    user[key] = value;
+                }
+            }
+        };
+
+        // Clear the usrname if it looks like a UUID
+        if (user.usrname().replace(/-/g,'').match(/[0-9a-f]{32}/)) {
+            user.usrname('');
+        }
+
+        // Don't use stub address if we have one from the staged user.
+        if (cuser.mailing_addresses().length || cuser.billing_addresses().length) {
+            user.addresses([]);
+        }
+
+        const addrFromStage = (stageAddr: IdlObject) => {
+            if (!stageAddr) { return; }
+
+            const cls = stageAddr.classname;
+            const addr = this.idl.create('aua');
+
+            addr.isnew(true);
+            addr.id(this.autoId--);
+            addr.valid('t');
+
+            return this.strings.interpolate('circ.patron.edit.default_addr_type')
+            .then(msg => addr.address_type(msg));
+
+            if (cls === 'stgma') {
+                user.mailing_address(addr);
+            } else {
+                user.billing_address(addr);
+            }
+
+            user.addresses.push(addr);
+
+            for (let key in this.idl.classes[cls].field_map) {
+                const field = this.idl.classes.aua.field_map[key];
+                if (field && !field.virtual) {
+                    const value = stageAddr[key]();
+                    if (value !== null) {
+                        addr[key](value);
+                    }
+                }
+            }
+        }
+
+        addrFromStage(cuser.mailing_addresses[0]);
+        addrFromStage(cuser.billing_addresses[0]);
+
+        if (user.addresses().length == 1) {
+            // Only one address, use it for both purposes.
+            const addr = user.addresses[0];
+            user.mailing_address(addr);
+            user.billing_address(addr);
+        }
+
+        if (cuser.cards[0]) {
+            const card = this.idl.create('ac');
+            card.isnew(true);
+            card.id(this.autoId--);
+            card.barcode(cuser.cards[0].barcode());
+            user.card(card);
+            user.cards([card]);
+
+            if (!user.usrname()) {
+                user.usrname(card.barcode());
+            }
+        }
+
+        cuser.settings.forEach(setting => {
+            this.userSettings[setting.setting()] = Boolean(setting.value());
+        });
+    }
+
     copyCloneData(clone: IdlObject) {
         const patron = this.patron;