]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/copy-alerts-dialog.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / copy-alerts-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError, from} from 'rxjs';
3 import {switchMap} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {IdlService, IdlObject} from '@eg/core/idl.service';
6 import {EventService} from '@eg/core/event.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {DialogComponent} from '@eg/share/dialog/dialog.component';
13 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
14 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
15
16 /**
17  * Dialog for managing copy alerts.
18  */
19
20 @Component({
21   selector: 'eg-copy-alerts-dialog',
22   templateUrl: 'copy-alerts-dialog.component.html'
23 })
24
25 export class CopyAlertsDialogComponent
26     extends DialogComponent implements OnInit {
27
28     _copyIds: number[];
29     @Input() set copyIds(ids: number[]) {
30         this._copyIds = [].concat(ids);
31     }
32     get copyIds(): number[] {
33         return this._copyIds;
34     }
35
36     _mode: string; // create | manage
37     @Input() set mode(m: string) {
38         this._mode = m;
39     }
40     get mode(): string {
41         return this._mode;
42     }
43
44     // In 'create' mode, we may be adding notes to multiple copies.
45     copies: IdlObject[];
46     // In 'manage' mode we only handle a single copy.
47     copy: IdlObject;
48     alertTypes: ComboboxEntry[];
49     newAlert: IdlObject;
50     changesMade: boolean;
51
52     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
53     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
54
55     constructor(
56         private modal: NgbModal, // required for passing to parent
57         private toast: ToastService,
58         private net: NetService,
59         private idl: IdlService,
60         private pcrud: PcrudService,
61         private org: OrgService,
62         private auth: AuthService) {
63         super(modal); // required for subclassing
64         this.copyIds = [];
65         this.copies = [];
66     }
67
68     ngOnInit() {}
69
70     /**
71      * Fetch the item/record, then open the dialog.
72      * Dialog promise resolves with true/false indicating whether
73      * the mark-damanged action occured or was dismissed.
74      */
75     open(args: NgbModalOptions): Observable<boolean> {
76         this.copy = null;
77         this.copies = [];
78         this.newAlert = this.idl.create('aca');
79         this.newAlert.create_staff(this.auth.user().id());
80
81         if (this.copyIds.length === 0) {
82             return throwError('copy ID required');
83         }
84
85         // In manage mode, we can only manage a single copy.
86         // But in create mode, we can add alerts to multiple copies.
87
88         if (this.mode === 'manage') {
89             if (this.copyIds.length > 1) {
90                 console.warn('Attempt to manage alerts for multiple copies.');
91                 this.copyIds = [this.copyIds[0]];
92             }
93         }
94
95         // Observerify data loading
96         const obs = from(
97             this.getAlertTypes()
98             .then(_ => this.getCopies())
99             .then(_ => this.mode === 'manage' ? this.getCopyAlerts() : null)
100         );
101
102         // Return open() observable to caller
103         return obs.pipe(switchMap(_ => super.open(args)));
104     }
105
106     getAlertTypes(): Promise<any> {
107         if (this.alertTypes) {
108             return Promise.resolve();
109         }
110         return this.pcrud.retrieveAll('ccat',
111         {   active: true,
112             scope_org: this.org.ancestors(this.auth.user().ws_ou(), true)
113         }, {atomic: true}
114         ).toPromise().then(alerts => {
115             this.alertTypes = alerts.map(a => ({id: a.id(), label: a.name()}));
116         });
117     }
118
119     getCopies(): Promise<any> {
120         return this.pcrud.search('acp', {id: this.copyIds}, {}, {atomic: true})
121         .toPromise().then(copies => {
122             this.copies = copies;
123             copies.forEach(c => c.copy_alerts([]));
124             if (this.mode === 'manage') {
125                 this.copy = copies[0];
126             }
127         });
128     }
129
130     // Copy alerts for the selected copies which have not been
131     // acknowledged by staff and are within org unit range of
132     // the alert type.
133     getCopyAlerts(): Promise<any> {
134         const copyIds = this.copies.map(c => c.id());
135         const typeIds = this.alertTypes.map(a => a.id);
136
137         return this.pcrud.search('aca',
138             {copy: copyIds, ack_time: null, alert_type: typeIds},
139             {}, {atomic: true})
140         .toPromise().then(alerts => {
141             alerts.forEach(a => {
142                 const copy = this.copies.filter(c => c.id() === a.copy())[0];
143                 copy.copy_alerts().push(a);
144             });
145         });
146     }
147
148     // Add the in-progress new note to all copies.
149     addNew() {
150         if (!this.newAlert.alert_type()) { return; }
151
152         const alerts: IdlObject[] = [];
153         this.copies.forEach(c => {
154             const a = this.idl.clone(this.newAlert);
155             a.copy(c.id());
156             alerts.push(a);
157         });
158
159         this.pcrud.create(alerts).toPromise().then(
160             newAlert => {
161                 this.successMsg.current().then(msg => this.toast.success(msg));
162                 this.changesMade = true;
163                 if (this.mode === 'create') {
164                     // In create mode, we assume the user wants to create
165                     // a single alert and be done with it.
166                     this.close(this.changesMade);
167                 } else {
168                     // Otherwise, add the alert to the copy
169                     this.copy.copy_alerts().push(newAlert);
170                 }
171             },
172             err => {
173                 this.errorMsg.current().then(msg => this.toast.danger(msg));
174             }
175         );
176     }
177
178     applyChanges() {
179         const alerts = this.copy.copy_alerts().filter(a => a.ischanged());
180         if (alerts.length === 0) { return; }
181         this.pcrud.update(alerts).toPromise().then(
182             ok => this.successMsg.current().then(msg => this.toast.success(msg)),
183             err => this.errorMsg.current().then(msg => this.toast.danger(msg))
184         );
185     }
186 }
187