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