Skip to content

Commit fe48fba

Browse files
chore: fix complex typescript errors
1 parent ecbbcdb commit fe48fba

File tree

27 files changed

+135
-139
lines changed

27 files changed

+135
-139
lines changed

.eslintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
"flowtype/no-types-missing-file-annotation": "off",
159159
"flowtype/define-flow-type": "off",
160160
"flowtype/require-valid-file-annotation": "off",
161-
162161
"@typescript-eslint/explicit-module-boundary-types": "off",
163162
"@typescript-eslint/explicit-function-return-type": "off",
164163
"@typescript-eslint/no-unused-vars": [

packages/rules-engine/.eslintrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@
7777
"flowtype/no-types-missing-file-annotation": "off",
7878
"flowtype/define-flow-type": "off",
7979
"flowtype/require-valid-file-annotation": "off",
80-
81-
"@typescript-eslint/indent": [2, 4],
8280
"@typescript-eslint/explicit-function-return-type": "off",
8381
"@typescript-eslint/no-unused-vars": [
8482
"error",

packages/rules-engine/d2.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
type: 'lib',
33
entryPoints: {
4-
lib: 'src/index.js',
4+
lib: 'src/index.ts',
55
},
66
};

packages/rules-engine/src/RulesEngine.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ export class RulesEngine {
2020
valueProcessor: ValueProcessor;
2121
variableService: VariableService;
2222
dateUtils: IDateUtils;
23-
userRoles: Array<string>;
23+
userRoles!: Array<string>;
2424
flags: Flag;
2525

2626
constructor(
2727
inputConverter: IConvertInputRulesValue,
2828
outputConverter: IConvertOutputRulesEffectsValue,
2929
dateUtils: IDateUtils,
30-
environment: keyof typeof environmentTypes,
30+
environment: typeof environmentTypes[keyof typeof environmentTypes],
3131
flags?: Flag,
3232
) {
3333
this.inputConverter = inputConverter;
3434
this.outputConverter = outputConverter;
3535
this.valueProcessor = new ValueProcessor(inputConverter);
3636
this.variableService = new VariableService(this.valueProcessor.processValue, dateUtils, environment);
3737
this.dateUtils = dateUtils;
38-
this.flags = flags ?? {};
38+
this.flags = flags ?? { verbose: false };
3939
}
4040

4141
/**
@@ -135,7 +135,7 @@ export class RulesEngine {
135135
'had no condition specified. Please check rule configuration.');
136136
}
137137

138-
let programRuleEffects = [];
138+
let programRuleEffects: any[] = [];
139139
if (isProgramRuleExpressionEffective) {
140140
programRuleEffects = rule.programRuleActions.map((
141141
{
@@ -206,7 +206,6 @@ export class RulesEngine {
206206
effects,
207207
dataElements,
208208
trackedEntityAttributes,
209-
// $FlowFixMe[exponential-spread]
210209
formValues: { ...selectedEntity, ...currentEvent },
211210
onProcessValue: this.valueProcessor.processValue,
212211
});

packages/rules-engine/src/d2Functions/getD2Functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export const getD2Functions = ({
318318
dhisFunctions,
319319
variablesHash,
320320
onError: () => log.error(`Evaluation of d2:condition expression ${params[0]} failed`),
321-
onVerboseLog: () => {},
321+
onVerboseLog: () => { /* Empty function - keeping original functionality as before ts rewrite */ },
322322
});
323323
return result ? params[1] : params[2];
324324
},

