-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathedit-business-rule-parameters.component.ts
More file actions
229 lines (212 loc) · 7.94 KB
/
edit-business-rule-parameters.component.ts
File metadata and controls
229 lines (212 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/** Angular Imports */
import { Component, OnChanges, Input, Output, EventEmitter, OnInit, inject } from '@angular/core';
import { Validators, UntypedFormGroup, UntypedFormControl, ReactiveFormsModule } from '@angular/forms';
/** Rxjs Imports */
import { distinctUntilChanged } from 'rxjs/operators';
/** Custom Services */
import { ReportsService } from 'app/reporting-plugin/services/reports.service';
import { SettingsService } from 'app/settings/settings.service';
/** Custom Models */
import { ReportParameter } from 'app/reporting-plugin/models/report-parameter.model';
import { SelectOption } from 'app/reporting-plugin/models/select-option.model';
import { Dates } from 'app/core/utils/dates';
import { MatDivider } from '@angular/material/divider';
import { NgFor, NgSwitch, NgIf, NgSwitchCase } from '@angular/common';
import { MatStepperPrevious, MatStepperNext } from '@angular/material/stepper';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Edit Business Rule Parameters.
*/
@Component({
selector: 'mifosx-edit-business-rule-parameters',
templateUrl: './edit-business-rule-parameters.component.html',
styleUrls: ['./edit-business-rule-parameters.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatDivider,
NgSwitch,
NgSwitchCase,
MatStepperPrevious,
FaIconComponent,
MatStepperNext
]
})
export class EditBusinessRuleParametersComponent implements OnInit, OnChanges {
private reportsService = inject(ReportsService);
private settingsService = inject(SettingsService);
private dateUtils = inject(Dates);
/** Run Report Parameters Data */
@Input() paramData: any;
/** SMS Campaign */
@Input() smsCampaign: any;
/** Template Parameters Event Emitter */
@Output() templateParameters = new EventEmitter();
/** Initializes new form group ReportForm */
ReportForm = new UntypedFormGroup({});
/** Array of all parent parameters */
parentParameters: any[] = [];
/** Displayed user choices */
paramValue: any;
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
maxDate = new Date();
ngOnInit(): void {
this.maxDate = this.settingsService.businessDate;
}
ngOnChanges() {
if (this.paramData) {
this.ReportForm = new UntypedFormGroup({});
this.paramValue = JSON.parse(this.smsCampaign.paramValue);
this.createRunReportForm();
this.disableFormWhenValid();
this.getResponseHeaders();
}
}
/**
* Establishes form controls for Report Parameter's name attribute,
* Fetches dropdown options and builds child dependencies.
*/
createRunReportForm() {
this.paramData.forEach((param: any) => {
if (!param.parentParameterName) {
// Non Child Parameter
this.ReportForm.addControl(param.name, new UntypedFormControl('', Validators.required));
const controlValue = this.paramValue[param.variable].toString();
switch (param.displayType) {
case 'text':
this.ReportForm.get(param.name).patchValue(controlValue);
break;
case 'select':
this.fetchSelectOptions(param, param.name);
break;
case 'date':
const dateFormat = this.settingsService.dateFormat;
const newControlValue = this.dateUtils.formatDate(controlValue, dateFormat);
this.ReportForm.get(param.name).patchValue(newControlValue);
break;
}
} else {
// Child Parameter
const parent: ReportParameter = this.paramData.find((entry: any) => entry.name === param.parentParameterName);
parent.childParameters.push(param);
this.updateParentParameters(parent);
}
});
this.setChildControls();
}
/**
* Updates the array of parent parameters.
* @param {ReportParameter} parent Parent report parameter
*/
updateParentParameters(parent: ReportParameter) {
const parentNames = this.parentParameters.map((parameter) => parameter.name);
if (!parentNames.includes(parent.name)) {
// Parent's first child.
this.parentParameters.push(parent);
} else {
// Parent already has a child
const index = parentNames.indexOf(parent.name);
this.parentParameters[index] = parent;
}
}
/**
* Subscribes to changes in parent parameters value, builds child parameter vis-a-vis parent's value.
*/
setChildControls() {
this.parentParameters.forEach((param: ReportParameter) => {
this.ReportForm.get(param.name).valueChanges.subscribe((option: any) => {
param.childParameters.forEach((child: ReportParameter) => {
if (child.displayType === 'none') {
this.ReportForm.addControl(child.name, new UntypedFormControl(child.defaultVal));
} else {
this.ReportForm.addControl(child.name, new UntypedFormControl('', Validators.required));
}
if (child.displayType === 'select') {
const inputstring = `${child.name}?${param.inputName}=${option.id}`;
this.fetchSelectOptions(child, inputstring);
}
});
});
});
}
/**
* Fetches Select Dropdown options for param type "Select".
* @param {ReportParameter} param Parameter for which dropdown options are required.
* @param {string} inputstring url substring for API call.
*/
fetchSelectOptions(param: ReportParameter, inputstring: string) {
this.reportsService.getSelectOptions(inputstring).subscribe((options: SelectOption[]) => {
param.selectOptions = options;
if (param.selectAll === 'Y') {
param.selectOptions.push({ id: '-1', name: 'All' });
}
const optionId = this.paramValue[param.variable].toString();
const option = options.find((entry) => entry.id === optionId);
this.ReportForm.controls[param.name].patchValue({ id: optionId, name: option.name });
});
}
/**
* Compare function for mat-select.
* Useful in patching values if value is an object.
* @param {any} option1 option 1
* @param {any} option2 option 2.
*/
compareOptions(option1: any, option2: any) {
return option1 && option2 && option1.id === option2.id;
}
/**
* Disable the Report Form once all values are patched.
*/
disableFormWhenValid() {
this.ReportForm.statusChanges.pipe(distinctUntilChanged()).subscribe((status: string) => {
if (status === 'VALID') {
this.ReportForm.disable();
}
});
}
/**
* Formats user response and readies it for utilization by run report function.
* @param {any} response Object containing formcontrol values.
*/
formatUserResponse(response: any, forHeaders: boolean) {
const formattedResponse: any = {};
let newKey: string;
for (const [
key,
value
] of Object.entries(response)) {
const param: ReportParameter = this.paramData.find((_entry: any) => _entry.variable === key);
newKey = forHeaders ? param.inputName : param.variable;
formattedResponse[newKey] = value;
}
return formattedResponse;
}
/**
* Gets run report response headers.
* Emits them via template parameters event emitter.
* TODO: Replace report object with report name once reports service is refactored.
*/
getResponseHeaders() {
const reportName = this.paramValue.reportName;
delete this.paramValue.reportName;
const formattedResponse = this.formatUserResponse(this.paramValue, true);
this.reportsService.getRunReportData(reportName, formattedResponse).subscribe(
(response: any) => {
this.templateParameters.emit(response.columnHeaders);
},
(error: any) => {
this.templateParameters.emit(null);
this.ReportForm.disable();
}
);
}
}