From 45725014ae3026adef19a8858b0a2191438d5e1f Mon Sep 17 00:00:00 2001 From: manelleg Date: Thu, 29 Jun 2023 20:17:26 +0200 Subject: [PATCH] fix: fixed age range in patient demographic - Ref gestion-de-projet#2148 --- .../CriteriaCardContent.tsx | 31 ++----- .../DemographicFrom/DemographicFrom.tsx | 15 ++-- src/types.ts | 2 +- src/utils/cohortCreation.ts | 80 ++++++------------- 4 files changed, 42 insertions(+), 86 deletions(-) diff --git a/src/components/CreationCohort/DiagramView/components/CriteriaCard/components/CriteriaCardContent/CriteriaCardContent.tsx b/src/components/CreationCohort/DiagramView/components/CriteriaCard/components/CriteriaCardContent/CriteriaCardContent.tsx index db060b533..e612eae21 100644 --- a/src/components/CreationCohort/DiagramView/components/CriteriaCard/components/CriteriaCardContent/CriteriaCardContent.tsx +++ b/src/components/CreationCohort/DiagramView/components/CriteriaCard/components/CriteriaCardContent/CriteriaCardContent.tsx @@ -243,11 +243,6 @@ const CriteriaCardContent: React.FC = ({ currentCriter } case 'Patient': { - const ageType: any = _currentCriteria.ageType ? _currentCriteria.ageType.id : 'year' - let ageUnit: 'an(s)' | 'mois' | 'jour(s)' = 'an(s)' - if (ageType === 'month') ageUnit = 'mois' - else if (ageType === 'day') ageUnit = 'jour(s)' - const displaySelectedGender = (genders: { id: string; label: string }[]) => { let currentGender: string[] = [] for (const gender of genders) { @@ -297,27 +292,17 @@ const CriteriaCardContent: React.FC = ({ currentCriter className={classes.criteriaChip} label={ - {` - ${_currentCriteria.years?.[0]} ${ageUnit} - ${_currentCriteria.years?.[0] === 130 ? ' ou plus' : ''} - `} + {displayCalendarFields( + 'Âge', + _currentCriteria.years?.[0], + _currentCriteria.ageType?.requestLabel, + _currentCriteria.years?.[1], + _currentCriteria.ageType?.requestLabel + )} } /> - ), - !!_currentCriteria.years && - _currentCriteria.years[0] !== _currentCriteria.years[1] && - !(_currentCriteria.years[0] === 0 && _currentCriteria.years[1] === 130 && ageUnit === 'an(s)') && ( - - {`Entre ${_currentCriteria.years[0]} et ${_currentCriteria.years[1]} ${ageUnit} - ${_currentCriteria.years[1] === 130 ? ' ou plus' : ''}`} - - } - /> - ) + ) ] break } diff --git a/src/components/CreationCohort/DiagramView/components/LogicalOperator/components/CriteriaRightPanel/DemographicFrom/DemographicFrom.tsx b/src/components/CreationCohort/DiagramView/components/LogicalOperator/components/CriteriaRightPanel/DemographicFrom/DemographicFrom.tsx index 8657d1301..bfb7aaa88 100644 --- a/src/components/CreationCohort/DiagramView/components/LogicalOperator/components/CriteriaRightPanel/DemographicFrom/DemographicFrom.tsx +++ b/src/components/CreationCohort/DiagramView/components/LogicalOperator/components/CriteriaRightPanel/DemographicFrom/DemographicFrom.tsx @@ -18,7 +18,7 @@ import KeyboardBackspaceIcon from '@mui/icons-material/KeyboardBackspace' import useStyles from './styles' -import { DemographicDataType } from 'types' +import { Calendar, CalendarLabel, CalendarRequestLabel, DemographicDataType } from 'types' type DemographicFormProps = { criteria: any @@ -32,7 +32,7 @@ const defaultDemographic: DemographicDataType = { title: 'Critère démographique', vitalStatus: [], gender: [], - ageType: { id: 'year', label: 'années' }, + ageType: { id: Calendar.YEAR, criteriaLabel: CalendarLabel.YEAR, requestLabel: CalendarRequestLabel.YEAR }, years: [0, 130], isInclusive: true } @@ -60,7 +60,7 @@ const DemographicForm: React.FC = (props) => { defaultValues.ageType && +defaultValues.years[0] === 0 && +defaultValues.years[1] === 130 && - defaultValues.ageType.id === 'year' + defaultValues.ageType.id === Calendar.YEAR ) { // If no input has been set return setError(true) @@ -239,13 +239,14 @@ const DemographicForm: React.FC = (props) => { option.label} + getOptionLabel={(option) => option.criteriaLabel} isOptionEqualToValue={(option, value) => option.id === value.id} value={defaultValues.ageType} onChange={(e, value) => _onChangeValue('ageType', value)} diff --git a/src/types.ts b/src/types.ts index 353951536..e83b43edf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -459,7 +459,7 @@ export type DemographicDataType = { type: 'Patient' gender: { id: string; label: string }[] | null vitalStatus: { id: string; label: string }[] | null - ageType: { id: string; label: string } | null + ageType: { id: Calendar; criteriaLabel: CalendarLabel; requestLabel: CalendarRequestLabel } years: [number, number] isInclusive?: boolean } diff --git a/src/utils/cohortCreation.ts b/src/utils/cohortCreation.ts index 2c04fd628..f4cfd4144 100644 --- a/src/utils/cohortCreation.ts +++ b/src/utils/cohortCreation.ts @@ -85,7 +85,7 @@ const DEFAULT_CRITERIA_ERROR: SelectedCriteriaType = { gender: [], vitalStatus: [], years: [0, 130], - ageType: { id: 'year', label: 'En année' } + ageType: { id: Calendar.YEAR, criteriaLabel: CalendarLabel.YEAR, requestLabel: CalendarRequestLabel.YEAR } } const DEFAULT_GROUP_ERROR: CriteriaGroupType = { @@ -187,21 +187,20 @@ const constructFilterFhir = (criterion: SelectedCriteriaType): string => { switch (criterion.type) { case RESSOURCE_TYPE_PATIENT: { - let ageFilter = '' - if (criterion.years && (criterion.years[0] !== 0 || criterion.years[1] !== 130)) { - const today = moment() - //@ts-ignore - const date1 = moment() - .subtract(criterion.years[1] + 1, criterion?.ageType?.id || 'years') - .add(1, 'days') - //@ts-ignore - const date2 = moment().subtract(criterion.years[0], criterion?.ageType?.id || 'years') - - ageFilter = - `${PATIENT_BIRTHDATE}=` + - `le${today.diff(date1, 'day')}` + - `&${PATIENT_BIRTHDATE}=` + - `ge${today.diff(date2, 'day')}` + let ageMin = '' + let ageMax = '' + + if ( + !( + criterion.years && + criterion.years[0] === 0 && + criterion.years[1] === 130 && + criterion.ageType && + criterion.ageType.id === Calendar.YEAR + ) + ) { + ageMin = `${PATIENT_BIRTHDATE}=ge${+criterion.years[0] * getCalendarMultiplicator(criterion.ageType?.id)}` + ageMax = `${PATIENT_BIRTHDATE}=le${+criterion.years[1] * getCalendarMultiplicator(criterion.ageType?.id)}` } filterFhir = [ @@ -218,7 +217,8 @@ const constructFilterFhir = (criterion: SelectedCriteriaType): string => { .reduce(searchReducer)}` : '' }`, - `${ageFilter ? `${ageFilter}` : ''}` + `${ageMin ? `${ageMin}` : ''}`, + `${ageMax ? `${ageMax}` : ''}` ] .filter((elem) => elem) .reduce(filterReducer) @@ -786,45 +786,15 @@ export async function unbuildRequest(_json: string): Promise { case PATIENT_BIRTHDATE: { currentCriterion.ageType = currentCriterion.ageType ? currentCriterion.ageType : null currentCriterion.years = currentCriterion.years ? currentCriterion.years : [0, 130] - const ageType = [ - { id: 'year', label: 'années' }, - { id: 'month', label: 'mois' }, - { id: 'day', label: 'jours' } - ] - - if (value?.search('le') === 0) { - const date = value?.replace('le', '') ? moment().subtract(value?.replace('le', ''), 'days') : null - const diff = date ? moment().diff(date, 'days') : 0 - - let currentAgeType: 'year' | 'month' | 'day' = 'year' - if (diff >= 130 && diff <= 3000) { - currentAgeType = 'month' - } else if (diff <= 130) { - currentAgeType = 'day' - } - const foundAgeType = ageType.find(({ id }) => id === currentAgeType) - currentCriterion.ageType = foundAgeType - if (date) currentCriterion.years[1] = moment().diff(date, currentAgeType) || 130 - } else if (value?.search('ge') === 0) { - const date = value?.replace('ge', '') - ? moment().subtract(+value?.replace('ge', '') + 1, 'days') - : null - const diff = date ? moment().diff(date, 'days') : 0 - - let currentAgeType: 'year' | 'month' | 'day' = 'year' - if (currentCriterion.ageType) { - currentAgeType = currentCriterion.ageType.id - } else { - if (diff >= 130 && diff <= 3000) { - currentAgeType = 'month' - } else if (diff <= 130) { - currentAgeType = 'day' - } - const foundAgeType = ageType.find(({ id }) => id === currentAgeType) - currentCriterion.ageType = currentCriterion.ageType ? currentCriterion.ageType : foundAgeType - } - currentCriterion.years[0] = moment().diff(date, currentAgeType) || 0 + if (value?.includes('ge')) { + const ageMin = value?.replace('ge', '') + currentCriterion.ageType = getCalendarType(+ageMin) + currentCriterion.years[0] = getValueFromCalendarType(currentCriterion.ageType?.id, +ageMin) + } else if (value?.includes('le')) { + const ageMax = value?.replace('le', '') + currentCriterion.ageType = getCalendarType(+ageMax) + currentCriterion.years[1] = getValueFromCalendarType(currentCriterion.ageType?.id, +ageMax) } break }