LP#1839341: Fix boolean save (and other issues)
authorMike Rylander <mrylander@gmail.com>
Fri, 19 Aug 2022 17:28:52 +0000 (13:28 -0400)
committerJane Sandberg <sandbergja@gmail.com>
Thu, 13 Oct 2022 16:04:01 +0000 (09:04 -0700)
This commit makes sure that YAOUSen of type 'bool' are saved as JSON
booleans rather than one of the strings "true" or "false".  A database
cleanup script may be useful to make sure that erroneously set values
from earlier iterations of this interface don't stick around and cause
problems.  A simple version of this would be:

UPDATE  actor.org_unit_setting
  SET   value = BTRIM(value,'"')
  WHERE value IS NOT NULL
        AND name IN (
                SELECT  name
                  FROM  config.org_unit_setting_type
                  WHERE datatype ='bool'
        );

Also addressed in this commit are linked field label display (see the
Acquisitions "Default copy location" setting) and a minor low-level call
that requests too-deep object fleshing.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Terran McCanna <tmccanna@georgialibraries.org>
Signed-off-by: Jane Sandberg <sandbergja@gmail.com>
Open-ILS/src/eg2/src/app/staff/admin/local/org-unit-settings/org-unit-settings.component.ts

index 50a9d98..8601eba 100644 (file)
@@ -100,7 +100,7 @@ export class OrgUnitSettingsComponent implements OnInit {
         if (this.midFetch) { return EMPTY; }
         this.midFetch = true;
         return new Observable<any>(observer => {
-            this.pcrud.retrieveAll('coust', {flesh: 3, flesh_fields: {
+            this.pcrud.retrieveAll('coust', {flesh: 1, flesh_fields: {
                 'coust': ['grp', 'view_perm']
             }},
             { authoritative: true }).subscribe(
@@ -139,15 +139,17 @@ export class OrgUnitSettingsComponent implements OnInit {
                                 const settingsObj = this.settingTypeArr.filter(
                                     setting => setting.name === key.name
                                 )[0];
-                                settingsObj.value = key.setting.value;
-                                settingsObj.value_str = settingsObj.value;
-                                if (settingsObj.dataType === 'link' && (key.setting.value || key.setting.value === 0)) {
-                                    this.fetchLinkedField(settingsObj.fmClass, key.setting.value, settingsObj.value_str).then(res => {
-                                        settingsObj.value_str = res;
-                                    });
+                                if (settingsObj) {
+                                    settingsObj.value_str = key.setting.value;
+                                    settingsObj.value = this.parseValType(key.setting.value, settingsObj.dataType);
+                                    if (settingsObj.dataType === 'link' && (key.setting.value || key.setting.value === 0)) {
+                                        this.fetchLinkedField(settingsObj.fmClass, key.setting.value, settingsObj.value_str).then(res => {
+                                            settingsObj.value_str = res;
+                                        });
+                                    }
+                                    settingsObj._org_unit = this.org.get(key.setting.org);
+                                    settingsObj.context = settingsObj._org_unit.shortname();
                                 }
-                                settingsObj._org_unit = this.org.get(key.setting.org);
-                                settingsObj.context = settingsObj._org_unit.shortname();
                             } else {
                                 key.setting = null;
                             }
@@ -164,7 +166,7 @@ export class OrgUnitSettingsComponent implements OnInit {
         return new Promise((resolve, reject) => {
             return this.pcrud.retrieve(fmClass, id).subscribe(linkedField => {
                 const fname = this.idl.getClassSelector(fmClass) || this.idl.classes[fmClass].pkey || 'id';
-                val = this.idl.toHash(linkedField)[val];
+                val = this.idl.toHash(linkedField)[fname];
                 resolve(val);
             });
         });
@@ -264,6 +266,9 @@ export class OrgUnitSettingsComponent implements OnInit {
     }
 
     updateSetting(obj, entry, noToast?: boolean): Promise<any> {
+        Object.keys(obj.setting).forEach(
+            key => obj.setting[key] = this.parseValType(obj.setting[key], entry.dataType)
+        );
         return this.net.request(
             'open-ils.actor',
             'open-ils.actor.org_unit.settings.update',
@@ -365,14 +370,27 @@ export class OrgUnitSettingsComponent implements OnInit {
     }
 
     parseValType(value, dataType) {
-        if (value === null || value === undefined) { return null; }
-        if (dataType === 'integer' || 'currency' || 'link') {
-            return Number(value);
-        } else if (dataType === 'bool') {
-            return (value === 'true');
-        } else {
-            return value;
+        if (value === null || value === undefined)
+            return null;
+
+        const intTypes = ['integer', 'currency', 'float'];
+        if (intTypes.includes(dataType)) {
+            value = Number(value);
+        }
+
+        if (typeof value == 'string') {
+            value = value.replace(/^"(.*)"$/, '$1');
         }
+
+        if (typeof value == 'string' && dataType === 'bool') {
+            if (value.match(/^t/)) {
+                value = true;
+            } else {
+                value = false;
+            }
+        }
+
+        return value
     }
 
     filterCoust() {