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