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