Skip to content

Commit

Permalink
Status linked enrollment (#89)
Browse files Browse the repository at this point in the history
* Allow individual enrollment to use status specific eligibility

* Minor refactoring for improved readability & consistency

* Update group enrollment to use status specific eligibility
  • Loading branch information
weilu authored Jul 30, 2024
1 parent 9f19b40 commit 61bfe4b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 85 deletions.
47 changes: 22 additions & 25 deletions src/components/EnrollmentGroupHeadPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,22 @@ class EnrollmentGroupHeadPanel extends FormPanel {
};

getDefaultAppliedCustomFilters = () => {
const { jsonExt } = this.props?.edited ?? {};
try {
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
return advancedCriteria.map(({ custom_filter_condition }) => {
const [field, filter, typeValue] = custom_filter_condition.split('__');
const [type, value] = typeValue.split('=');
return {
custom_filter_condition,
field,
filter,
type,
value,
};
});
} catch (error) {
return [];
}
const benefitPlan = this.props?.edited;
const jsonExt = benefitPlan?.jsonExt ?? '{}';
const status = benefitPlan?.status;
const jsonData = JSON.parse(jsonExt);
const filters = jsonData.advanced_criteria?.[status] || [];
return filters.map(({ custom_filter_condition }) => {
const [field, filter, typeValue] = custom_filter_condition.split('__');
const [type, value] = typeValue.split('=');
return {
custom_filter_condition,
field,
filter,
type,
value,
};
});
};

setAppliedCustomFilters = (appliedCustomFilters) => {
Expand All @@ -71,7 +69,6 @@ class EnrollmentGroupHeadPanel extends FormPanel {
render() {
// eslint-disable-next-line no-unused-vars
const { edited, classes, intl } = this.props;
const enrollment = { ...edited };
const { appliedCustomFilters, appliedFiltersRowStructure } = this.state;
return (
<>
Expand All @@ -83,7 +80,7 @@ class EnrollmentGroupHeadPanel extends FormPanel {
required
filterLabels={false}
onChange={(benefitPlan) => this.updateAttribute('benefitPlan', benefitPlan)}
value={enrollment?.benefitPlan}
value={edited?.benefitPlan}
type="GROUP"
/>
</Grid>
Expand All @@ -94,7 +91,7 @@ class EnrollmentGroupHeadPanel extends FormPanel {
withNull={false}
filterLabels={false}
onChange={(status) => this.updateAttribute('status', status)}
value={enrollment?.status}
value={edited?.status}
/>
</Grid>
</Grid>
Expand All @@ -107,8 +104,8 @@ class EnrollmentGroupHeadPanel extends FormPanel {
<Divider />
<Grid container className={classes.item}>
<AdvancedCriteriaGroupForm
object={enrollment.benefitPlan}
objectToSave={enrollment}
object={edited.benefitPlan}
objectToSave={edited}
moduleName="individual"
objectType="Individual"
setAppliedCustomFilters={this.setAppliedCustomFilters}
Expand All @@ -117,8 +114,8 @@ class EnrollmentGroupHeadPanel extends FormPanel {
setAppliedFiltersRowStructure={this.setAppliedFiltersRowStructure}
updateAttributes={this.updateJsonExt}
getDefaultAppliedCustomFilters={this.getDefaultAppliedCustomFilters}
additionalParams={enrollment?.benefitPlan ? { benefitPlan: `${decodeId(enrollment.benefitPlan.id)}` } : null}
edited={this.props.edited}
additionalParams={edited?.benefitPlan ? { benefitPlan: `${decodeId(edited.benefitPlan.id)}` } : null}
edited={edited}
/>
</Grid>
</>
Expand Down
47 changes: 22 additions & 25 deletions src/components/EnrollmentHeadPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,22 @@ class EnrollmentHeadPanel extends FormPanel {
};

getDefaultAppliedCustomFilters = () => {
const { jsonExt } = this.props?.edited ?? {};
try {
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
return advancedCriteria.map(({ custom_filter_condition }) => {
const [field, filter, typeValue] = custom_filter_condition.split('__');
const [type, value] = typeValue.split('=');
return {
custom_filter_condition,
field,
filter,
type,
value,
};
});
} catch (error) {
return [];
}
const benefitPlan = this.props?.edited;
const jsonExt = benefitPlan?.jsonExt ?? '{}';
const status = benefitPlan?.status;
const jsonData = JSON.parse(jsonExt);
const filters = jsonData.advanced_criteria?.[status] || [];
return filters.map(({ custom_filter_condition }) => {
const [field, filter, typeValue] = custom_filter_condition.split('__');
const [type, value] = typeValue.split('=');
return {
custom_filter_condition,
field,
filter,
type,
value,
};
});
};

setAppliedCustomFilters = (appliedCustomFilters) => {
Expand All @@ -71,7 +69,6 @@ class EnrollmentHeadPanel extends FormPanel {
render() {
// eslint-disable-next-line no-unused-vars
const { edited, classes, intl } = this.props;
const enrollment = { ...edited };
const { appliedCustomFilters, appliedFiltersRowStructure } = this.state;
return (
<>
Expand All @@ -83,7 +80,7 @@ class EnrollmentHeadPanel extends FormPanel {
required
filterLabels={false}
onChange={(benefitPlan) => this.updateAttribute('benefitPlan', benefitPlan)}
value={enrollment?.benefitPlan}
value={edited?.benefitPlan}
/>
</Grid>
<Grid item xs={3} className={classes.item}>
Expand All @@ -93,7 +90,7 @@ class EnrollmentHeadPanel extends FormPanel {
withNull={false}
filterLabels={false}
onChange={(status) => this.updateAttribute('status', status)}
value={enrollment?.status}
value={edited?.status}
/>
</Grid>
</Grid>
Expand All @@ -106,8 +103,8 @@ class EnrollmentHeadPanel extends FormPanel {
<Divider />
<Grid container className={classes.item}>
<AdvancedCriteriaForm
object={enrollment.benefitPlan}
objectToSave={enrollment}
object={edited.benefitPlan}
objectToSave={edited}
moduleName="individual"
objectType="Individual"
setAppliedCustomFilters={this.setAppliedCustomFilters}
Expand All @@ -116,8 +113,8 @@ class EnrollmentHeadPanel extends FormPanel {
setAppliedFiltersRowStructure={this.setAppliedFiltersRowStructure}
updateAttributes={this.updateJsonExt}
getDefaultAppliedCustomFilters={this.getDefaultAppliedCustomFilters}
additionalParams={enrollment?.benefitPlan ? { benefitPlan: `${decodeId(enrollment.benefitPlan.id)}` } : null}
edited={this.props.edited}
additionalParams={edited?.benefitPlan ? { benefitPlan: `${decodeId(edited.benefitPlan.id)}` } : null}
edited={edited}
/>
</Grid>
</>
Expand Down
44 changes: 28 additions & 16 deletions src/components/dialogs/AdvancedCriteriaForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { bindActionCreators } from 'redux';
import AddCircle from '@material-ui/icons/Add';
import Typography from '@material-ui/core/Typography';
import AdvancedCriteriaRowValue from './AdvancedCriteriaRowValue';
import { CLEARED_STATE_FILTER, INDIVIDUAL } from '../../constants';
import {
CLEARED_STATE_FILTER,
INDIVIDUAL,
DEFAULT_BENEFICIARY_STATUS,
} from '../../constants';
import { isBase64Encoded, isEmptyObject } from '../../utils';
import { confirmEnrollment, fetchIndividualEnrollmentSummary } from '../../actions';
import IndividualPreviewEnrollmentDialog from './IndividualPreviewEnrollmentDialog';
Expand Down Expand Up @@ -58,20 +62,28 @@ function AdvancedCriteriaForm({
});
const [filters, setFilters] = useState(getDefaultAppliedCustomFilters());
const [filtersToApply, setFiltersToApply] = useState(null);
const status = edited?.status;

const getBenefitPlanDefaultCriteria = () => {
const { jsonExt } = edited?.benefitPlan ?? {};
try {
const jsonData = JSON.parse(jsonExt);
return jsonData.advanced_criteria || [];
} catch (error) {
return [];
const jsonExt = edited?.benefitPlan?.jsonExt ?? '{}';
const jsonData = JSON.parse(jsonExt);

// Note: advanced_criteria is migrated from [filters] to {status: filters}
// For backward compatibility default status take on the old filters
let criteria = jsonData?.advanced_criteria || {};
if (Array.isArray(criteria)) {
criteria = { [DEFAULT_BENEFICIARY_STATUS]: criteria };
}

return criteria[status] || [];
};

useEffect(() => {
if (!getDefaultAppliedCustomFilters().length) {
const defaultAppliedCustomFilters = getDefaultAppliedCustomFilters();
if (!defaultAppliedCustomFilters.length) {
setFilters(getBenefitPlanDefaultCriteria());
} else {
setFilters(defaultAppliedCustomFilters);
}
}, [edited]);

Expand Down Expand Up @@ -104,12 +116,12 @@ function AdvancedCriteriaForm({

function updateJsonExt(inputJsonExt, outputFilters) {
const existingData = JSON.parse(inputJsonExt || '{}');
// eslint-disable-next-line no-prototype-builtins
if (!existingData.hasOwnProperty('advanced_criteria')) {
existingData.advanced_criteria = [];
}
const filterData = JSON.parse(outputFilters);
existingData.advanced_criteria = filterData;

const advancedCriteria = existingData?.advanced_criteria || {};
const updatedAdvancedCriteria = { ...advancedCriteria, [status]: filterData };
existingData.advanced_criteria = updatedAdvancedCriteria;

const updatedJsonExt = JSON.stringify(existingData);
return updatedJsonExt;
}
Expand All @@ -135,7 +147,7 @@ function AdvancedCriteriaForm({

// Parse the jsonExt string to extract advanced_criteria
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
const advancedCriteria = jsonData.advanced_criteria?.[status] || [];

// Extract custom_filter_condition values and construct customFilters array
const customFilters = advancedCriteria.map((criterion) => `"${criterion.custom_filter_condition}"`);
Expand Down Expand Up @@ -189,15 +201,15 @@ function AdvancedCriteriaForm({
);
const jsonExt = updateJsonExt(objectToSave.jsonExt, outputFilters);
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
const advancedCriteria = jsonData.advanced_criteria?.[status] || [];

// Extract custom_filter_condition values and construct customFilters array
const customFilters = advancedCriteria.map((criterion) => `"${criterion.custom_filter_condition}"`);
setFiltersToApply(customFilters);
const params = {
customFilters: `[${customFilters}]`,
benefitPlanId: `"${decodeId(object.id)}"`,
status: `"${objectToSave.status}"`,
status: `"${status}"`,
};
confirmEnrollment(
params,
Expand Down
45 changes: 28 additions & 17 deletions src/components/dialogs/AdvancedCriteriaGroupForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import { bindActionCreators } from 'redux';
import AddCircle from '@material-ui/icons/Add';
import Typography from '@material-ui/core/Typography';
import AdvancedCriteriaRowValue from './AdvancedCriteriaRowValue';
import { CLEARED_STATE_FILTER, GROUP, INDIVIDUAL } from '../../constants';
import {
CLEARED_STATE_FILTER,
INDIVIDUAL,
DEFAULT_BENEFICIARY_STATUS,
} from '../../constants';
import { isBase64Encoded, isEmptyObject } from '../../utils';
import { confirmGroupEnrollment, fetchGroupEnrollmentSummary } from '../../actions';
import IndividualPreviewEnrollmentDialog from './IndividualPreviewEnrollmentDialog';
import GroupPreviewEnrollmentDialog from './GroupPreviewEnrollmentDialog';

const styles = (theme) => ({
Expand Down Expand Up @@ -59,20 +62,28 @@ function AdvancedCriteriaGroupForm({
});
const [filters, setFilters] = useState(getDefaultAppliedCustomFilters());
const [filtersToApply, setFiltersToApply] = useState(null);
const status = edited?.status;

const getBenefitPlanDefaultCriteria = () => {
const { jsonExt } = edited?.benefitPlan ?? {};
try {
const jsonData = JSON.parse(jsonExt);
return jsonData.advanced_criteria || [];
} catch (error) {
return [];
const jsonExt = edited?.benefitPlan?.jsonExt ?? '{}';
const jsonData = JSON.parse(jsonExt);

// Note: advanced_criteria is migrated from [filters] to {status: filters}
// For backward compatibility default status take on the old filters
let criteria = jsonData?.advanced_criteria || {};
if (Array.isArray(criteria)) {
criteria = { [DEFAULT_BENEFICIARY_STATUS]: criteria };
}

return criteria[status] || [];
};

useEffect(() => {
if (!getDefaultAppliedCustomFilters().length) {
const defaultAppliedCustomFilters = getDefaultAppliedCustomFilters();
if (!defaultAppliedCustomFilters.length) {
setFilters(getBenefitPlanDefaultCriteria());
} else {
setFilters(defaultAppliedCustomFilters);
}
}, [edited]);

Expand Down Expand Up @@ -105,12 +116,12 @@ function AdvancedCriteriaGroupForm({

function updateJsonExt(inputJsonExt, outputFilters) {
const existingData = JSON.parse(inputJsonExt || '{}');
// eslint-disable-next-line no-prototype-builtins
if (!existingData.hasOwnProperty('advanced_criteria')) {
existingData.advanced_criteria = [];
}
const filterData = JSON.parse(outputFilters);
existingData.advanced_criteria = filterData;

const advancedCriteria = existingData?.advanced_criteria || {};
const updatedAdvancedCriteria = { ...advancedCriteria, [status]: filterData };
existingData.advanced_criteria = updatedAdvancedCriteria;

const updatedJsonExt = JSON.stringify(existingData);
return updatedJsonExt;
}
Expand All @@ -136,7 +147,7 @@ function AdvancedCriteriaGroupForm({

// Parse the jsonExt string to extract advanced_criteria
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
const advancedCriteria = jsonData.advanced_criteria?.[status] || [];

// Extract custom_filter_condition values and construct customFilters array
const customFilters = advancedCriteria.map((criterion) => `"${criterion.custom_filter_condition}"`);
Expand Down Expand Up @@ -190,15 +201,15 @@ function AdvancedCriteriaGroupForm({
);
const jsonExt = updateJsonExt(objectToSave.jsonExt, outputFilters);
const jsonData = JSON.parse(jsonExt);
const advancedCriteria = jsonData.advanced_criteria || [];
const advancedCriteria = jsonData.advanced_criteria?.[status] || [];

// Extract custom_filter_condition values and construct customFilters array
const customFilters = advancedCriteria.map((criterion) => `"${criterion.custom_filter_condition}"`);
setFiltersToApply(customFilters);
const params = {
customFilters: `[${customFilters}]`,
benefitPlanId: `"${decodeId(object.id)}"`,
status: `"${objectToSave.status}"`,
status: `"${status}"`,
};
confirmGroupEnrollment(
params,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const BENEFICIARY_STATUS = {
SUSPENDED: 'SUSPENDED',
};

export const DEFAULT_BENEFICIARY_STATUS = 'POTENTIAL';

export const GROUP_INDIVIDUAL_ROLES = {
HEAD: 'HEAD',
SPOUSE: 'SPOUSE',
Expand Down
5 changes: 4 additions & 1 deletion src/pages/EnrollmentGroupPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
journalize,
} from '@openimis/fe-core';
import EnrollmentGroupHeadPanel from '../components/EnrollmentGroupHeadPanel';
import { DEFAULT_BENEFICIARY_STATUS } from '../constants';

const useStyles = makeStyles((theme) => ({
page: theme.page,
Expand All @@ -27,7 +28,9 @@ function EnrollmentGroupPage({
const history = useHistory();
const { formatMessage } = useTranslations('individual', modulesManager);

const [editedEnrollment, setEditedEnrollment] = useState({ status: 'ACTIVE' });
const [editedEnrollment, setEditedEnrollment] = useState({
status: DEFAULT_BENEFICIARY_STATUS,
});

const back = () => history.goBack();

Expand Down
Loading

0 comments on commit 61bfe4b

Please sign in to comment.