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';
17 * Dialog for managing copy alerts.
20 export interface CopyAlertsChanges {
21 newAlerts: IdlObject[];
22 changedAlerts: IdlObject[];
26 selector: 'eg-copy-alerts-dialog',
27 templateUrl: 'copy-alerts-dialog.component.html'
30 export class CopyAlertsDialogComponent
31 extends DialogComponent implements OnInit {
33 // If there are multiple copyIds, only new alerts may be applied.
34 // If there is only one copyId, then alerts may be applied or removed.
35 @Input() copyIds: number[] = [];
37 mode: string; // create | manage
39 // If true, no attempt is made to save new alerts to the
40 // database. It's assumed this takes place in the calling code.
41 @Input() inPlaceCreateMode = false;
43 // In 'create' mode, we may be adding notes to multiple copies.
45 // In 'manage' mode we only handle a single copy.
48 alertTypes: ComboboxEntry[];
50 newAlerts: IdlObject[];
54 @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
55 @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
58 private modal: NgbModal, // required for passing to parent
59 private toast: ToastService,
60 private net: NetService,
61 private idl: IdlService,
62 private pcrud: PcrudService,
63 private org: OrgService,
64 private auth: AuthService) {
65 super(modal); // required for subclassing
73 * Fetch the item/record, then open the dialog.
74 * Dialog promise resolves with true/false indicating whether
75 * the mark-damanged action occured or was dismissed.
77 open(args: NgbModalOptions): Observable<CopyAlertsChanges> {
80 this.newAlert = this.idl.create('aca');
82 this.newAlert.create_staff(this.auth.user().id());
84 if (this.copyIds.length === 0 && !this.inPlaceCreateMode) {
85 return throwError('copy ID required');
88 // In manage mode, we can only manage a single copy.
89 // But in create mode, we can add alerts to multiple copies.
90 // We can only manage copies that already exist in the database.
91 if (this.copyIds.length === 1 && this.copyIds[0] > 0) {
97 // Observerify data loading
100 .then(_ => this.getCopies())
101 .then(_ => this.mode === 'manage' ? this.getCopyAlerts() : null)
104 // Return open() observable to caller
105 return obs.pipe(switchMap(_ => super.open(args)));
108 getAlertTypes(): Promise<any> {
109 if (this.alertTypes) { return Promise.resolve(); }
111 return this.pcrud.retrieveAll('ccat',
113 scope_org: this.org.ancestors(this.auth.user().ws_ou(), true)
115 ).toPromise().then(alerts => {
116 this.alertTypes = alerts.map(a => ({id: a.id(), label: a.name()}));
120 getCopies(): Promise<any> {
122 // Avoid fetch if we're only adding notes to isnew copies.
123 const ids = this.copyIds.filter(id => id > 0);
124 if (ids.length === 0) { return Promise.resolve(); }
126 return this.pcrud.search('acp', {id: this.copyIds}, {}, {atomic: true})
127 .toPromise().then(copies => {
128 this.copies = copies;
129 copies.forEach(c => c.copy_alerts([]));
130 if (this.mode === 'manage') {
131 this.copy = copies[0];
136 // Copy alerts for the selected copies which have not been
137 // acknowledged by staff and are within org unit range of
139 getCopyAlerts(): Promise<any> {
140 const typeIds = this.alertTypes.map(a => a.id);
142 return this.pcrud.search('aca',
143 {copy: this.copyIds, ack_time: null, alert_type: typeIds},
145 .toPromise().then(alerts => {
146 alerts.forEach(a => {
147 const copy = this.copies.filter(c => c.id() === a.copy())[0];
148 copy.copy_alerts().push(a);
153 getAlertTypeLabel(alert: IdlObject): string {
154 const alertType = this.alertTypes.filter(t => t.id === alert.alert_type());
155 return alertType[0].label;
158 removeAlert(alert: IdlObject) {
159 // the only type of alerts we can remove are pending ones that
160 // we have created during the lifetime of this modal; alerts
161 // that already exist can only be cleared
162 this.newAlerts = this.newAlerts.filter(t => t.id() !== alert.id());
165 // Add the in-progress new note to all copies.
167 if (!this.newAlert.alert_type()) { return; }
169 this.newAlert.id(this.autoId--);
170 this.newAlert.isnew(true);
171 this.newAlerts.push(this.newAlert);
173 this.newAlert = this.idl.create('aca');
179 const changedAlerts = this.copy ?
180 this.copy.copy_alerts().filter(a => a.ischanged()) :
182 if (this.inPlaceCreateMode) {
183 this.close({ newAlerts: this.newAlerts, changedAlerts: changedAlerts });
188 this.newAlerts.forEach(alert => {
189 this.copies.forEach(c => {
190 const a = this.idl.clone(alert);
197 if (this.mode === 'manage') {
198 changedAlerts.forEach(alert => {
202 this.pcrud.autoApply(alerts).toPromise().then(
204 this.successMsg.current().then(msg => this.toast.success(msg));
205 this.close({ newAlerts: this.newAlerts, changedAlerts: changedAlerts });
207 err => this.errorMsg.current().then(msg => this.toast.danger(msg))