/**
* 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 {
// 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',
'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 === '') {
}
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 {