LP#1775466 Support server-stored settings
authorBill Erickson <berickxx@gmail.com>
Fri, 3 Aug 2018 17:38:46 +0000 (13:38 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 3 Aug 2018 17:38:46 +0000 (13:38 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/core/store.service.ts

index cce5a46..43ffa6f 100644 (file)
@@ -1,8 +1,18 @@
 /**
  * Store and retrieve data from various sources.
  */
-import {Injectable} from '@angular/core';
+import {Injectable, Injector} from '@angular/core';
 import {CookieService} from 'ngx-cookie';
+import {NetService} from './net.service';
+
+// Settings summary objects returned by the API
+export interface ServerSettingSummary {
+    name: string;
+    value: string;
+    has_org_setting: boolean;
+    has_user_setting: boolean;
+    has_workstation_setting: boolean;
+}
 
 @Injectable({providedIn: 'root'})
 export class StoreService {
@@ -13,6 +23,11 @@ export class StoreService {
     // Note cookies shared with /eg/staff must be stored at "/"
     loginSessionBasePath = '/';
 
+    // Loaded on demand to avoid circular ref in compiler.
+    auth: any;
+
+    cache: {[key: string]: ServerSettingSummary};
+
     // Set of keys whose values should disappear at logout.
     loginSessionKeys: string[] = [
         'eg.auth.token',
@@ -21,7 +36,12 @@ export class StoreService {
         'eg.auth.time.oc'
     ];
 
-    constructor(private cookieService: CookieService) {}
+    constructor(
+        private injector: Injector,
+        private cookieService: CookieService,
+        private net: NetService) {
+        this.cache = {};
+    }
 
     private parseJson(valJson: string): any {
         if (valJson === undefined || valJson === null || valJson === '') {
@@ -85,7 +105,29 @@ export class StoreService {
     }
 
     getServerItem(key: string): Promise<any> {
-        return Promise.resolve();
+
+        if (this.cache[key]) {
+            return Promise.resolve(this.cache[key].value);
+        }
+
+        if (!this.auth) {
+            this.auth = this.injector.get('AuthService');
+        }
+
+        if (this.auth.token()) {
+            return Promise.reject('auth token required for server settings');
+        }
+
+        return this.net.request(
+            'open-ils.actor',
+            'open-ils.actor.settings.retrieve',
+            [key], this.auth.token()
+        ).toPromise().then(
+            (summary: ServerSettingSummary) => {
+                this.cache[summary.name] = summary;
+                return summary.value;
+            }
+        );
     }
 
     getSessionItem(key: string): any {