1 import {Injectable} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {IdlObject, IdlService} from './idl.service';
4 import {NetService} from './net.service';
5 import {AuthService} from './auth.service';
6 import {PcrudService} from './pcrud.service';
8 type OrgNodeOrId = number | IdlObject;
11 canHaveUsers?: boolean;
12 canHaveVolumes?: boolean;
13 opacVisible?: boolean;
18 interface OrgSettingsBatch {
22 @Injectable({providedIn: 'root'})
23 export class OrgService {
25 private orgList: IdlObject[] = [];
26 private orgTree: IdlObject; // root node + children
27 private orgMap: {[id: number]: IdlObject} = {};
28 private settingsCache: OrgSettingsBatch = {};
31 private net: NetService,
32 private auth: AuthService,
33 private pcrud: PcrudService
36 get(nodeOrId: OrgNodeOrId): IdlObject {
37 if (typeof nodeOrId === 'object') {
40 return this.orgMap[nodeOrId];
48 * Returns a list of org units that match the selected criteria.
49 * All filters must match for an org to be included in the result set.
50 * Unset filter options are ignored.
52 filterList(filter: OrgFilter, asId?: boolean): any[] {
54 this.list().forEach(org => {
56 const chu = filter.canHaveUsers;
57 if (chu && !this.canHaveUsers(org)) { return; }
58 if (chu === false && this.canHaveUsers(org)) { return; }
60 const chv = filter.canHaveVolumes;
61 if (chv && !this.canHaveVolumes(org)) { return; }
62 if (chv === false && this.canHaveVolumes(org)) { return; }
64 const ov = filter.opacVisible;
65 if (ov && !this.opacVisible(org)) { return; }
66 if (ov === false && this.opacVisible(org)) { return; }
68 if (filter.inList && !filter.inList.includes(org.id())) {
72 if (filter.notInList && filter.notInList.includes(org.id())) {
76 // All filter tests passed. Add it to the list
77 list.push(asId ? org.id() : org);
89 return this.orgList[0];
92 // list of org_unit objects or IDs for ancestors + me
93 ancestors(nodeOrId: OrgNodeOrId, asId?: boolean): any[] {
94 let node = this.get(nodeOrId);
95 if (!node) { return []; }
97 while ( (node = this.get(node.parent_ou())) ) {
101 return nodes.map(n => n.id());
106 // tests that a node can have users
107 canHaveUsers(nodeOrId): boolean {
108 return this.get(nodeOrId).ou_type().can_have_users() === 't';
111 // tests that a node can have volumes
112 canHaveVolumes(nodeOrId): boolean {
116 .can_have_vols() === 't';
119 opacVisible(nodeOrId): boolean {
120 return this.get(nodeOrId).opac_visible() === 't';
123 // list of org_unit objects or IDs for me + descendants
124 descendants(nodeOrId: OrgNodeOrId, asId?: boolean): any[] {
125 const node = this.get(nodeOrId);
126 if (!node) { return []; }
128 const descend = (n) => {
130 n.children().forEach(descend);
134 return nodes.map(n => n.id());
139 // list of org_unit objects or IDs for ancestors + me + descendants
140 fullPath(nodeOrId: OrgNodeOrId, asId?: boolean): any[] {
141 const list = this.ancestors(nodeOrId, false).concat(
142 this.descendants(nodeOrId, false).slice(1));
144 return list.map(n => n.id());
149 sortTree(sortField?: string, node?: IdlObject): void {
150 if (!sortField) { sortField = 'shortname'; }
151 if (!node) { node = this.orgTree; }
153 node.children.sort((a, b) => {
154 return a[sortField]() < b[sortField]() ? -1 : 1;
157 node.children.forEach(n => this.sortTree(n));
160 absorbTree(node?: IdlObject): void {
166 this.orgMap[node.id()] = node;
167 this.orgList.push(node);
168 node.children().forEach(c => this.absorbTree(c));
172 * Grabs all of the org units from the server, chops them up into
173 * various shapes, then returns an "all done" promise.
175 fetchOrgs(): Promise<void> {
176 return this.pcrud.search('aou', {parent_ou : null},
177 {flesh : -1, flesh_fields : {aou : ['children', 'ou_type']}},
179 ).toPromise().then(tree => {
187 * Populate 'target' with settings from cache where available.
188 * Return the list of settings /not/ pulled from cache.
190 private settingsFromCache(names: string[], target: any) {
191 const cacheKeys = Object.keys(this.settingsCache);
193 cacheKeys.forEach(key => {
194 const matchIdx = names.indexOf(key);
196 target[key] = this.settingsCache[key];
197 names.splice(matchIdx, 1);
205 * Fetch org settings from the network.
206 * 'auth' is null for anonymous lookup.
208 private settingsFromNet(orgId: number,
209 names: string[], auth?: string): Promise<any> {
212 return new Promise((resolve, reject) => {
215 'open-ils.actor.ou_setting.ancestor_default.batch',
219 Object.keys(blob).forEach(key => {
220 const val = blob[key]; // null or hash
221 settings[key] = val ? val.value : null;
234 settings(name: string | string[],
235 orgId?: number, anonymous?: boolean): Promise<OrgSettingsBatch> {
237 let names = [].concat(name);
239 let auth: string = null;
240 let useCache = false;
242 if (this.auth.user()) {
244 useCache = Number(orgId) === Number(this.auth.user().ws_ou());
246 orgId = this.auth.user().ws_ou();
250 // avoid passing auth token when anonymous is requested.
252 auth = this.auth.token();
255 } else if (!anonymous) {
256 return Promise.reject(
257 'Use "anonymous" To retrieve org settings without an authtoken');
261 names = this.settingsFromCache(names, settings);
264 // All requested settings found in cache (or name list is empty)
265 if (names.length === 0) {
266 return Promise.resolve(settings);
269 return this.settingsFromNet(orgId, names, auth)
272 Object.keys(sets).forEach(key => {
273 this.settingsCache[key] = sets[key];