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 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
18 * Dialog for managing copy alerts.
22 selector: 'eg-copy-alerts-dialog',
23 templateUrl: 'copy-alerts-dialog.component.html'
26 export class CopyAlertsDialogComponent
27 extends DialogComponent implements OnInit {
30 @Input() set copyIds(ids: number[]) {
31 this._copyIds = [].concat(ids);
33 get copyIds(): number[] {
37 _mode: string; // create | manage
38 @Input() set mode(m: string) {
45 // In 'create' mode, we may be adding notes to multiple copies.
47 // In 'manage' mode we only handle a single copy.
49 alertTypes: ComboboxEntry[];
53 @ViewChild('successMsg') private successMsg: StringComponent;
54 @ViewChild('errorMsg') private errorMsg: StringComponent;
55 @ViewChild('confirmDeleteDialog')
56 private confirmDeleteDialog: ConfirmDialogComponent;
59 private modal: NgbModal, // required for passing to parent
60 private toast: ToastService,
61 private net: NetService,
62 private idl: IdlService,
63 private pcrud: PcrudService,
64 private org: OrgService,
65 private auth: AuthService) {
66 super(modal); // required for subclassing
74 * Fetch the item/record, then open the dialog.
75 * Dialog promise resolves with true/false indicating whether
76 * the mark-damanged action occured or was dismissed.
78 open(args: NgbModalOptions): Observable<boolean> {
81 this.newAlert = this.idl.create('aca');
82 this.newAlert.create_staff(this.auth.user().id());
84 if (this.copyIds.length === 0) {
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.
91 if (this.mode === 'manage') {
92 if (this.copyIds.length > 1) {
93 console.warn('Attempt to manage alerts for multiple copies.');
94 this.copyIds = [this.copyIds[0]];
98 // Observerify data loading
101 .then(_ => this.getCopies())
102 .then(_ => this.mode === 'manage' ? this.getCopyAlerts() : null)
105 // Return open() observable to caller
106 return obs.pipe(switchMap(_ => super.open(args)));
109 getAlertTypes(): Promise<any> {
110 if (this.alertTypes) {
111 return Promise.resolve();
113 return this.pcrud.retrieveAll('ccat',
115 scope_org: this.org.ancestors(this.auth.user().ws_ou(), true)
117 ).toPromise().then(alerts => {
118 this.alertTypes = alerts.map(a => ({id: a.id(), label: a.name()}));
122 getCopies(): Promise<any> {
123 return this.pcrud.search('acp', {id: this.copyIds}, {}, {atomic: true})
124 .toPromise().then(copies => {
125 this.copies = copies;
126 copies.forEach(c => c.copy_alerts([]));
127 if (this.mode === 'manage') {
128 this.copy = copies[0];
133 // Copy alerts for the selected copies which have not been
134 // acknowledged by staff and are within org unit range of
136 getCopyAlerts(): Promise<any> {
137 const copyIds = this.copies.map(c => c.id());
138 const typeIds = this.alertTypes.map(a => a.id);
140 return this.pcrud.search('aca',
141 {copy: copyIds, ack_time: null, alert_type: typeIds},
143 .toPromise().then(alerts => {
144 alerts.forEach(a => {
145 const copy = this.copies.filter(c => c.id() === a.copy())[0];
146 copy.copy_alerts().push(a);
151 // Add the in-progress new note to all copies.
153 if (!this.newAlert.alert_type()) { return; }
155 const alerts: IdlObject[] = [];
156 this.copies.forEach(c => {
157 const a = this.idl.clone(this.newAlert);
162 this.pcrud.create(alerts).toPromise().then(
164 this.successMsg.current().then(msg => this.toast.success(msg));
165 this.changesMade = true;
166 if (this.mode === 'create') {
167 // In create mode, we assume the user wants to create
168 // a single alert and be done with it.
169 this.close(this.changesMade);
171 // Otherwise, add the alert to the copy
172 this.copy.copy_alerts().push(newAlert);
176 this.errorMsg.current().then(msg => this.toast.danger(msg));
182 const alerts = this.copy.copy_alerts().filter(a => a.ischanged());
183 if (alerts.length === 0) { return; }
184 this.pcrud.update(alerts).toPromise().then(
185 ok => this.successMsg.current().then(msg => this.toast.success(msg)),
186 err => this.errorMsg.current().then(msg => this.toast.danger(msg))