1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {EventService} from '@eg/core/event.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {DialogComponent} from '@eg/share/dialog/dialog.component';
11 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
12 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
13 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
16 * Dialog for managing copy alerts.
20 selector: 'eg-copy-alerts-dialog',
21 templateUrl: 'copy-alerts-dialog.component.html'
24 export class CopyAlertsDialogComponent
25 extends DialogComponent implements OnInit {
28 @Input() set copyIds(ids: number[]) {
29 this._copyIds = [].concat(ids);
31 get copyIds(): number[] {
35 _mode: string; // create | manage
36 @Input() set mode(m: string) {
43 // In 'create' mode, we may be adding notes to multiple copies.
45 // In 'manage' mode we only handle a single copy.
47 alertTypes: ComboboxEntry[];
51 @ViewChild('successMsg') private successMsg: StringComponent;
52 @ViewChild('errorMsg') private errorMsg: StringComponent;
53 @ViewChild('confirmDeleteDialog')
54 private confirmDeleteDialog: ConfirmDialogComponent;
57 private modal: NgbModal, // required for passing to parent
58 private toast: ToastService,
59 private net: NetService,
60 private idl: IdlService,
61 private pcrud: PcrudService,
62 private org: OrgService,
63 private auth: AuthService) {
64 super(modal); // required for subclassing
72 * Fetch the item/record, then open the dialog.
73 * Dialog promise resolves with true/false indicating whether
74 * the mark-damanged action occured or was dismissed.
76 async open(args: NgbModalOptions): Promise<boolean> {
79 this.newAlert = this.idl.create('aca');
80 this.newAlert.create_staff(this.auth.user().id());
82 if (this.copyIds.length === 0) {
83 return Promise.reject('copy ID required');
86 // In manage mode, we can only manage a single copy.
87 // But in create mode, we can add alerts to multiple copies.
89 if (this.mode === 'manage') {
90 if (this.copyIds.length > 1) {
91 console.warn('Attempt to manage alerts for multiple copies.');
92 this.copyIds = [this.copyIds[0]];
96 await this.getAlertTypes();
97 await this.getCopies();
98 if (this.mode === 'manage') {
99 await this.getCopyAlerts();
101 return super.open(args);
104 async getAlertTypes(): Promise<any> {
105 if (this.alertTypes) {
106 return Promise.resolve();
108 return this.pcrud.retrieveAll('ccat',
110 scope_org: this.org.ancestors(this.auth.user().ws_ou(), true)
112 ).toPromise().then(alerts => {
113 this.alertTypes = alerts.map(a => ({id: a.id(), label: a.name()}));
117 async getCopies(): Promise<any> {
118 return this.pcrud.search('acp', {id: this.copyIds}, {}, {atomic: true})
119 .toPromise().then(copies => {
120 this.copies = copies;
121 copies.forEach(c => c.copy_alerts([]));
122 if (this.mode === 'manage') {
123 this.copy = copies[0];
128 // Copy alerts for the selected copies which have not been
129 // acknowledged by staff and are within org unit range of
131 async getCopyAlerts(): Promise<any> {
132 const copyIds = this.copies.map(c => c.id());
133 const typeIds = this.alertTypes.map(a => a.id);
135 return this.pcrud.search('aca',
136 {copy: copyIds, ack_time: null, alert_type: typeIds},
138 .toPromise().then(alerts => {
139 alerts.forEach(a => {
140 const copy = this.copies.filter(c => c.id() === a.copy())[0];
141 copy.copy_alerts().push(a);
146 // Add the in-progress new note to all copies.
148 if (!this.newAlert.alert_type()) { return; }
150 const alerts: IdlObject[] = [];
151 this.copies.forEach(c => {
152 const a = this.idl.clone(this.newAlert);
157 this.pcrud.create(alerts).toPromise().then(
159 this.successMsg.current().then(msg => this.toast.success(msg));
160 this.changesMade = true;
161 if (this.mode === 'create') {
162 // In create mode, we assume the user wants to create
163 // a single alert and be done with it.
164 this.close(this.changesMade);
166 // Otherwise, add the alert to the copy
167 this.copy.copy_alerts().push(newAlert);
171 this.errorMsg.current().then(msg => this.toast.danger(msg))
177 const alerts = this.copy.copy_alerts().filter(a => a.ischanged());
178 if (alerts.length === 0) { return ;}
179 this.pcrud.update(alerts).toPromise().then(
180 ok => this.successMsg.current().then(msg => this.toast.success(msg)),
181 err => this.errorMsg.current().then(msg => this.toast.danger(msg))