]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/picklist/upload.service.ts
LP#1929749: fix lint
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / picklist / upload.service.ts
1 import {Injectable} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {tap, map} from 'rxjs/operators';
4 import {HttpClient} from '@angular/common/http';
5 import {saveAs} from 'file-saver';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {PermService} from '@eg/core/perm.service';
12 import {EventService} from '@eg/core/event.service';
13 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
14 import {VandelayImportSelection} from '@eg/staff/cat/vandelay/vandelay.service';
15
16
17 @Injectable()
18 export class PicklistUploadService {
19
20     allQueues: {[qtype: string]: IdlObject[]};
21     attrDefs: {[atype: string]: IdlObject[]};
22     bibSources: IdlObject[];
23     matchSets: {[stype: string]: IdlObject[]};
24     importItemAttrDefs: IdlObject[];
25     mergeProfiles: IdlObject[];
26     providersList: IdlObject[];
27     fiscalYears: IdlObject[];
28     selectionLists: IdlObject[];
29     queueType: string;
30     recordType: string;
31
32
33     importSelection: VandelayImportSelection;
34
35     constructor(
36         private http: HttpClient,
37         private idl: IdlService,
38         private org: OrgService,
39         private evt: EventService,
40         private net: NetService,
41         private auth: AuthService,
42         private pcrud: PcrudService,
43         private perm: PermService
44     ) {
45         this.attrDefs = {};
46         this.allQueues = {};
47         this.matchSets = {};
48         this.importSelection = null;
49         this.queueType = 'acq';
50         this.recordType = 'bib';
51     }
52
53     getAttrDefs(dtype: string): Promise<IdlObject[]> {
54         if (this.attrDefs[dtype]) {
55             return Promise.resolve(this.attrDefs[dtype]);
56         }
57         const cls = (dtype === 'bib') ? 'vqbrad' : 'vqarad';
58         const orderBy = {};
59         orderBy[cls] = 'id';
60         return this.pcrud.retrieveAll(cls,
61             {order_by: orderBy}, {atomic: true}).toPromise()
62         .then(list => {
63             this.attrDefs[dtype] = list;
64             return list;
65         });
66     }
67
68     getMergeProfiles(): Promise<IdlObject[]> {
69         if (this.mergeProfiles) {
70             return Promise.resolve(this.mergeProfiles);
71         }
72
73         const owners = this.org.ancestors(this.auth.user().ws_ou(), true);
74         return this.pcrud.search('vmp',
75             {owner: owners}, {order_by: {vmp: ['name']}}, {atomic: true})
76         .toPromise().then(profiles => {
77             this.mergeProfiles = profiles;
78             return profiles;
79         });
80     }
81
82     getProvidersList(): Promise<IdlObject[]> {
83         if (this.providersList) {
84             return Promise.resolve(this.providersList);
85         }
86
87         const owners = this.org.ancestors(this.auth.user().ws_ou(), true);
88         return this.pcrud.search('acqpro',
89             {owner: owners}, {order_by: {acqpro: ['code']}}, {atomic: true})
90         .toPromise().then(providers => {
91             this.providersList = providers;
92             return providers;
93         });
94     }
95
96     getSelectionLists(): Promise<IdlObject[]> {
97         if (this.selectionLists) {
98             return Promise.resolve(this.selectionLists);
99         }
100
101         const owners = this.auth.user().id();
102         return this.pcrud.search('acqpl',
103             {owner: owners}, {order_by: {acqpl: ['name']}}, {atomic: true})
104         .toPromise().then(lists => {
105             this.selectionLists = lists;
106             return lists;
107         });
108     }
109
110     getAllQueues(qtype: string): Promise<IdlObject[]> {
111         if (this.allQueues[qtype]) {
112             return Promise.resolve(this.allQueues[qtype]);
113         } else {
114             this.allQueues[qtype] = [];
115         }
116
117         return this.net.request(
118             'open-ils.vandelay',
119             `open-ils.vandelay.bib_queue.owner.retrieve`,
120             this.auth.token()
121         ).pipe(tap(
122             queue => this.allQueues[qtype].push(queue)
123         )).toPromise().then(() => this.allQueues[qtype]);
124     }
125
126     getBibSources(): Promise<IdlObject[]> {
127         if (this.bibSources) {
128             return Promise.resolve(this.bibSources);
129         }
130
131         return this.pcrud.retrieveAll('cbs',
132           {order_by: {cbs: 'id'}},
133           {atomic: true}
134         ).toPromise().then(sources => {
135             this.bibSources = sources;
136             return sources;
137         });
138     }
139
140     getFiscalYears(): Promise<IdlObject[]> {
141         if (this.fiscalYears) {
142             return Promise.resolve(this.fiscalYears);
143         }
144
145         return this.pcrud.retrieveAll('acqfy',
146           {order_by: {acqfy: 'year'}},
147           {atomic: true}
148         ).toPromise().then(years => {
149             this.fiscalYears = years;
150             return years;
151         });
152     }
153
154     getItemImportDefs(): Promise<IdlObject[]> {
155         if (this.importItemAttrDefs) {
156             return Promise.resolve(this.importItemAttrDefs);
157         }
158
159         const owners = this.org.ancestors(this.auth.user().ws_ou(), true);
160         return this.pcrud.search('viiad', {owner: owners}, {}, {atomic: true})
161         .toPromise().then(defs => {
162             this.importItemAttrDefs = defs;
163             return defs;
164         });
165     }
166
167     getMatchSets(mtype: string): Promise<IdlObject[]> {
168
169         const mstype = 'biblio';
170
171         if (this.matchSets[mtype]) {
172             return Promise.resolve(this.matchSets[mtype]);
173         } else {
174             this.matchSets[mtype] = [];
175         }
176
177         const owners = this.org.ancestors(this.auth.user().ws_ou(), true);
178
179         return this.pcrud.search('vms',
180             {owner: owners, mtype: mstype}, {}, {atomic: true})
181         .toPromise().then(sets => {
182             this.matchSets[mtype] = sets;
183             return sets;
184         });
185     }
186
187
188     createQueue(
189         queueName: string,
190         queueType: string,
191         importDefId: number,
192         matchSet: number): Promise<number> {
193
194         const method = `open-ils.vandelay.bib_queue.create`;
195         queueType = 'acq';
196
197
198         return new Promise((resolve, reject) => {
199             this.net.request(
200                 'open-ils.vandelay', method,
201                 this.auth.token(), queueName, null, queueType,
202                 matchSet, importDefId
203             ).subscribe(queue => {
204                 const e = this.evt.parse(queue);
205                 if (e) {
206                     reject(e);
207                 } else {
208                     this.allQueues['bib'].push(queue);
209                     resolve(queue.id());
210                 }
211             });
212         });
213     }
214
215     createSelectionList(
216         picklistName: string,
217         picklistOrg: number
218     ): Promise<number> {
219
220         const newpicklist = this.idl.create('acqpl');
221         newpicklist.owner(this.auth.user().id());
222         newpicklist.name(picklistName);
223         newpicklist.org_unit(picklistOrg);
224
225         return new Promise((resolve, reject) => {
226             this.net.request(
227                 'open-ils.acq', 'open-ils.acq.picklist.create',
228                 this.auth.token(), newpicklist
229             ).subscribe((picklist) => {
230                 if (this.evt.parse(picklist)) {
231                     console.error(picklist);
232                 } else {
233                     console.log(picklist);
234                     resolve(picklist);
235                 }
236             });
237         });
238     }
239
240 }
241