Skip to content

Commit

Permalink
fix: [DHIS2-15888] multi text element in program rules (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru authored Jan 23, 2024
1 parent 3254686 commit fd0776d
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const normalizeRuleVariable = (data: any, valueType: string) => {
[typeKeys.AGE]: convertString,
[typeKeys.TEXT]: convertString,
[typeKeys.LONG_TEXT]: convertString,
[typeKeys.MULTI_TEXT]: convertString,
};

if (!data && data !== 0 && data !== false) {
Expand Down
1 change: 1 addition & 0 deletions packages/rules-engine/src/constants/typeKeys.const.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
export const typeKeys = {
TEXT: 'TEXT',
MULTI_TEXT: 'MULTI_TEXT',
LONG_TEXT: 'LONG_TEXT',
LETTER: 'LETTER',
PHONE_NUMBER: 'PHONE_NUMBER',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { typeKeys } from './typeKeys.const';

export const mapTypeToInterfaceFnName = {
[typeKeys.TEXT]: 'convertText',
[typeKeys.MULTI_TEXT]: 'convertMultiText',
[typeKeys.LONG_TEXT]: 'convertLongText',
[typeKeys.LETTER]: 'convertLetter',
[typeKeys.PHONE_NUMBER]: 'convertPhoneNumber',
Expand Down
7 changes: 7 additions & 0 deletions packages/rules-engine/src/d2Functions/getD2Functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,11 @@ export const getD2Functions = ({
return params[0];
},
},
multiTextContains: {
parameters: 2,
execute: (params: any) => {
log.warn('multiTextContains not implemented yet');
return params[0];
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ export type D2Functions = $ReadOnly<{|
lastEventDate: D2FunctionConfig,
addControlDigits: D2FunctionConfig,
checkControlDigits: D2FunctionConfig,
multiTextContains: D2FunctionConfig,
|}>;
15 changes: 14 additions & 1 deletion packages/rules-engine/src/helpers/OptionSetHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// @flow
import { typeKeys } from '../constants';
import type { Option } from '../services/VariableService';

export class OptionSetHelper {
static getName(options, key) {
static getName(options: Array<Option>, key: any, dataElementValueType: string) {
if (options) {
if (dataElementValueType === typeKeys.MULTI_TEXT) {
const values = key.split(',');
const optionsName = options.reduce(
(acc, option) => (values.includes(option.code) ? acc.concat(option.displayName) : acc),
[],
);
return optionsName.length !== 0 ? optionsName.join(',') : key;
}

const option = options.find(o => o.code === key);
return (option && option.displayName) || key;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rules-engine/src/rulesEngine.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export type IDateUtils = {|

export type IConvertInputRulesValue = {|
convertText(value: any): ?string,
convertMultiText(value: any): ?string,
convertLongText(value: any): ?string,
convertLetter(value: any): ?string,
convertPhoneNumber(value: any): ?string,
Expand Down Expand Up @@ -203,6 +204,7 @@ export type IConvertInputRulesValue = {|

export type IConvertOutputRulesEffectsValue = {|
convertText(value: string): any,
convertMultiText(value: string): any,
convertLongText(value: string): any,
convertLetter(value: string): any,
convertPhoneNumber(value: string): any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class VariableService {
) {
const value = this.onProcessValue(rawValue, valueType);
return (value !== null && useNameForOptionSet && dataElements && dataElements[dataElementId] && dataElements[dataElementId].optionSetId) ?
OptionSetHelper.getName(optionSets[dataElements[dataElementId].optionSetId].options, value) :
OptionSetHelper.getName(optionSets[dataElements[dataElementId].optionSetId].options, value, dataElements[dataElementId].valueType) :
value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type {
EventsData,
TEIValues,
Enrollment,
Option,
OptionSet,
OptionSets,
CompareDates,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type Enrollment = {
+enrollmentId?: string,
};

type Option = {
export type Option = {
id: string,
code: string,
displayName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const styles = theme => ({
const MULTI_TEXT_SEPARATOR = ',';

const MultiSelectFieldComponentPlain = (props: Props) => {
const { onSelect, options, value = '', translations, onFocus, onBlur } = props;
const { onSelect, options, value = '', translations, onFocus, onBlur, disabled } = props;
const [selected, setSelected] = useState([]);

useEffect(() => {
Expand Down Expand Up @@ -54,6 +54,7 @@ const MultiSelectFieldComponentPlain = (props: Props) => {
filterable
filterPlaceholder={translations.filterPlaceholder}
noMatchText={translations.noMatchText}
disabled={disabled}
>
{options.map(option => (
<MultiSelectOption key={option.id} label={option.label} value={option.value} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Props = {
onBlur: (value: ?string) => void,
options: Array<MultiSelectOptionConfig>,
value?: string,
disabled: boolean,
translations: {
filterPlaceholder: string,
noMatchText: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ test('Post process rules effects', () => {
},
{ type: 'HIDEPROGRAMSTAGE', id: 'PUZaKR0Jh2k' },
{ id: 'SWfdBhglX0fk', type: 'HIDESECTION' },
{ id: 'w75KJ2mc4zz', type: 'ASSIGN', value: 'true' },
{ id: 'wasdJ2mc4zz', type: 'ASSIGN', value: 'true' },
{ id: 'w75KJ2mc4zz', type: 'ASSIGN', value: 'value1' },
{ id: 'wasdJ2mc4zz', type: 'ASSIGN', value: 32 },
{ id: 'pjsdJ2mc4zz', type: 'ASSIGN', value: 'value1,value2' },
{ id: 'ky6dJ2mc4zz', type: 'ASSIGN', value: 'value1,value2' },
{
id: 'w75KJ2mc4zz',
message: ' true',
Expand Down Expand Up @@ -63,7 +65,8 @@ test('Post process rules effects', () => {
id: 'optionSet',
name: 'optionSet',
code: 'optionSet',
options: [{ option: { value: false } }],
options: [{ value: 'value1' }],
dataElement: { type: dataElementTypes.TEXT },
};
});

Expand All @@ -73,8 +76,37 @@ test('Post process rules effects', () => {
o.type = dataElementTypes.NUMBER;
o.compulsory = true;
});

const dataElement3 = new DataElement((o) => {
o.id = 'pjsdJ2mc4zz';
o.name = 'dataElement3';
o.type = dataElementTypes.MULTI_TEXT;
o.optionSet = {
id: 'optionSet3',
name: 'optionSet3',
code: 'optionSet3',
options: [{ value: 'value1' }, { value: 'value2' }],
dataElement: { type: dataElementTypes.MULTI_TEXT },
};
});

const dataElement4 = new DataElement((o) => {
o.id = 'ky6dJ2mc4zz';
o.name = 'dataElement4';
o.type = dataElementTypes.MULTI_TEXT;
o.optionSet = {
id: 'optionSet4',
name: 'optionSet4',
code: 'optionSet4',
options: [{ value: 'value1' }, { value: 'notAMatch' }],
dataElement: { type: dataElementTypes.MULTI_TEXT },
useNameForOptionSet: true,
};
});
initSection.addElement(dataElement1);
initSection.addElement(dataElement2);
initSection.addElement(dataElement3);
initSection.addElement(dataElement4);
});

const section2 = new Section((initSection) => {
Expand Down Expand Up @@ -149,12 +181,22 @@ test('Post process rules effects', () => {
{
id: 'w75KJ2mc4zz',
type: 'ASSIGN',
value: null,
value: 'value1',
},
{
id: 'wasdJ2mc4zz',
type: 'ASSIGN',
value: 'true',
value: 32,
},
{
id: 'pjsdJ2mc4zz',
type: 'ASSIGN',
value: 'value1,value2',
},
{
id: 'ky6dJ2mc4zz',
type: 'ASSIGN',
value: null,
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ describe('Event rules engine', () => {
ovY6E8BSdto: { id: 'ovY6E8BSdto', valueType: 'TEXT', optionSetId: 'dsgBmIZ0Yrq' },
Z5z8vFQy0w0: { id: 'Z5z8vFQy0w0', valueType: 'TEXT' },
TzqawmlPkI5: { id: 'TzqawmlPkI5', valueType: 'TEXT', optionSetId: 'L6eMZDJkCwX' },
leqawmlPkI5: { id: 'leqawmlPkI5', valueType: 'MULTI_TEXT', optionSetId: 'Jy0pZDJkCwX' },
f8j4XDEozvj: { id: 'f8j4XDEozvj', valueType: 'TEXT', optionSetId: 'xD9QOIvNAjw' },
jBBkFuPKctq: { id: 'jBBkFuPKctq', valueType: 'TEXT', optionSetId: 'T9zjyaIkRqH' },
A4Fg6jgWauf: { id: 'A4Fg6jgWauf', valueType: 'TEXT', optionSetId: 'w1vUkxq8IOl' },
Expand Down Expand Up @@ -715,6 +716,14 @@ describe('Event rules engine', () => {
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'rapvdmYrwLb',
dataElementId: 'leqawmlPkI5',
displayName: 'HISTORY',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'JPIyrAmJapV',
dataElementId: 'CUbZcLm9LyN',
Expand Down Expand Up @@ -823,6 +832,31 @@ describe('Event rules engine', () => {
},
],
},
Jy0pZDJkCwX: {
id: 'Jy0pZDJkCwX',
displayName: 'Yes/No/Unknown',
version: 3,
valueType: 'MULTI_TEXT',
options: [
{
id: 'x9yVKkv9koc',
displayName: 'Yes',
code: 'Yes',
translations: [],
},
{
id: 'R98tI2c6rF5',
displayName: 'No',
code: 'No',
translations: [],
},
{
id: 'pqxvAQU1z9W',
code: 'Unknown',
translations: [],
},
],
},
dsgBmIZ0Yrq: {
id: 'dsgBmIZ0Yrq',
displayName: 'Test Result',
Expand Down Expand Up @@ -984,7 +1018,7 @@ describe('Event rules engine', () => {
],
],
[
{ QQLXTXVidW2: 'Yes' },
{ QQLXTXVidW2: 'Yes', leqawmlPkI5: 'Yes,No' },
[
{
type: 'ASSIGN',
Expand Down Expand Up @@ -1059,7 +1093,7 @@ describe('Event rules engine', () => {
],
],
[
{ QQLXTXVidW2: 'No' },
{ QQLXTXVidW2: 'No', leqawmlPkI5: 'Yes,No' },
[
{
type: 'ASSIGN',
Expand Down Expand Up @@ -2561,9 +2595,22 @@ describe('Assign effects', () => {
mjus9Dvnyt5: { id: 'mjus9Dvnyt5', valueType: 'FILE_RESOURCE' },
fgrr9Dvnyt5: { id: 'fgrr9Dvnyt5', valueType: 'IMAGE' },
oZ3fhkd9taw: { id: 'oZ3fhkd9taw', valueType: 'UNKNOWN' },
hyur9Dvnyt5: { id: 'hyur9Dvnyt5', valueType: 'MULTI_TEXT', optionSetId: 'pC3N9N77UmT' },
ght5r9Dnyt5: { id: 'ght5r9Dnyt5', valueType: 'MULTI_TEXT', optionSetId: 'pC3N9N77UmT' },
};
const orgUnit = { id: 'DiszpKrYNg8', name: 'Ngelehun CHC' };
const optionSets = {};
const optionSets = {
pC3N9N77UmT: {
id: 'pC3N9N77UmT',
displayName: 'Gender',
version: 0,
valueType: 'MULTI_TEXT',
options: [
{ id: 'rBvjJYbMCVx', displayName: 'Male', code: 'Male', translations: [] },
{ id: 'Mnp3oXrpAbK', displayName: 'Female', code: 'Female', translations: [] },
],
dataElement: { type: 'MULTI_TEXT' },
} };
const currentEvent = {};

test('Assign effect corner cases', () => {
Expand Down Expand Up @@ -2677,6 +2724,18 @@ describe('Assign effects', () => {
dataElementId: 'jhrr9Dvnyt5',
programRuleActionType: 'ASSIGN',
},
{
id: 'juyur9Dvnyt5',
data: "'Male,Female'",
dataElementId: 'hyur9Dvnyt5',
programRuleActionType: 'ASSIGN',
},
{
id: 'gty5r9Dvnyt5',
data: "'Female,Male,Female'",
dataElementId: 'ght5r9Dnyt5',
programRuleActionType: 'ASSIGN',
},
{
id: 'lopr9Dvnyt5',
data: 'username',
Expand Down Expand Up @@ -2826,6 +2885,18 @@ describe('Assign effects', () => {
type: 'ASSIGN',
value: '-30',
},
{
id: 'hyur9Dvnyt5',
targetDataType: 'dataElement',
type: 'ASSIGN',
value: 'Male,Female',
},
{
id: 'ght5r9Dnyt5',
targetDataType: 'dataElement',
type: 'ASSIGN',
value: 'Female,Male',
},
{
id: 'plor9Dvnyt5',
targetDataType: 'dataElement',
Expand Down
Loading

0 comments on commit fd0776d

Please sign in to comment.