]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/triggers/trigger-edit.component.ts
LP#1855780: improve reporting of event test errors
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / triggers / trigger-edit.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
14 import {Router} from '@angular/router';
15
16 @Component({
17     templateUrl: './trigger-edit.component.html'
18 })
19
20 export class EditEventDefinitionComponent implements OnInit {
21
22     evtDefId: number;
23     evtDefName: String;
24
25     testErr1: String = '';
26     testErr2: String = '';
27     testResult: String = '';
28     testDone: Boolean = false;
29
30     envDataSource: GridDataSource = new GridDataSource();
31     paramDataSource: GridDataSource = new GridDataSource();
32
33     editTab: 'def' | 'env' | 'param' | 'test' = 'def';
34
35     @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
36     @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
37
38     @ViewChild('envGrid') envGrid: GridComponent;
39     @ViewChild('paramGrid') paramGrid: GridComponent;
40
41     @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
42     @ViewChild('updateFailedString') updateFailedString: StringComponent;
43     @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
44     @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
45     @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
46     @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
47     @ViewChild('createSuccessString') createSuccessString: StringComponent;
48     @ViewChild('createErrString') createErrString: StringComponent;
49     @ViewChild('eventDuringTestString') eventDuringTestString: StringComponent;
50     @ViewChild('errorDuringTestString') errorDuringTestString: StringComponent;
51
52     constructor(
53         private router: Router,
54         private idl: IdlService,
55         private pcrud: PcrudService,
56         private toast: ToastService,
57         private route: ActivatedRoute,
58         private net: NetService,
59         private auth: AuthService,
60     ) {
61     }
62
63     ngOnInit() {
64         this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
65
66         // get current event def name to display on the banner
67         this.pcrud.search('atevdef',
68             {id: this.evtDefId}, {}).toPromise().then(rec => {
69             this.evtDefName = rec.name();
70         });
71
72         this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
73             return this.pcrud.search('atenv',
74                 {event_def: this.evtDefId}, {});
75         };
76
77         this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
78             return this.pcrud.search('atevparam',
79                 {event_def: this.evtDefId}, {});
80         };
81     }
82
83     onTabChange(event: NgbNavChangeEvent) {
84         this.editTab = event.nextId;
85     }
86
87     createNewEnv = () => {
88         this.createNewThing(this.envDialog, this.envGrid, 'atenv');
89     }
90
91     createNewParam = () => {
92         this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
93     }
94
95     createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
96         currentDialog.mode = 'create';
97         currentDialog.recordId = null;
98         const newRecord = this.idl.create(idl);
99         newRecord.event_def(this.evtDefId);
100         currentDialog.mode = 'create';
101         currentDialog.record = newRecord;
102         currentDialog.open({size: 'lg'}).subscribe(
103             ok => {
104                 this.createSuccessString.current()
105                     .then(str => this.toast.success(str));
106                 currentGrid.reload();
107             },
108             rejection => {
109                 if (!rejection.dismissed) {
110                     this.createErrString.current()
111                         .then(str => this.toast.danger(str));
112                 }
113             }
114         );
115     }
116
117     deleteSelected = (idlThings: IdlObject[]) => {
118         let currentGrid;
119         if (idlThings[0].classname === 'atenv') {
120             currentGrid = this.envGrid;
121         } else {
122             currentGrid = this.paramGrid;
123         }
124         idlThings.forEach(idlThing => idlThing.isdeleted(true));
125         let _deleted = 0;
126         this.pcrud.autoApply(idlThings).subscribe(
127             val => {
128                 console.debug('deleted: ' + val);
129                 this.deleteSuccessString.current()
130                     .then(str => this.toast.success(str));
131                 _deleted++;
132             },
133             err => {
134                 this.deleteFailedString.current()
135                     .then(str => this.toast.danger(str));
136             },
137             () => {
138                 if (_deleted > 0) {
139                     currentGrid.reload();
140                 }
141             }
142         );
143     }
144
145     editSelected = (selectedRecords: IdlObject[]) => {
146         const editOneThing = (record: IdlObject) => {
147             if (!record) { return; }
148             this.showEditDialog(record).then(
149                 () => editOneThing(selectedRecords.shift()));
150         };
151         editOneThing(selectedRecords.shift());
152     }
153
154     showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
155         let currentDialog;
156         let currentGrid;
157         if (selectedRecord.classname === 'atenv') {
158             currentDialog = this.envDialog;
159             currentGrid = this.envGrid;
160         } else {
161             currentDialog = this.paramDialog;
162             currentGrid = this.paramGrid;
163         }
164         currentDialog.mode = 'update';
165         const clone = this.idl.clone(selectedRecord);
166         currentDialog.record = clone;
167         return new Promise((resolve, reject) => {
168             currentDialog.open({size: 'lg'}).subscribe(
169                 result => {
170                     this.updateSuccessString.current()
171                         .then(str => this.toast.success(str));
172                     currentGrid.reload();
173                     resolve(result);
174                 },
175                 error => {
176                     this.updateFailedString.current()
177                         .then(str => this.toast.danger(str));
178                     reject(error);
179                }
180            );
181        });
182     }
183
184     runTest = (barcode) => {
185         if (!barcode) {
186             return;
187         }
188         this.clearTestResults();
189         this.net.request(
190             'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
191             this.auth.token(), this.evtDefId, barcode
192         ).subscribe(res => {
193             this.testDone = true;
194             if (res.ilsevent) {
195                 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
196                     .then(str => this.testErr1 = str);
197                 this.testErr2 = res.desc;
198             } else {
199                 this.testResult = res.template_output().data();
200             }
201         }, err => {
202             this.testDone = true;
203             this.errorDuringTestString.current().then(str => this.testErr1 = str);
204             this.testErr2 = err;
205         });
206     }
207
208     clearTestResults = () => {
209         this.testDone = false;
210         this.testErr1 = '';
211         this.testErr2 = '';
212         this.testResult = '';
213     }
214     back = () => {
215         this.router.navigate(['/staff/admin/local/action_trigger/event_definition/']);
216     }
217 }