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';
8 ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
11 selector: 'eg-admin-org-address',
12 templateUrl: './org-addr.component.html'
14 export class OrgAddressComponent {
16 orgUnit: IdlObject = null;
17 private tabName: string;
19 private _orgId: number;
21 get orgId(): number { return this._orgId; }
23 @Input() set orgId(newId: number) {
25 if (!this._orgId || this._orgId !== newId) {
34 @Output() addrChange: EventEmitter<IdlObject>;
37 private idl: IdlService,
38 private org: OrgService,
39 private pcrud: PcrudService
41 this.addrChange = new EventEmitter<IdlObject>();
42 this.tabName = 'billing_address';
46 if (!this.orgId) { return; }
48 return this.pcrud.retrieve('aou', this.orgId,
49 {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
53 ADDR_TYPES.forEach(aType => {
54 if (!this.addr(aType)) {
55 this.createAddress(aType);
61 tabChanged($event: NgbTabChangeEvent) {
62 this.tabName = $event.nextId;
65 addrTypes(): string[] { // for UI
69 // Template shorthand -- get a specific address by type.
70 addr(addrType: string) {
71 return this.orgUnit ? this.orgUnit[addrType]() : null;
74 createAddress(addrType: string) {
75 const addr = this.idl.create('aoa');
78 addr.org_unit(this.orgId);
79 this.orgUnit[addrType](addr);
82 cloneAddress(addrType: string) {
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);
93 const addr = this.idl.clone(fromAddr);
97 this.orgUnit[addrType](addr);
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 => {
104 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
109 deleteAddress($event: any) {
110 const addr = $event.record;
111 const tmpOrg = this.updatableOrg();
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()) {
118 this.createAddress(aType);
122 this.pcrud.update(tmpOrg).toPromise()
123 .then(_ => this.pcrud.remove(addr).toPromise())
124 .then(_ => this.addrChange.emit(addr));
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) {
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
139 // update local copy with version that has an ID.
140 this.orgUnit[this.tabName](addr);
142 const org = this.updatableOrg();
143 org[this.tabName](addr.id());
145 // Creating a new address -- tell our org about it.
146 this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
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);
154 ADDR_TYPES.forEach(aType => {
155 const addr = this.addr(aType);
156 if (addr) { org[aType](addr.id()); }