]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/org-addr.component.ts
LP#1803790: apply alphabetization fix to Angular admin splash pages
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / org-addr.component.ts
1 import {Component, Input, Output, EventEmitter} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {OrgService} from '@eg/core/org.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
6
7 const ADDR_TYPES =
8     ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
9
10 @Component({
11     selector: 'eg-admin-org-address',
12     templateUrl: './org-addr.component.html'
13 })
14 export class OrgAddressComponent {
15
16     orgUnit: IdlObject = null;
17     private tabName: string;
18
19     private _orgId: number;
20
21     get orgId(): number { return this._orgId; }
22
23     @Input() set orgId(newId: number) {
24         if (newId) {
25             if (!this._orgId || this._orgId !== newId) {
26                 this._orgId = newId;
27                 this.init();
28             }
29         } else {
30             this._orgId = null;
31         }
32     }
33
34     @Output() addrChange: EventEmitter<IdlObject>;
35
36     constructor(
37         private idl: IdlService,
38         private org: OrgService,
39         private pcrud: PcrudService
40     ) {
41         this.addrChange = new EventEmitter<IdlObject>();
42         this.tabName = 'billing_address';
43     }
44
45     init() {
46         if (!this.orgId) { return; }
47
48         return this.pcrud.retrieve('aou', this.orgId,
49             {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
50             {authoritative: true}
51         ).subscribe(org => {
52             this.orgUnit = org;
53             ADDR_TYPES.forEach(aType => {
54                 if (!this.addr(aType)) {
55                     this.createAddress(aType);
56                 }
57             });
58         });
59     }
60
61     tabChanged($event: NgbTabChangeEvent) {
62         this.tabName = $event.nextId;
63     }
64
65     addrTypes(): string[] { // for UI
66         return ADDR_TYPES;
67     }
68
69     // Template shorthand -- get a specific address by type.
70     addr(addrType: string) {
71         return this.orgUnit ? this.orgUnit[addrType]() : null;
72     }
73
74     createAddress(addrType: string) {
75         const addr = this.idl.create('aoa');
76         addr.isnew(true);
77         addr.valid('t');
78         addr.org_unit(this.orgId);
79         this.orgUnit[addrType](addr);
80     }
81
82     cloneAddress(addrType: string) {
83
84         // Find the address
85         let fromAddr: IdlObject;
86         ADDR_TYPES.forEach(aType => {
87             if (aType !== addrType &&
88                 this.addr(aType).id() === this.addr(addrType).id()) {
89                 fromAddr = this.addr(aType);
90             }
91         });
92
93         const addr = this.idl.clone(fromAddr);
94         addr.id(null);
95         addr.isnew(true);
96         addr.valid('t');
97         this.orgUnit[addrType](addr);
98     }
99
100     // True if the provided address is used for more than one addr type.
101     sharedAddress(addrId: number): boolean {
102         return ADDR_TYPES.filter(aType => {
103             return (
104                 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
105             );
106         }).length > 1;
107     }
108
109     deleteAddress($event: any) {
110         const addr = $event.record;
111         const tmpOrg = this.updatableOrg();
112
113         // Set the FKey to NULL on the org unit for deleted addresses
114         ADDR_TYPES.forEach(aType => {
115             const a = this.addr(aType);
116             if (a && a.id() === addr.id()) {
117                 tmpOrg[aType](null);
118                 this.createAddress(aType);
119             }
120         });
121
122         this.pcrud.update(tmpOrg).toPromise()
123         .then(_ => this.pcrud.remove(addr).toPromise())
124         .then(_ => this.addrChange.emit(addr));
125     }
126
127     // Addr saved by fm-editor.
128     // In the case of new address creation, point the org unit at
129     // the new address ID.
130     addrSaved(addr: number | IdlObject) {
131
132         if (typeof addr !== 'object') {
133             // pcrud returns a number on 'update' calls.  No need to
134             // reload the data on a simple address change. it's changed
135             // in place.
136             return;
137         }
138
139         // update local copy with version that has an ID.
140         this.orgUnit[this.tabName](addr);
141
142         const org = this.updatableOrg();
143         org[this.tabName](addr.id());
144
145         // Creating a new address -- tell our org about it.
146         this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
147     }
148
149     // Create an unfleshed org unit object that's a clone of this.orgUnit
150     // to use when pushing updates to the server.
151     updatableOrg(): IdlObject {
152         const org = this.idl.clone(this.orgUnit);
153
154         ADDR_TYPES.forEach(aType => {
155             const addr = this.addr(aType);
156             if (addr) { org[aType](addr.id()); }
157         });
158
159         return org;
160     }
161
162 }
163