]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/recent-imports.component.ts
LP1850473: manual and automated eslint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / recent-imports.component.ts
1 import {Component, OnInit} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {AuthService} from '@eg/core/auth.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {VandelayService} from './vandelay.service';
6
7 @Component({
8     templateUrl: 'recent-imports.component.html'
9 })
10
11 export class RecentImportsComponent implements OnInit {
12
13     trackers: IdlObject[];
14     // eslint-disable-next-line no-magic-numbers
15     refreshInterval = 2000; // ms
16     sinceDate: string;
17     pollTimeout: any;
18
19     constructor(
20         private idl: IdlService,
21         private auth: AuthService,
22         private pcrud: PcrudService,
23         private vandelay: VandelayService
24     ) {
25         this.trackers = [];
26     }
27
28     ngOnInit() {
29         // Default to showing all trackers created today.
30         const d = new Date();
31         d.setHours(0);
32         d.setMinutes(0);
33         d.setSeconds(0);
34         this.sinceDate = d.toISOString();
35
36         this.pollTrackers();
37     }
38
39     dateFilterChange(iso: string) {
40         if (iso) {
41             this.sinceDate = iso;
42             if (this.pollTimeout) {
43                 clearTimeout(this.pollTimeout);
44                 this.pollTimeout = null;
45             }
46             this.trackers = [];
47             this.pollTrackers();
48         }
49     }
50
51     pollTrackers() {
52
53         // Report on recent trackers for this workstation and for the
54         // logged in user.  Always show active trackers regardless
55         // of sinceDate.
56         const query: any = {
57             '-and': [
58                 {
59                     '-or': [
60                         {workstation: this.auth.user().wsid()},
61                         {usr: this.auth.user().id()}
62                     ],
63                 }, {
64                     '-or': [
65                         {create_time: {'>=': this.sinceDate}},
66                         {state: 'active'}
67                     ]
68                 }
69             ]
70         };
71
72         this.pcrud.search('vst', query, {order_by: {vst: 'create_time'}})
73             .subscribe(
74                 tracker => {
75                 // The screen flickers less if the tracker array is
76                 // updated inline instead of rebuilt every time.
77
78                     const existing =
79                     this.trackers.filter(t => t.id() === tracker.id())[0];
80
81                     if (existing) {
82                         existing.update_time(tracker.update_time());
83                         existing.state(tracker.state());
84                         existing.total_actions(tracker.total_actions());
85                         existing.actions_performed(tracker.actions_performed());
86                     } else {
87
88                         // Only show the import tracker when both an enqueue
89                         // and import tracker exist for a given session.
90                         const sameSes = this.trackers.filter(
91                             t => t.session_key() === tracker.session_key())[0];
92
93                         if (sameSes) {
94                             if (sameSes.action_type() === 'enqueue') {
95                             // Remove the enqueueu tracker
96
97                                 for (let idx = 0; idx < this.trackers.length; idx++) {
98                                     const trkr = this.trackers[idx];
99                                     if (trkr.id() === sameSes.id()) {
100                                         console.debug(
101                                             `removing tracker ${trkr.id()} from the list`);
102                                         this.trackers.splice(idx, 1);
103                                         break;
104                                     }
105                                 }
106                             } else if (sameSes.action_type() === 'import') {
107                             // Avoid adding the new enqueue tracker
108                                 return;
109                             }
110                         }
111
112                         console.debug(`adding tracker ${tracker.id()} to list`);
113
114                         this.trackers.unshift(tracker);
115                         this.fleshTrackerQueue(tracker);
116                     }
117                 },
118                 (err: unknown) => {},
119                 ()  => {
120                     const active =
121                     this.trackers.filter(t => t.state() === 'active');
122
123                     // Continue updating the display with updated tracker
124                     // data as long as we have any active trackers.
125                     if (active.length > 0) {
126                         this.pollTimeout = setTimeout(
127                             () => this.pollTrackers(), this.refreshInterval);
128                     } else {
129                         this.pollTimeout = null;
130                     }
131                 }
132             );
133     }
134
135     fleshTrackerQueue(tracker: IdlObject) {
136         const qClass = tracker.record_type() === 'bib' ? 'vbq' : 'vaq';
137         this.pcrud.retrieve(qClass, tracker.queue())
138             .subscribe(queue => tracker.queue(queue));
139     }
140
141 }