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.
21 selector: 'eg-copy-alerts-dialog',
22 templateUrl: 'copy-alerts-dialog.component.html'
25 export class CopyAlertsDialogComponent
26 extends DialogComponent implements OnInit {
29 @Input() set copyIds(ids: number[]) {
30 this._copyIds = [].concat(ids);
32 get copyIds(): number[] {
36 _mode: string; // create | manage
37 @Input() set mode(m: string) {
44 // In 'create' mode, we may be adding notes to multiple copies.
46 // In 'manage' mode we only handle a single copy.
48 alertTypes: ComboboxEntry[];
52 @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
53 @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
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
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.
75 open(args: NgbModalOptions): Observable<boolean> {
78 this.newAlert = this.idl.create('aca');
79 this.newAlert.create_staff(this.auth.user().id());
81 if (this.copyIds.length === 0) {
82 return throwError('copy ID required');
85 // In manage mode, we can only manage a single copy.
86 // But in create mode, we can add alerts to multiple copies.
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]];
95 // Observerify data loading
98 .then(_ => this.getCopies())
99 .then(_ => this.mode === 'manage' ? this.getCopyAlerts() : null)
102 // Return open() observable to caller
103 return obs.pipe(switchMap(_ => super.open(args)));
106 getAlertTypes(): Promise<any> {
107 if (this.alertTypes) {
108 return Promise.resolve();
110 return this.pcrud.retrieveAll('ccat',
112 scope_org: this.org.ancestors(this.auth.user().ws_ou(), true)
114 ).toPromise().then(alerts => {
115 this.alertTypes = alerts.map(a => ({id: a.id(), label: a.name()}));
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];
130 // Copy alerts for the selected copies which have not been
131 // acknowledged by staff and are within org unit range of
133 getCopyAlerts(): Promise<any> {
134 const copyIds = this.copies.map(c => c.id());
135 const typeIds = this.alertTypes.map(a => a.id);
137 return this.pcrud.search('aca',
138 {copy: copyIds, ack_time: null, alert_type: typeIds},
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);
148 // Add the in-progress new note to all copies.
150 if (!this.newAlert.alert_type()) { return; }
152 const alerts: IdlObject[] = [];
153 this.copies.forEach(c => {
154 const a = this.idl.clone(this.newAlert);
159 this.pcrud.create(alerts).toPromise().then(
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);
168 // Otherwise, add the alert to the copy
169 this.copy.copy_alerts().push(newAlert);
173 this.errorMsg.current().then(msg => this.toast.danger(msg));
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))