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';
10 export class ProviderSummary {
13 export class ProviderRecord {
19 constructor(record: IdlObject) {
20 this.id = Number(record.id());
22 this.canDelete = false;
23 this.canAdmin = false;
28 export class ProviderRecordService {
30 private currentProvider: ProviderRecord;
31 private currentProviderId: number = null;
33 private providerUpdatedSource = new Subject<number>();
34 providerUpdated$ = this.providerUpdatedSource.asObservable();
36 private permissions: any;
37 private viewOUs: number[] = [];
40 private idl: IdlService,
41 private net: NetService,
42 private pcrud: PcrudService,
43 private perm: PermService
45 this.currentProvider = null;
49 loadPerms(): Promise<any> {
50 if (this.permissions) {
51 return Promise.resolve();
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);
61 this.permissions['MANAGE_PROVIDER'].forEach(ou => {
62 if (!this.viewOUs.includes(ou)) {
63 this.viewOUs.push(ou);
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 },
77 flesh_fields: { acqpro: [
78 'attributes', 'holdings_subfields', 'contacts',
79 'addresses', 'provider_notes',
80 'edi_accounts', 'currency_type', 'edi_default'
83 acqpc: ['provider', 'addresses'],
84 acqphsm: ['provider'],
85 acqlipad: ['provider'],
86 acqedi: ['attr_set', 'provider'],
90 ).pipe(defaultIfEmpty(emptyGuard), map(acqpro => {
91 if (acqpro.id() === 'no_provider_fetched') {
92 throw new Error('no provider to fetch');
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;
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;
113 this.currentProvider = provider;
114 this.checkIfCanDelete(provider);
115 this.checkIfCanAdmin(provider);
120 checkIfCanDelete(prov: ProviderRecord) {
121 this.pcrud.search('acqpo', { provider: prov.id }, { limit: 1 }).toPromise()
123 if (!acqpo || acqpo.length === 0) {
124 this.pcrud.search('jub', { provider: prov.id }, { limit: 1 }).toPromise()
126 if (!jub || jub.length === 0) {
127 this.pcrud.search('acqinv', { provider: prov.id }, { limit: 1 }).toPromise()
129 prov.canDelete = true;
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;
146 checkIfCanAdminAtAll(): boolean {
147 if (typeof this.permissions === 'undefined') {
150 if (Object.keys(this.permissions).length > 0 &&
151 this.permissions['ADMIN_PROVIDER'].length > 0) {
158 getViewOUs(): number[] {
162 current(): IdlObject {
163 return this.currentProvider ? this.currentProvider.record : null;
165 currentProviderRecord(): ProviderRecord {
166 return this.currentProvider ? this.currentProvider : null;
169 fetch(id: number): Promise<any> {
170 return new Promise((resolve, reject) => {
171 this.getProviderRecord(id).subscribe(
182 refreshCurrent(): Promise<any> {
183 if (this.currentProviderId) {
184 return this.fetch(this.currentProviderId);
186 return Promise.reject();
190 batchUpdate(list: IdlObject | IdlObject[]): Observable<any> {
191 return this.pcrud.autoApply(list);
194 announceProviderUpdated() {
195 this.providerUpdatedSource.next(this.currentProviderId);