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';
18 export class PicklistUploadService {
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[];
33 importSelection: VandelayImportSelection;
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
48 this.importSelection = null;
49 this.queueType = 'acq';
50 this.recordType = 'bib';
53 getAttrDefs(dtype: string): Promise<IdlObject[]> {
54 if (this.attrDefs[dtype]) {
55 return Promise.resolve(this.attrDefs[dtype]);
57 const cls = (dtype === 'bib') ? 'vqbrad' : 'vqarad';
60 return this.pcrud.retrieveAll(cls,
61 {order_by: orderBy}, {atomic: true}).toPromise()
63 this.attrDefs[dtype] = list;
68 getMergeProfiles(): Promise<IdlObject[]> {
69 if (this.mergeProfiles) {
70 return Promise.resolve(this.mergeProfiles);
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;
82 getProvidersList(): Promise<IdlObject[]> {
83 if (this.providersList) {
84 return Promise.resolve(this.providersList);
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;
96 getSelectionLists(): Promise<IdlObject[]> {
97 if (this.selectionLists) {
98 return Promise.resolve(this.selectionLists);
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;
110 getAllQueues(qtype: string): Promise<IdlObject[]> {
111 if (this.allQueues[qtype]) {
112 return Promise.resolve(this.allQueues[qtype]);
114 this.allQueues[qtype] = [];
117 return this.net.request(
119 `open-ils.vandelay.bib_queue.owner.retrieve`,
122 queue => this.allQueues[qtype].push(queue)
123 )).toPromise().then(() => this.allQueues[qtype]);
126 getBibSources(): Promise<IdlObject[]> {
127 if (this.bibSources) {
128 return Promise.resolve(this.bibSources);
131 return this.pcrud.retrieveAll('cbs',
132 {order_by: {cbs: 'id'}},
134 ).toPromise().then(sources => {
135 this.bibSources = sources;
140 getFiscalYears(): Promise<IdlObject[]> {
141 if (this.fiscalYears) {
142 return Promise.resolve(this.fiscalYears);
145 return this.pcrud.retrieveAll('acqfy',
146 {order_by: {acqfy: 'year'}},
148 ).toPromise().then(years => {
149 this.fiscalYears = years;
154 getItemImportDefs(): Promise<IdlObject[]> {
155 if (this.importItemAttrDefs) {
156 return Promise.resolve(this.importItemAttrDefs);
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;
167 getMatchSets(mtype: string): Promise<IdlObject[]> {
169 const mstype = 'biblio';
171 if (this.matchSets[mtype]) {
172 return Promise.resolve(this.matchSets[mtype]);
174 this.matchSets[mtype] = [];
177 const owners = this.org.ancestors(this.auth.user().ws_ou(), true);
179 return this.pcrud.search('vms',
180 {owner: owners, mtype: mstype}, {}, {atomic: true})
181 .toPromise().then(sets => {
182 this.matchSets[mtype] = sets;
192 matchSet: number): Promise<number> {
194 const method = `open-ils.vandelay.bib_queue.create`;
198 return new Promise((resolve, reject) => {
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);
208 this.allQueues['bib'].push(queue);
216 picklistName: string,
220 const newpicklist = this.idl.create('acqpl');
221 newpicklist.owner(this.auth.user().id());
222 newpicklist.name(picklistName);
223 newpicklist.org_unit(picklistOrg);
225 return new Promise((resolve, reject) => {
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);
233 console.log(picklist);