Skip to content

Commit

Permalink
fix: fixed age range in patient demographic - Ref gestion-de-projet#2148
Browse files Browse the repository at this point in the history
  • Loading branch information
ManelleG committed Jun 29, 2023
1 parent 9bffc0a commit 4572501
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,6 @@ const CriteriaCardContent: React.FC<CriteriaCardContentProps> = ({ 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) {
Expand Down Expand Up @@ -297,27 +292,17 @@ const CriteriaCardContent: React.FC<CriteriaCardContentProps> = ({ currentCriter
className={classes.criteriaChip}
label={
<Typography style={{ maxWidth: 500 }} noWrap>
{`
${_currentCriteria.years?.[0]} ${ageUnit}
${_currentCriteria.years?.[0] === 130 ? ' ou plus' : ''}
`}
{displayCalendarFields(
'Âge',
_currentCriteria.years?.[0],
_currentCriteria.ageType?.requestLabel,
_currentCriteria.years?.[1],
_currentCriteria.ageType?.requestLabel
)}
</Typography>
}
/>
),
!!_currentCriteria.years &&
_currentCriteria.years[0] !== _currentCriteria.years[1] &&
!(_currentCriteria.years[0] === 0 && _currentCriteria.years[1] === 130 && ageUnit === 'an(s)') && (
<Chip
className={classes.criteriaChip}
label={
<Typography style={{ maxWidth: 500 }} noWrap>
{`Entre ${_currentCriteria.years[0]} et ${_currentCriteria.years[1]} ${ageUnit}
${_currentCriteria.years[1] === 130 ? ' ou plus' : ''}`}
</Typography>
}
/>
)
)
]
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -60,7 +60,7 @@ const DemographicForm: React.FC<DemographicFormProps> = (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)
Expand Down Expand Up @@ -239,13 +239,14 @@ const DemographicForm: React.FC<DemographicFormProps> = (props) => {

<Autocomplete
id="criteria-ageType-autocomplete"
disableClearable
className={classes.inputItem}
options={[
{ id: 'year', label: 'années' },
{ id: 'month', label: 'mois' },
{ id: 'day', label: 'jours' }
{ id: Calendar.YEAR, criteriaLabel: CalendarLabel.YEAR, requestLabel: CalendarRequestLabel.YEAR },
{ id: Calendar.MONTH, criteriaLabel: CalendarLabel.MONTH, requestLabel: CalendarRequestLabel.MONTH },
{ id: Calendar.DAY, criteriaLabel: CalendarLabel.DAY, requestLabel: CalendarRequestLabel.DAY }
]}
getOptionLabel={(option) => option.label}
getOptionLabel={(option) => option.criteriaLabel}
isOptionEqualToValue={(option, value) => option.id === value.id}
value={defaultValues.ageType}
onChange={(e, value) => _onChangeValue('ageType', value)}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
80 changes: 25 additions & 55 deletions src/utils/cohortCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = [
Expand All @@ -218,7 +217,8 @@ const constructFilterFhir = (criterion: SelectedCriteriaType): string => {
.reduce(searchReducer)}`
: ''
}`,
`${ageFilter ? `${ageFilter}` : ''}`
`${ageMin ? `${ageMin}` : ''}`,
`${ageMax ? `${ageMax}` : ''}`
]
.filter((elem) => elem)
.reduce(filterReducer)
Expand Down Expand Up @@ -786,45 +786,15 @@ export async function unbuildRequest(_json: string): Promise<any> {
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
}
Expand Down

0 comments on commit 4572501

Please sign in to comment.