]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/triggers/trigger-edit.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[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     evtReactor: string;
25     evtAltEligible: Boolean = false;
26
27     testErr1: String = '';
28     testErr2: String = '';
29     testResult: String = '';
30     testDone: Boolean = false;
31
32     altTemplateDataSource: GridDataSource = new GridDataSource();
33     envDataSource: GridDataSource = new GridDataSource();
34     paramDataSource: GridDataSource = new GridDataSource();
35
36     editTab: 'def' | 'alt' | 'env' | 'param' | 'test' = 'def';
37
38     @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
39     @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
40     @ViewChild('altTemplateDialog') altTemplateDialog: FmRecordEditorComponent;
41
42     @ViewChild('envGrid') envGrid: GridComponent;
43     @ViewChild('paramGrid') paramGrid: GridComponent;
44     @ViewChild('altTemplateGrid') altTemplateGrid: GridComponent;
45
46     @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
47     @ViewChild('updateFailedString') updateFailedString: StringComponent;
48     @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
49     @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
50     @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
51     @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
52     @ViewChild('createSuccessString') createSuccessString: StringComponent;
53     @ViewChild('createErrString') createErrString: StringComponent;
54     @ViewChild('eventDuringTestString') eventDuringTestString: StringComponent;
55     @ViewChild('errorDuringTestString') errorDuringTestString: StringComponent;
56
57     constructor(
58         private router: Router,
59         private idl: IdlService,
60         private pcrud: PcrudService,
61         private toast: ToastService,
62         private route: ActivatedRoute,
63         private net: NetService,
64         private auth: AuthService,
65     ) {
66     }
67
68     ngOnInit() {
69         this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
70
71         // get current event def name to display on the banner
72         this.pcrud.search('atevdef',
73             {id: this.evtDefId}, {}).toPromise().then(rec => {
74             this.evtDefName = rec.name();
75         });
76
77         // get current event def reactor to decide if the alt template tab should show
78         this.pcrud.search('atevdef',
79             {id: this.evtDefId}, {}).toPromise().then(rec => {
80             this.evtReactor = rec.reactor();
81             if ('ProcessTemplate SendEmail SendSMS'.indexOf(this.evtReactor) > -1) { this.evtAltEligible = true; }
82         });
83
84         this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
85             return this.pcrud.search('atenv',
86                 {event_def: this.evtDefId}, {});
87         };
88
89         this.altTemplateDataSource.getRows = (pager: Pager, sort: any[]) => {
90             return this.pcrud.search('atevalt',
91                 {event_def: this.evtDefId}, {});
92         };
93
94         this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
95             return this.pcrud.search('atevparam',
96                 {event_def: this.evtDefId}, {});
97         };
98     }
99
100     onTabChange(event: NgbNavChangeEvent) {
101         this.editTab = event.nextId;
102     }
103
104     createNewEnv = () => {
105         this.createNewThing(this.envDialog, this.envGrid, 'atenv');
106     }
107
108     createNewAltTemplate = () => {
109         this.createNewThing(this.altTemplateDialog, this.altTemplateGrid, 'atevalt');
110     }
111
112     createNewParam = () => {
113         this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
114     }
115
116     createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
117         currentDialog.mode = 'create';
118         currentDialog.recordId = null;
119         const newRecord = this.idl.create(idl);
120         newRecord.event_def(this.evtDefId);
121         currentDialog.mode = 'create';
122         currentDialog.record = newRecord;
123         currentDialog.open({size: 'lg'}).subscribe(
124             ok => {
125                 this.createSuccessString.current()
126                     .then(str => this.toast.success(str));
127                 currentGrid.reload();
128             },
129             rejection => {
130                 if (!rejection.dismissed) {
131                     this.createErrString.current()
132                         .then(str => this.toast.danger(str));
133                 }
134             }
135         );
136     }
137
138     deleteSelected = (idlThings: IdlObject[]) => {
139         let currentGrid;
140         if (idlThings[0].classname === 'atenv') {
141             currentGrid = this.envGrid;
142         } else if (idlThings[0].classname === 'atevalt') {
143             currentGrid = this.altTemplateGrid;
144         } else {
145             currentGrid = this.paramGrid;
146         }
147         idlThings.forEach(idlThing => idlThing.isdeleted(true));
148         let _deleted = 0;
149         this.pcrud.autoApply(idlThings).subscribe(
150             val => {
151                 console.debug('deleted: ' + val);
152                 this.deleteSuccessString.current()
153                     .then(str => this.toast.success(str));
154                 _deleted++;
155             },
156             err => {
157                 this.deleteFailedString.current()
158                     .then(str => this.toast.danger(str));
159             },
160             () => {
161                 if (_deleted > 0) {
162                     currentGrid.reload();
163                 }
164             }
165         );
166     }
167
168     editSelected = (selectedRecords: IdlObject[]) => {
169         const editOneThing = (record: IdlObject) => {
170             if (!record) { return; }
171             this.showEditDialog(record).then(
172                 () => editOneThing(selectedRecords.shift()));
173         };
174         editOneThing(selectedRecords.shift());
175     }
176
177     showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
178         let currentDialog;
179         let currentGrid;
180         if (selectedRecord.classname === 'atenv') {
181             currentDialog = this.envDialog;
182             currentGrid = this.envGrid;
183         } else if (selectedRecord.classname === 'atevalt') {
184             currentDialog = this.altTemplateDialog;
185             currentGrid = this.altTemplateGrid;
186         } else {
187             currentDialog = this.paramDialog;
188             currentGrid = this.paramGrid;
189         }
190         currentDialog.mode = 'update';
191         const clone = this.idl.clone(selectedRecord);
192         currentDialog.record = clone;
193         return new Promise((resolve, reject) => {
194             currentDialog.open({size: 'lg'}).subscribe(
195                 result => {
196                     this.updateSuccessString.current()
197                         .then(str => this.toast.success(str));
198                     currentGrid.reload();
199                     resolve(result);
200                 },
201                 error => {
202                     this.updateFailedString.current()
203                         .then(str => this.toast.danger(str));
204                     reject(error);
205                }
206            );
207        });
208     }
209
210     runTest = (barcode) => {
211         if (!barcode) {
212             return;
213         }
214         this.clearTestResults();
215         this.net.request(
216             'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
217             this.auth.token(), this.evtDefId, barcode
218         ).subscribe(res => {
219             this.testDone = true;
220             if (res.ilsevent) {
221                 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
222                     .then(str => this.testErr1 = str);
223                 this.testErr2 = res.desc;
224             } else {
225                 this.testResult = res.template_output().data();
226             }
227         }, err => {
228             this.testDone = true;
229             this.errorDuringTestString.current().then(str => this.testErr1 = str);
230             this.testErr2 = err;
231         });
232     }
233
234     clearTestResults = () => {
235         this.testDone = false;
236         this.testErr1 = '';
237         this.testErr2 = '';
238         this.testResult = '';
239     }
240     back = () => {
241         this.router.navigate(['/staff/admin/local/action_trigger/event_definition/']);
242     }
243 }