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';
17 templateUrl: './trigger-edit.component.html'
20 export class EditEventDefinitionComponent implements OnInit {
25 evtAltEligible: Boolean = false;
27 testErr1: String = '';
28 testErr2: String = '';
29 testResult: String = '';
30 testDone: Boolean = false;
32 altTemplateDataSource: GridDataSource = new GridDataSource();
33 envDataSource: GridDataSource = new GridDataSource();
34 paramDataSource: GridDataSource = new GridDataSource();
36 editTab: 'def' | 'alt' | 'env' | 'param' | 'test' = 'def';
38 @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
39 @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
40 @ViewChild('altTemplateDialog') altTemplateDialog: FmRecordEditorComponent;
42 @ViewChild('envGrid') envGrid: GridComponent;
43 @ViewChild('paramGrid') paramGrid: GridComponent;
44 @ViewChild('altTemplateGrid') altTemplateGrid: GridComponent;
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;
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,
69 this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
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();
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; }
84 this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
85 return this.pcrud.search('atenv',
86 {event_def: this.evtDefId}, {});
89 this.altTemplateDataSource.getRows = (pager: Pager, sort: any[]) => {
90 return this.pcrud.search('atevalt',
91 {event_def: this.evtDefId}, {});
94 this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
95 return this.pcrud.search('atevparam',
96 {event_def: this.evtDefId}, {});
100 onTabChange(event: NgbNavChangeEvent) {
101 this.editTab = event.nextId;
104 createNewEnv = () => {
105 this.createNewThing(this.envDialog, this.envGrid, 'atenv');
108 createNewAltTemplate = () => {
109 this.createNewThing(this.altTemplateDialog, this.altTemplateGrid, 'atevalt');
112 createNewParam = () => {
113 this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
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(
125 this.createSuccessString.current()
126 .then(str => this.toast.success(str));
127 currentGrid.reload();
130 if (!rejection.dismissed) {
131 this.createErrString.current()
132 .then(str => this.toast.danger(str));
138 deleteSelected = (idlThings: IdlObject[]) => {
140 if (idlThings[0].classname === 'atenv') {
141 currentGrid = this.envGrid;
142 } else if (idlThings[0].classname === 'atevalt') {
143 currentGrid = this.altTemplateGrid;
145 currentGrid = this.paramGrid;
147 idlThings.forEach(idlThing => idlThing.isdeleted(true));
149 this.pcrud.autoApply(idlThings).subscribe(
151 console.debug('deleted: ' + val);
152 this.deleteSuccessString.current()
153 .then(str => this.toast.success(str));
157 this.deleteFailedString.current()
158 .then(str => this.toast.danger(str));
162 currentGrid.reload();
168 editSelected = (selectedRecords: IdlObject[]) => {
169 const editOneThing = (record: IdlObject) => {
170 if (!record) { return; }
171 this.showEditDialog(record).then(
172 () => editOneThing(selectedRecords.shift()));
174 editOneThing(selectedRecords.shift());
177 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
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;
187 currentDialog = this.paramDialog;
188 currentGrid = this.paramGrid;
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(
196 this.updateSuccessString.current()
197 .then(str => this.toast.success(str));
198 currentGrid.reload();
202 this.updateFailedString.current()
203 .then(str => this.toast.danger(str));
210 runTest = (barcode) => {
214 this.clearTestResults();
216 'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
217 this.auth.token(), this.evtDefId, barcode
219 this.testDone = true;
221 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
222 .then(str => this.testErr1 = str);
223 this.testErr2 = res.desc;
225 this.testResult = res.template_output().data();
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/']);