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, Router} 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 {NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
16 templateUrl: './trigger-edit.component.html'
19 export class EditEventDefinitionComponent implements OnInit {
24 evtAltEligible: Boolean = false;
26 testErr1: String = '';
27 testErr2: String = '';
28 testResult: String = '';
29 testDone: Boolean = false;
31 altTemplateDataSource: GridDataSource = new GridDataSource();
32 envDataSource: GridDataSource = new GridDataSource();
33 paramDataSource: GridDataSource = new GridDataSource();
35 editTab: 'def' | 'alt' | 'env' | 'param' | 'test' = 'def';
37 @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
38 @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
39 @ViewChild('altTemplateDialog') altTemplateDialog: FmRecordEditorComponent;
41 @ViewChild('envGrid') envGrid: GridComponent;
42 @ViewChild('paramGrid') paramGrid: GridComponent;
43 @ViewChild('altTemplateGrid') altTemplateGrid: GridComponent;
45 @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
46 @ViewChild('updateFailedString') updateFailedString: StringComponent;
47 @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
48 @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
49 @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
50 @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
51 @ViewChild('createSuccessString') createSuccessString: StringComponent;
52 @ViewChild('createErrString') createErrString: StringComponent;
53 @ViewChild('eventDuringTestString') eventDuringTestString: StringComponent;
54 @ViewChild('errorDuringTestString') errorDuringTestString: StringComponent;
57 private router: Router,
58 private idl: IdlService,
59 private pcrud: PcrudService,
60 private toast: ToastService,
61 private route: ActivatedRoute,
62 private net: NetService,
63 private auth: AuthService,
68 this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
70 // get current event def name to display on the banner
71 this.pcrud.search('atevdef',
72 {id: this.evtDefId}, {}).toPromise().then(rec => {
73 this.evtDefName = rec.name();
76 // get current event def reactor to decide if the alt template tab should show
77 this.pcrud.search('atevdef',
78 {id: this.evtDefId}, {}).toPromise().then(rec => {
79 this.evtReactor = rec.reactor();
80 if ('ProcessTemplate SendEmail SendSMS'.indexOf(this.evtReactor) > -1) { this.evtAltEligible = true; }
83 this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
84 return this.pcrud.search('atenv',
85 {event_def: this.evtDefId}, {});
88 this.altTemplateDataSource.getRows = (pager: Pager, sort: any[]) => {
89 return this.pcrud.search('atevalt',
90 {event_def: this.evtDefId}, {});
93 this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
94 return this.pcrud.search('atevparam',
95 {event_def: this.evtDefId}, {});
99 onTabChange(event: NgbNavChangeEvent) {
100 this.editTab = event.nextId;
103 createNewEnv = () => {
104 this.createNewThing(this.envDialog, this.envGrid, 'atenv');
107 createNewAltTemplate = () => {
108 this.createNewThing(this.altTemplateDialog, this.altTemplateGrid, 'atevalt');
111 createNewParam = () => {
112 this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
115 createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
116 currentDialog.mode = 'create';
117 currentDialog.recordId = null;
118 const newRecord = this.idl.create(idl);
119 newRecord.event_def(this.evtDefId);
120 currentDialog.mode = 'create';
121 currentDialog.record = newRecord;
122 currentDialog.open({size: 'lg'}).subscribe(
124 this.createSuccessString.current()
125 .then(str => this.toast.success(str));
126 currentGrid.reload();
129 if (!rejection.dismissed) {
130 this.createErrString.current()
131 .then(str => this.toast.danger(str));
137 deleteSelected = (idlThings: IdlObject[]) => {
139 if (idlThings[0].classname === 'atenv') {
140 currentGrid = this.envGrid;
141 } else if (idlThings[0].classname === 'atevalt') {
142 currentGrid = this.altTemplateGrid;
144 currentGrid = this.paramGrid;
146 idlThings.forEach(idlThing => idlThing.isdeleted(true));
148 this.pcrud.autoApply(idlThings).subscribe(
150 console.debug('deleted: ' + val);
151 this.deleteSuccessString.current()
152 .then(str => this.toast.success(str));
156 this.deleteFailedString.current()
157 .then(str => this.toast.danger(str));
161 currentGrid.reload();
167 editSelected = (selectedRecords: IdlObject[]) => {
168 const editOneThing = (record: IdlObject) => {
169 if (!record) { return; }
170 this.showEditDialog(record).then(
171 () => editOneThing(selectedRecords.shift()));
173 editOneThing(selectedRecords.shift());
176 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
179 if (selectedRecord.classname === 'atenv') {
180 currentDialog = this.envDialog;
181 currentGrid = this.envGrid;
182 } else if (selectedRecord.classname === 'atevalt') {
183 currentDialog = this.altTemplateDialog;
184 currentGrid = this.altTemplateGrid;
186 currentDialog = this.paramDialog;
187 currentGrid = this.paramGrid;
189 currentDialog.mode = 'update';
190 const clone = this.idl.clone(selectedRecord);
191 currentDialog.record = clone;
192 return new Promise((resolve, reject) => {
193 currentDialog.open({size: 'lg'}).subscribe(
195 this.updateSuccessString.current()
196 .then(str => this.toast.success(str));
197 currentGrid.reload();
201 this.updateFailedString.current()
202 .then(str => this.toast.danger(str));
209 runTest = (barcode) => {
213 this.clearTestResults();
215 'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
216 this.auth.token(), this.evtDefId, barcode
218 this.testDone = true;
220 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
221 .then(str => this.testErr1 = str);
222 this.testErr2 = res.desc;
224 this.testResult = res.template_output().data();
226 // eslint-disable-next-line rxjs/no-implicit-any-catch
228 this.testDone = true;
229 this.errorDuringTestString.current().then(str => this.testErr1 = str);
234 clearTestResults = () => {
235 this.testDone = false;
238 this.testResult = '';
241 this.router.navigate(['/staff/admin/local/action_trigger/event_definition/']);