]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/workstation/workstations/workstations.component.ts
LP1825896 Store workstations in Hatch when available
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / workstation / workstations / workstations.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {StoreService} from '@eg/core/store.service';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {PermService} from '@eg/core/perm.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {EventService} from '@eg/core/event.service';
10 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
11
12 // Slim version of the WS that's stored in the cache.
13 interface Workstation {
14     id: number;
15     name: string;
16     owning_lib: number;
17 }
18
19 @Component({
20   templateUrl: 'workstations.component.html'
21 })
22 export class WorkstationsComponent implements OnInit {
23
24     selectedName: string;
25     workstations: Workstation[] = [];
26     removeWorkstation: string;
27     newOwner: IdlObject;
28     newName: string;
29     defaultName: string;
30
31     @ViewChild('workstationExistsDialog', { static: true })
32     private wsExistsDialog: ConfirmDialogComponent;
33
34     // Org selector options.
35     hideOrgs: number[];
36     disableOrgs: number[];
37     orgOnChange = (org: IdlObject): void => {
38         this.newOwner = org;
39     }
40
41     constructor(
42         private router: Router,
43         private route: ActivatedRoute,
44         private evt: EventService,
45         private net: NetService,
46         private store: StoreService,
47         private auth: AuthService,
48         private org: OrgService,
49         private perm: PermService
50     ) {}
51
52     ngOnInit() {
53         this.store.getWorkstations()
54
55         .then(wsList => {
56             this.workstations = wsList || [];
57             return this.store.getDefaultWorkstation();
58
59         }).then(def => {
60             this.defaultName = def;
61             this.selectedName = this.auth.workstation() || this.defaultName;
62             const rm = this.route.snapshot.paramMap.get('remove');
63             if (rm) { this.removeSelected(this.removeWorkstation = rm); }
64         });
65
66         // TODO: use the org selector limitPerm option
67         this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
68         .then(perms => {
69             // Disable org units that cannot have users and any
70             // that this user does not have work perms for.
71             this.disableOrgs =
72                 this.org.filterList({canHaveUsers : false}, true)
73                 .concat(this.org.filterList(
74                     {notInList : perms.REGISTER_WORKSTATION}, true));
75         });
76     }
77
78     selected(): Workstation {
79         return this.workstations.filter(
80           ws => ws.name === this.selectedName)[0];
81     }
82
83     useNow(): void {
84         if (this.selected()) {
85             this.router.navigate(['/staff/login'],
86                 {queryParams: {workstation: this.selected().name}});
87         }
88     }
89
90     setDefault(): void {
91       if (this.selected()) {
92             this.defaultName = this.selected().name;
93             this.store.setDefaultWorkstation(this.defaultName);
94         }
95     }
96
97     removeSelected(name?: string): void {
98         if (!name) {
99             name = this.selected().name;
100         }
101
102         this.workstations = this.workstations.filter(w => w.name !== name);
103         this.store.setWorkstations(this.workstations);
104
105         if (this.defaultName === name) {
106             this.defaultName = null;
107             this.store.removeWorkstations();
108         }
109     }
110
111     canDeleteSelected(): boolean {
112         return true;
113     }
114
115     registerWorkstation(): void {
116         console.log(`Registering new workstation ` +
117             `"${this.newName}" at ${this.newOwner.shortname()}`);
118
119         this.newName = this.newOwner.shortname() + '-' + this.newName;
120
121         this.registerWorkstationApi().then(
122             wsId => this.registerWorkstationLocal(wsId),
123             notOk => console.log('Workstation registration canceled/failed')
124         );
125     }
126
127     private handleCollision(): Promise<number> {
128         return new Promise((resolve, reject) => {
129             this.wsExistsDialog.open().subscribe(override => {
130                 if (override) {
131                     this.registerWorkstationApi(true).then(
132                         wsId => resolve(wsId),
133                         notOk => reject(notOk)
134                     );
135                 }
136             });
137         });
138     }
139
140
141     private registerWorkstationApi(override?: boolean): Promise<number> {
142         let method = 'open-ils.actor.workstation.register';
143         if (override) {
144             method += '.override';
145         }
146
147         return new Promise((resolve, reject) => {
148             this.net.request(
149                 'open-ils.actor', method,
150                 this.auth.token(), this.newName, this.newOwner.id()
151             ).subscribe(wsId => {
152                 const evt = this.evt.parse(wsId);
153                 if (evt) {
154                     if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
155                         this.handleCollision().then(
156                             id => resolve(id),
157                             notOk => reject(notOk)
158                         );
159                     } else {
160                         console.error(`Registration failed ${evt}`);
161                         reject();
162                     }
163                 } else {
164                    resolve(wsId);
165                 }
166             });
167         });
168     }
169
170     private registerWorkstationLocal(wsId: number) {
171         const ws: Workstation = {
172             id: wsId,
173             name: this.newName,
174             owning_lib: this.newOwner.id()
175         };
176
177         this.workstations.push(ws);
178         this.store.setWorkstations(this.workstations);
179         this.newName = '';
180         // when registering our first workstation, mark it as the
181         // default and show it as selected in the ws selector.
182         if (this.workstations.length === 1) {
183             this.selectedName = ws.name;
184             this.setDefault();
185         }
186     }
187 }
188
189