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';
9 export class ProviderSummary {
12 export class ProviderRecord {
18 constructor(record: IdlObject) {
19 this.id = Number(record.id());
21 this.canDelete = false;
22 this.canAdmin = false;
27 export class ProviderRecordService {
29 public currentProvider: ProviderRecord;
30 private currentProviderId: number = null;
32 private providerUpdatedSource = new Subject<number>();
33 providerUpdated$ = this.providerUpdatedSource.asObservable();
35 private permissions: any;
36 private viewOUs: number[] = [];
39 private idl: IdlService,
40 private pcrud: PcrudService,
41 private perm: PermService
43 this.currentProvider = null;
47 loadPerms(): Promise<any> {
48 if (this.permissions) {
49 return Promise.resolve();
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);
59 this.permissions['MANAGE_PROVIDER'].forEach(ou => {
60 if (!this.viewOUs.includes(ou)) {
61 this.viewOUs.push(ou);
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 },
75 flesh_fields: { acqpro: [
76 'attributes', 'holdings_subfields', 'contacts',
77 'addresses', 'provider_notes',
78 'edi_accounts', 'currency_type', 'edi_default'
81 acqpc: ['provider', 'addresses'],
82 acqphsm: ['provider'],
83 acqlipad: ['provider'],
84 acqedi: ['attr_set', 'provider'],
88 ).pipe(defaultIfEmpty(emptyGuard), map(acqpro => {
89 if (acqpro.id() === 'no_provider_fetched') {
90 throw new Error('no provider to fetch');
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;
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;
111 this.currentProvider = provider;
112 this.checkIfCanDelete(provider);
113 this.checkIfCanAdmin(provider);
118 checkIfCanDelete(prov: ProviderRecord) {
119 this.pcrud.search('acqpo', { provider: prov.id }, { limit: 1 }).toPromise()
121 if (!acqpo || acqpo.length === 0) {
122 this.pcrud.search('jub', { provider: prov.id }, { limit: 1 }).toPromise()
124 if (!jub || jub.length === 0) {
125 this.pcrud.search('acqinv', { provider: prov.id }, { limit: 1 }).toPromise()
127 prov.canDelete = true;
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;
144 checkIfCanAdminAtAll(): boolean {
145 if (typeof this.permissions === 'undefined') {
148 if (Object.keys(this.permissions).length > 0 &&
149 this.permissions['ADMIN_PROVIDER'].length > 0) {
156 getViewOUs(): number[] {
160 current(): IdlObject {
161 return this.currentProvider ? this.currentProvider.record : null;
163 currentProviderRecord(): ProviderRecord {
164 return this.currentProvider ? this.currentProvider : null;
167 fetch(id: number): Promise<any> {
168 return new Promise((resolve, reject) => {
169 this.getProviderRecord(id).subscribe(
180 refreshCurrent(): Promise<any> {
181 if (this.currentProviderId) {
182 return this.fetch(this.currentProviderId);
184 return Promise.reject();
188 batchUpdate(list: IdlObject | IdlObject[]): Observable<any> {
189 return this.pcrud.autoApply(list);
192 announceProviderUpdated() {
193 this.providerUpdatedSource.next(this.currentProviderId);