]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/provider/provider-record.service.ts
4f46b512c9ca16c5da090fe92d943f3c006892a0
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / provider / provider-record.service.ts
1 import {Injectable} from '@angular/core';
2 import {Observable, from} from 'rxjs';
3 import {empty, throwError, Subject} from 'rxjs';
4 import {map, defaultIfEmpty} from 'rxjs/operators';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {NetService} from '@eg/core/net.service';
8 import {PermService} from '@eg/core/perm.service';
9
10 export class ProviderSummary {
11 }
12
13 export class ProviderRecord {
14     id: number;
15     record: IdlObject;
16     canDelete: boolean;
17     canAdmin: boolean;
18
19     constructor(record: IdlObject) {
20         this.id = Number(record.id());
21         this.record = record;
22         this.canDelete = false;
23         this.canAdmin = false;
24     }
25 }
26
27 @Injectable()
28 export class ProviderRecordService {
29
30     private currentProvider: ProviderRecord;
31     private currentProviderId: number = null;
32
33     private providerUpdatedSource = new Subject<number>();
34     providerUpdated$ = this.providerUpdatedSource.asObservable();
35
36     private permissions: any;
37     private viewOUs: number[] = [];
38
39     constructor(
40         private idl: IdlService,
41         private net: NetService,
42         private pcrud: PcrudService,
43         private perm: PermService
44     ) {
45         this.currentProvider = null;
46         this.loadPerms();
47     }
48
49     loadPerms(): Promise<any> {
50         if (this.permissions) {
51             return Promise.resolve();
52         }
53         return this.perm.hasWorkPermAt(['ADMIN_PROVIDER', 'MANAGE_PROVIDER', 'VIEW_PROVIDER'], true).then(permMap => {
54             this.permissions = permMap;
55             this.viewOUs.concat(permMap['VIEW_PROVIDER']);
56             this.permissions['ADMIN_PROVIDER'].forEach(ou => {
57                 if (!this.viewOUs.includes(ou)) {
58                     this.viewOUs.push(ou);
59                 }
60             });
61             this.permissions['MANAGE_PROVIDER'].forEach(ou => {
62                 if (!this.viewOUs.includes(ou)) {
63                     this.viewOUs.push(ou);
64                 }
65             });
66         });
67     }
68
69     getProviderRecord(id: number): Observable<ProviderRecord> {
70         console.debug('fetching provider ' + id);
71         this.currentProviderId = id;
72         const emptyGuard = this.idl.create('acqpro');
73         emptyGuard.id('no_provider_fetched');
74         return this.pcrud.search('acqpro', { id: id },
75             {
76                 flesh: 3,
77                 flesh_fields: { acqpro:   [
78                                             'attributes', 'holdings_subfields', 'contacts',
79                                             'addresses', 'provider_notes',
80                                             'edi_accounts', 'currency_type', 'edi_default'
81                                           ],
82                                 acqpa:    ['provider'],
83                                 acqpc:    ['provider', 'addresses'],
84                                 acqphsm:  ['provider'],
85                                 acqlipad: ['provider'],
86                                 acqedi:   ['attr_set', 'provider'],
87                               }
88             },
89             {}
90         ).pipe(defaultIfEmpty(emptyGuard), map(acqpro => {
91             if (acqpro.id() === 'no_provider_fetched') {
92                 throw new Error('no provider to fetch');
93             }
94             const provider = new ProviderRecord(acqpro);
95             // make a copy of holding_tag for use by the holdings definitions tab
96             acqpro['_holding_tag'] = acqpro.holding_tag();
97             acqpro.edi_accounts().forEach(acct => {
98                 acct['_is_default'] = false;
99                 if (acqpro.edi_default()) {
100                     if (acct.id() === acqpro.edi_default().id()) {
101                         acct['_is_default'] = true;
102                     }
103                 }
104             });
105             acqpro.contacts().forEach(acct => {
106                 acct['_is_primary'] = false;
107                 if (acqpro.primary_contact()) {
108                     if (acct.id() === acqpro.primary_contact()) {
109                         acct['_is_primary'] = true;
110                     }
111                 }
112             });
113             this.currentProvider = provider;
114             this.checkIfCanDelete(provider);
115             this.checkIfCanAdmin(provider);
116             return provider;
117         }));
118     }
119
120     checkIfCanDelete(prov: ProviderRecord) {
121         this.pcrud.search('acqpo', { provider: prov.id }, { limit: 1 }).toPromise()
122         .then(acqpo => {
123             if (!acqpo || acqpo.length === 0) {
124                 this.pcrud.search('jub', { provider: prov.id }, { limit: 1 }).toPromise()
125                 .then(jub => {
126                     if (!jub || jub.length === 0) {
127                         this.pcrud.search('acqinv', { provider: prov.id }, { limit: 1 }).toPromise()
128                         .then(acqinv => {
129                             prov.canDelete = true;
130                         });
131                     }
132                 });
133             }
134         });
135     }
136
137     checkIfCanAdmin(prov: ProviderRecord) {
138         this.loadPerms().then(x => {
139             if (Object.keys(this.permissions).length > 0 &&
140                 this.permissions['ADMIN_PROVIDER'].includes(prov.record.owner())) {
141                 prov.canAdmin = true;
142             }
143         });
144     }
145
146     checkIfCanAdminAtAll(): boolean {
147         if (typeof this.permissions === 'undefined') {
148             return false;
149         }
150         if (Object.keys(this.permissions).length > 0 &&
151             this.permissions['ADMIN_PROVIDER'].length > 0) {
152             return true;
153         } else {
154             return false;
155         }
156     }
157
158     getViewOUs(): number[] {
159         return this.viewOUs;
160     }
161
162     current(): IdlObject {
163         return this.currentProvider ? this.currentProvider.record : null;
164     }
165     currentProviderRecord(): ProviderRecord {
166         return this.currentProvider ? this.currentProvider : null;
167     }
168
169     fetch(id: number): Promise<any> {
170         return new Promise((resolve, reject) => {
171             this.getProviderRecord(id).subscribe(
172                 result => {
173                     resolve();
174                 },
175                 error => {
176                     reject();
177                 },
178             );
179         });
180     }
181
182     refreshCurrent(): Promise<any> {
183         if (this.currentProviderId) {
184             return this.fetch(this.currentProviderId);
185         } else {
186             return Promise.reject();
187         }
188     }
189
190     batchUpdate(list: IdlObject | IdlObject[]): Observable<any> {
191         return this.pcrud.autoApply(list);
192     }
193
194     announceProviderUpdated() {
195         this.providerUpdatedSource.next(this.currentProviderId);
196     }
197
198 }