-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathbusiness-rule-parameters.component.ts
More file actions
206 lines (190 loc) · 6.99 KB
/
business-rule-parameters.component.ts
File metadata and controls
206 lines (190 loc) · 6.99 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
/**
* 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';
/** 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 { MatStepperNext } from '@angular/material/stepper';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Business Rule Parameters Component.
*/
@Component({
selector: 'mifosx-business-rule-parameters',
templateUrl: './business-rule-parameters.component.html',
styleUrls: ['./business-rule-parameters.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatDivider,
NgSwitch,
NgSwitchCase,
MatStepperNext
]
})
export class BusinessRuleParametersComponent implements OnInit, OnChanges {
private reportsService = inject(ReportsService);
private settingsService = inject(SettingsService);
private dateUtils = inject(Dates);
/** Run Report Parameters Data */
@Input() paramData: any;
/** Report Name */
reportName: string;
/** Initializes new form group ReportForm */
ReportForm = new UntypedFormGroup({});
/** Array of all parent parameters */
parentParameters: any[] = [];
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
maxDate = new Date();
/** Template Parameters Event Emitter */
@Output() templateParameters = new EventEmitter();
ngOnInit(): void {
this.maxDate = this.settingsService.businessDate;
}
ngOnChanges() {
if (this.paramData) {
this.ReportForm = new UntypedFormGroup({});
this.reportName = this.paramData.reportName;
this.paramData = this.paramData.response;
this.createRunReportForm();
}
}
/**
* Formatted form value for API request.
*/
get businessRuleFormValue() {
const formattedresponse = this.formatUserResponse(this.ReportForm.value, false);
formattedresponse.reportName = this.reportName;
return { paramValue: formattedresponse };
}
/**
* 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));
if (param.displayType === 'select') {
this.fetchSelectOptions(param, param.name);
}
} 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' });
}
});
}
/**
* 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.name === key);
newKey = forHeaders ? param.inputName : param.variable;
switch (param.displayType) {
case 'text':
formattedResponse[newKey] = value;
break;
case 'select':
formattedResponse[newKey] = (value as { id: string | number })['id'];
break;
case 'date':
const dateFormat = this.settingsService.dateFormat;
formattedResponse[newKey] = this.dateUtils.formatDate(value, dateFormat);
break;
case 'none':
formattedResponse[newKey] = value;
break;
}
}
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 formattedresponse = this.formatUserResponse(this.ReportForm.value, true);
this.reportsService.getRunReportData(this.reportName, formattedresponse).subscribe(
(response: any) => {
this.templateParameters.emit(response.columnHeaders);
},
(error: any) => {
this.templateParameters.emit(null);
}
);
}
}