packages/rules-engine/src/helpers/OptionSetHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class OptionSetHelper {
77
if (dataElementValueType === typeKeys.MULTI_TEXT) {
88
const values = key.split(',');
99
const optionsName = options.reduce(
10-
(acc, option) => (values.includes(option.code) ? acc.concat(option.displayName) : acc),
10+
(acc: any, option) => (values.includes(option.code) ? acc.concat(option.displayName) : acc),
1111
[],
1212
);
1313
return optionsName.length !== 0 ? optionsName.join(',') : key;

packages/rules-engine/src/helpers/previousValueCheck.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { rulesEngineEffectTargetDataTypes, typeKeys } from '../constants';
2-
import type { DataElements, TrackedEntityAttributes, HideOutputEffect } from '../rulesEngine.types';
2+
import type { DataElements, TrackedEntityAttributes, HideOutputEffect, OutputEffect } from '../rulesEngine.types';
33

44
const processDataElementValue = ({
55
dataElementId,
@@ -55,9 +55,9 @@ export const getOutputEffectsWithPreviousValueCheck = ({
5555
dataElements: DataElements | null,
5656
trackedEntityAttributes: TrackedEntityAttributes | null,
5757
formValues?: { [key: string]: any } | null,
58-
onProcessValue: (value: any, type: keyof typeof typeKeys) => any,
58+
onProcessValue?: (value: any, type: typeof typeKeys[keyof typeof typeKeys]) => any,
5959
}) =>
60-
outputEffects.reduce((acc, outputEffect) => {
60+
outputEffects.reduce((acc: OutputEffect[], outputEffect) => {
6161
if (formValues && Object.keys(formValues).length !== 0 && outputEffect.targetDataType) {
6262
const formValue = formValues[outputEffect.id];
6363
const rawValue = mapByTargetDataTypes[outputEffect.targetDataType]({
@@ -68,7 +68,7 @@ export const getOutputEffectsWithPreviousValueCheck = ({
6868
});
6969
if (rawValue) {
7070
const { valueType, name } = rawValue;
71-
const value = onProcessValue(formValue, valueType);
71+
const value = onProcessValue && onProcessValue(formValue, valueType);
7272

7373
if (value != null) {
7474
return [...acc, { ...outputEffect, hadValue: true, name }];

packages/rules-engine/src/processors/ValueProcessor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class ValueProcessor {
1111
};
1212

1313
converterObject: IConvertInputRulesValue;
14-
processValue: (value: any, type: typeof typeKeys[keyof typeof typeKeys]) => any;
1514

1615
constructor(converterObject: IConvertInputRulesValue) {
1716
this.converterObject = converterObject;
@@ -28,7 +27,7 @@ export class ValueProcessor {
2827
log.warn(errorCreator(ValueProcessor.errorMessages.CONVERTER_NOT_FOUND)({ type }));
2928
return value;
3029
}
31-
// $FlowFixMe
30+
3231
const convertedValue = this.converterObject[convertFnName] ? this.converterObject[convertFnName](value) : value;
3332
return convertedValue ?? null;
3433
}

packages/rules-engine/src/processors/rulesEffectsProcessor/rulesEffectsProcessor.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getRulesEffectsProcessor(
5959

6060
function createErrorDetectionEffect(
6161
effect: ProgramRuleEffect,
62-
type: keyof typeof effectActions): any {
62+
type: typeof effectActions[keyof typeof effectActions]): any {
6363
const result = createEffectsForConfiguredDataTypes(effect, (): any => ({
6464
type,
6565
message: `${effect.displayContent || ''} ${sanitiseFalsy(effect.data)}`,
@@ -72,7 +72,7 @@ export function getRulesEffectsProcessor(
7272

7373
function createWarningEffect(
7474
effect: ProgramRuleEffect,
75-
type: keyof typeof effectActions): WarningEffect {
75+
type: typeof effectActions[keyof typeof effectActions]): WarningEffect {
7676
const result = createErrorDetectionEffect(effect, type);
7777
if (Array.isArray(result)) {
7878
return result;
@@ -86,7 +86,7 @@ export function getRulesEffectsProcessor(
8686

8787
function createErrorEffect(
8888
effect: ProgramRuleEffect,
89-
type: keyof typeof effectActions): ErrorEffect {
89+
type: typeof effectActions[keyof typeof effectActions]): ErrorEffect {
9090
const result = createErrorDetectionEffect(effect, type);
9191
if (Array.isArray(result)) {
9292
return result;
@@ -102,7 +102,6 @@ export function getRulesEffectsProcessor(
102102
let outputValue;
103103
if (normalizedValue || normalizedValue === 0 || normalizedValue === false) {
104104
const converterName: string = mapTypeToInterfaceFnName[valueType];
105-
// $FlowExpectedError
106105
const outputConverter = outputConverters[converterName];
107106
if (!converterName || !outputConverter) {
108107
log.warn(errorCreator('converter for valueType is missing')({ valueType }));
@@ -118,7 +117,7 @@ export function getRulesEffectsProcessor(
118117
function createAssignValueEffect(
119118
effect: ProgramRuleEffect,
120119
element: DataElement | TrackedEntityAttribute,
121-
targetDataType: keyof typeof rulesEngineEffectTargetDataTypes,
120+
targetDataType: typeof rulesEngineEffectTargetDataTypes[keyof typeof rulesEngineEffectTargetDataTypes],
122121
): AssignOutputEffect {
123122
const normalizedValue = normalizeRuleVariable(effect.data, element.valueType);
124123
const outputValue = convertNormalizedValueToOutputValue(normalizedValue, element.valueType);
@@ -140,7 +139,7 @@ export function getRulesEffectsProcessor(
140139
effects.push(createAssignValueEffect(
141140
effect,
142141
dataElements[effect.dataElementId],
143-
'DATA_ELEMENT' as keyof typeof rulesEngineEffectTargetDataTypes),
142+
rulesEngineEffectTargetDataTypes.DATA_ELEMENT),
144143
);
145144
}
146145
if (trackedEntityAttributes &&
@@ -149,7 +148,7 @@ export function getRulesEffectsProcessor(
149148
effects.push(createAssignValueEffect(
150149
effect,
151150
trackedEntityAttributes[effect.trackedEntityAttributeId],
152-
'TRACKED_ENTITY_ATTRIBUTE' as keyof typeof rulesEngineEffectTargetDataTypes),
151+
rulesEngineEffectTargetDataTypes.TRACKED_ENTITY_ATTRIBUTE),
153152
);
154153
}
155154
return effects;
@@ -160,7 +159,7 @@ export function getRulesEffectsProcessor(
160159
dataElements: DataElements | null,
161160
trackedEntityAttributes: TrackedEntityAttributes | null,
162161
formValues?: {[key: string]: any} | null,
163-
onProcessValue?: (value: any, type: keyof typeof typeKeys) => any,
162+
onProcessValue?: (value: any, type: typeof typeKeys[keyof typeof typeKeys]) => any,
164163
): Array<HideOutputEffect> {
165164
const outputEffects = createEffectsForConfiguredDataTypes(
166165
effect,
@@ -169,32 +168,31 @@ export function getRulesEffectsProcessor(
169168
content: effect.content,
170169
}),
171170
);
172-
const result = getOutputEffectsWithPreviousValueCheck({
171+
return getOutputEffectsWithPreviousValueCheck({
173172
outputEffects,
174173
formValues,
175-
dataElementId: effect.dataElementId,
176-
trackedEntityAttributeId: effect.trackedEntityAttributeId,
174+
dataElementId: effect.dataElementId as string,
175+
trackedEntityAttributeId: effect.trackedEntityAttributeId as string,
177176
dataElements,
178177
trackedEntityAttributes,
179178
onProcessValue,
180179
});
181-
return Array.isArray(result) ? result : [result as any];
182180
}
183181

184182
function processShowError(effect: ProgramRuleEffect): ErrorEffect {
185-
return createErrorEffect(effect, effectActions.SHOW_ERROR as any);
183+
return createErrorEffect(effect, effectActions.SHOW_ERROR);
186184
}
187185

188186
function processShowWarning(effect: ProgramRuleEffect): WarningEffect {
189-
return createWarningEffect(effect, effectActions.SHOW_WARNING as any);
187+
return createWarningEffect(effect, effectActions.SHOW_WARNING);
190188
}
191189

192190
function processShowErrorOnComplete(effect: ProgramRuleEffect): ErrorEffect {
193-
return createErrorEffect(effect, effectActions.SHOW_ERROR_ONCOMPLETE as any);
191+
return createErrorEffect(effect, effectActions.SHOW_ERROR_ONCOMPLETE);
194192
}
195193

196194
function processShowWarningOnComplete(effect: ProgramRuleEffect): WarningEffect {
197-
return createWarningEffect(effect, effectActions.SHOW_WARNING_ONCOMPLETE as any);
195+
return createWarningEffect(effect, effectActions.SHOW_WARNING_ONCOMPLETE);
198196
}
199197

200198
function processHideSection(effect: ProgramRuleEffect): HideOutputEffect | null {
@@ -203,7 +201,7 @@ export function getRulesEffectsProcessor(
203201
}
204202

205203
return {
206-
type: effectActions.HIDE_SECTION as any,
204+
type: effectActions.HIDE_SECTION,
207205
id: effect.programStageSectionId,
208206
};
209207
}
@@ -214,21 +212,21 @@ export function getRulesEffectsProcessor(
214212
}
215213

216214
return {
217-
type: effectActions.HIDE_PROGRAM_STAGE as any,
215+
type: effectActions.HIDE_PROGRAM_STAGE,
218216
id: effect.programStageId,
219217
};
220218
}
221219

222220
function processMakeCompulsory(effect: ProgramRuleEffect): Array<CompulsoryEffect> {
223221
return createEffectsForConfiguredDataTypes(effect, () => ({
224-
type: effectActions.MAKE_COMPULSORY as any,
222+
type: effectActions.MAKE_COMPULSORY,
225223
}));
226224
}
227225

228226
function processDisplayText(effect: ProgramRuleEffect): any {
229227
const message = effect.displayContent || '';
230228
return {
231-
type: effectActions.DISPLAY_TEXT as any,
229+
type: effectActions.DISPLAY_TEXT,
232230
id: effect.location,
233231
displayText: {
234232
id: effect.id,
@@ -242,7 +240,7 @@ export function getRulesEffectsProcessor(
242240
const data = effect.data !== undefined ? effect.data : '';
243241

244242
return {
245-
type: effectActions.DISPLAY_KEY_VALUE_PAIR as any,
243+
type: effectActions.DISPLAY_KEY_VALUE_PAIR,
246244
id: effect.location,
247245
displayKeyValuePair: {
248246
id: effect.id,
@@ -255,21 +253,21 @@ export function getRulesEffectsProcessor(
255253

256254
function processHideOptionGroup(effect: ProgramRuleEffect): any {
257255
return createEffectsForConfiguredDataTypes(effect, () => ({
258-
type: effectActions.HIDE_OPTION_GROUP as any,
256+
type: effectActions.HIDE_OPTION_GROUP,
259257
optionGroupId: effect.optionGroupId,
260258
}));
261259
}
262260

263261
function processHideOption(effect: ProgramRuleEffect): any {
264262
return createEffectsForConfiguredDataTypes(effect, () => ({
265-
type: effectActions.HIDE_OPTION as any,
263+
type: effectActions.HIDE_OPTION,
266264
optionId: effect.optionId,
267265
}));
268266
}
269267

270268
function processShowOptionGroup(effect: ProgramRuleEffect): any {
271269
return createEffectsForConfiguredDataTypes(effect, () => ({
272-
type: effectActions.SHOW_OPTION_GROUP as any,
270+
type: effectActions.SHOW_OPTION_GROUP,
273271
optionGroupId: effect.optionGroupId,
274272
}));
275273
}
@@ -300,9 +298,9 @@ export function getRulesEffectsProcessor(
300298
}: {
301299
effects: Array<ProgramRuleEffect>,
302300
dataElements: DataElements | null,
303-
trackedEntityAttributes: TrackedEntityAttributes | null,
301+
trackedEntityAttributes?: TrackedEntityAttributes | null,
304302
formValues?: { [key: string]: any } | null,
305-
onProcessValue?: (value: any, type: keyof typeof typeKeys) => any,
303+
onProcessValue?: (value: any, type: typeof typeKeys[keyof typeof typeKeys]) => any,
306304
}): OutputEffects {
307305
return effects
308306
.filter(({ action }) => mapActionsToProcessor[action])

packages/rules-engine/src/rulesEngine.types.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ import type {
1111
import { effectActions, rulesEngineEffectTargetDataTypes } from './constants';
1212

1313
export type OutputEffect = {
14-
type: keyof typeof effectActions,
14+
type: typeof effectActions[keyof typeof effectActions],
1515
id: string,
16-
targetDataType?: keyof typeof rulesEngineEffectTargetDataTypes,
16+
targetDataType?: typeof rulesEngineEffectTargetDataTypes[keyof typeof rulesEngineEffectTargetDataTypes],
1717
content?: string,
1818
name?: string,
1919
hadValue?: boolean,
2020
};
2121

22+
export type DisplayTextEffect = OutputEffect & {
23+
displayText: string,
24+
};
25+
26+
export type DisplayKeyValuePairEffect = OutputEffect & {
27+
displayKeyValuePair: { key: string, value: any },
28+
};
29+
2230
export type OutputEffects = Array<OutputEffect>;
2331

2432
export type AssignOutputEffect = OutputEffect & {
@@ -77,16 +85,16 @@ export type ProgramRuleAction = {
7785
programStageId?: string | null,
7886
programStageSectionId?: string | null,
7987
trackedEntityAttributeId?: string | null,
80-
optionGroupId: string | null,
81-
optionId: string | null,
88+
optionGroupId?: string | null,
89+
optionId?: string | null,
8290
style?: any,
83-
name: string,
91+
name?: string,
8492
};
8593

8694
export type ProgramRule = {
8795
id: string,
8896
name: string,
89-
priority: number,
97+
priority?: number,
9098
condition: string,
9199
description?: string | null,
92100
displayName: string, // TODO: Refactor and remove
@@ -115,9 +123,9 @@ export type RuleVariable = {
115123
useCodeForOptionSet: boolean,
116124
variableType: string,
117125
hasValue: boolean,
118-
variableEventDate: string | null,
126+
variableEventDate?: string | null,
119127
variablePrefix: string,
120-
allValues: Array<any> | null,
128+
allValues?: Array<any> | null,
121129
};
122130

123131
export type RuleVariables = { [key: string]: RuleVariable };

0 commit comments

Comments
 (0)