Skip to content

Commit

Permalink
fix: fixed error in age criteria - Ref gestion-de-projet#2454
Browse files Browse the repository at this point in the history
  • Loading branch information
ManelleG committed Jan 18, 2024
1 parent a4321ef commit c08bdb4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/utils/age.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export const convertDurationToTimestamp = (duration: DurationType | null): numbe
return year * 365 + month * 30 + day
}

export const convertTimestampToDuration = (timestamp: number | null, type: Calendar): DurationType => {
export const convertTimestampToDuration = (timestamp: number | null): DurationType => {
const duration: DurationType = { year: 130, month: 0, day: 0 }
if (!timestamp) return duration
duration.year = type === Calendar.MONTH ? Math.floor(timestamp / 12) : Math.floor(timestamp / 365)
timestamp = type === Calendar.MONTH ? timestamp % 12 : timestamp % 365
duration.month = type === Calendar.MONTH ? Math.floor(timestamp / 1) : Math.floor(timestamp / 30)
timestamp = type === Calendar.MONTH ? timestamp % 1 : timestamp % 30
duration.year = Math.floor(timestamp / 365)
timestamp = timestamp % 365
duration.month = Math.floor(timestamp / 30)
timestamp = timestamp % 30
duration.day = timestamp
return duration
}
36 changes: 15 additions & 21 deletions src/utils/cohortCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ import {
PROCEDURE_HIERARCHY
} from '../constants'
import { DocumentAttachmentMethod, LabelObject, SearchByTypes } from 'types/searchCriterias'
import { Calendar } from 'types/dates'
import { convertDurationToString, convertTimestampToDuration } from './age'
import { Comparators, RessourceType, SelectedCriteriaType, CriteriaDataKey } from 'types/requestCriterias'
import { parseOccurence } from './valueComparator'
import { parseDocumentAttachment } from './documentAttachment'
import {
buildAgeFilter,
buildComparatorFilter,
buildDateFilter,
buildDurationFilter,
Expand Down Expand Up @@ -207,7 +204,10 @@ const constructFilterFhir = (criterion: SelectedCriteriaType, deidentified: bool
buildDateFilter(criterion.deathDates[0], PATIENT_DEATHDATE, 'ge'),
buildDateFilter(criterion.deathDates[1], PATIENT_DEATHDATE, 'le'),
criterion.birthdates[0] === null && criterion.birthdates[1] === null
? buildAgeFilter(criterion.age, deidentified ? PATIENT_AGE_MONTH : PATIENT_AGE_DAY, deidentified)
? buildDurationFilter(criterion.age[0], deidentified ? PATIENT_AGE_MONTH : PATIENT_AGE_DAY, 'ge')
: '',
criterion.birthdates[1] === null && criterion.birthdates[1] === null
? buildDurationFilter(criterion.age[1], deidentified ? PATIENT_AGE_MONTH : PATIENT_AGE_DAY, 'le')
: ''
]
.filter((elem) => elem)
Expand All @@ -216,6 +216,8 @@ const constructFilterFhir = (criterion: SelectedCriteriaType, deidentified: bool
}

case RessourceType.ENCOUNTER: {
deidentified = false //TODO erase this line when deidentified param for encounter is implemented

filterFhir = [
'subject.active=true',
buildLabelObjectFilter(criterion.admissionMode, ENCOUNTER_ADMISSIONMODE),
Expand All @@ -231,7 +233,8 @@ const constructFilterFhir = (criterion: SelectedCriteriaType, deidentified: bool
buildEncounterServiceFilter(criterion.encounterService, SERVICE_PROVIDER),
buildDurationFilter(criterion?.duration?.[0], ENCOUNTER_DURATION, 'ge'),
buildDurationFilter(criterion?.duration?.[1], ENCOUNTER_DURATION, 'le'),
buildAgeFilter(criterion.age, ENCOUNTER_MIN_BIRTHDATE, deidentified, ENCOUNTER_MAX_BIRTHDATE)
buildDurationFilter(criterion?.age[0], deidentified ? ENCOUNTER_MIN_BIRTHDATE : ENCOUNTER_MIN_BIRTHDATE, 'ge'),
buildDurationFilter(criterion?.age[1], deidentified ? ENCOUNTER_MAX_BIRTHDATE : ENCOUNTER_MAX_BIRTHDATE, 'le')
]
.filter((elem) => elem)
.reduce(filterReducer)
Expand Down Expand Up @@ -584,21 +587,12 @@ export async function unbuildRequest(_json: string): Promise<any> {
const value = filter ? filter[1] : null

switch (key) {
case PATIENT_AGE_DAY: {
if (value?.includes('ge')) {
const ageMin = value?.replace('ge', '')
currentCriterion.age[0] = convertDurationToString(convertTimestampToDuration(+ageMin, Calendar.DAY))
} else if (value?.includes('le')) {
const ageMax = value?.replace('le', '')
currentCriterion.age[1] = convertDurationToString(convertTimestampToDuration(+ageMax, Calendar.DAY))
}
break
}
case PATIENT_AGE_DAY:
case PATIENT_AGE_MONTH: {
if (value?.includes('ge')) {
currentCriterion.age[0] = unbuildDurationFilter(value, Calendar.MONTH)
currentCriterion.age[0] = unbuildDurationFilter(value)
} else if (value?.includes('le')) {
currentCriterion.age[1] = unbuildDurationFilter(value, Calendar.MONTH)
currentCriterion.age[1] = unbuildDurationFilter(value)
}
break
}
Expand Down Expand Up @@ -666,18 +660,18 @@ export async function unbuildRequest(_json: string): Promise<any> {
switch (key) {
case ENCOUNTER_DURATION: {
if (value.includes('ge')) {
currentCriterion.duration[0] = unbuildDurationFilter(value, Calendar.DAY)
currentCriterion.duration[0] = unbuildDurationFilter(value)
} else if (value.includes('le')) {
currentCriterion.duration[1] = unbuildDurationFilter(value, Calendar.DAY)
currentCriterion.duration[1] = unbuildDurationFilter(value)
}
break
}
case ENCOUNTER_MIN_BIRTHDATE: {
currentCriterion.age[0] = unbuildDurationFilter(value, Calendar.DAY)
currentCriterion.age[0] = unbuildDurationFilter(value)
break
}
case ENCOUNTER_MAX_BIRTHDATE: {
currentCriterion.age[1] = unbuildDurationFilter(value, Calendar.DAY)
currentCriterion.age[1] = unbuildDurationFilter(value)
break
}
case ENCOUNTER_ENTRYMODE: {
Expand Down
27 changes: 4 additions & 23 deletions src/utils/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { CriteriaNameType, CriteriaName, ScopeTreeRow } from 'types'
import { DocumentAttachmentMethod, DurationRangeType, LabelObject } from 'types/searchCriterias'
import { DocumentAttachmentMethod, LabelObject } from 'types/searchCriterias'
import {
convertDurationToString,
convertDurationToTimestamp,
convertStringToDuration,
convertTimestampToDuration,
substructAgeString
convertTimestampToDuration
} from './age'
import docTypes from 'assets/docTypes.json'
import { OBSERVATION_VALUE, RequeteurCriteriaType } from './cohortCreation'
Expand All @@ -24,7 +23,6 @@ import {
} from 'types/requestCriterias'
import { comparatorToFilter, parseOccurence } from './valueComparator'
import services from 'services/aphp'
import { Calendar } from 'types/dates'

const searchReducer = (accumulator: any, currentValue: any): string =>
accumulator || accumulator === false ? `${accumulator},${currentValue}` : currentValue ? currentValue : accumulator
Expand Down Expand Up @@ -107,9 +105,9 @@ export const buildDurationFilter = (age: string | null | undefined, fhirKey: str
return `${fhirKey}=${comparator}${convertedRange}`
}

export const unbuildDurationFilter = (value: string, type: Calendar) => {
export const unbuildDurationFilter = (value: string) => {
const cleanValue = value?.replace(comparator, '')
return convertDurationToString(convertTimestampToDuration(+cleanValue, type))
return convertDurationToString(convertTimestampToDuration(+cleanValue))
}

export const buildSearchFilter = (criterion: string, fhirKey: string) => {
Expand Down Expand Up @@ -186,23 +184,6 @@ export const unbuildDocTypesFilter = (currentCriterion: any, filterName: string,
}
}

export const buildAgeFilter = (
age: DurationRangeType,
fhirKey: string,
deidentified: boolean,
fhirKeyMaxValue?: string
) => {
const ageToBirthdate: [string, string] = [
moment(substructAgeString(age?.[0] || '0/0/0')).format('MM/DD/YYYY'),
moment(substructAgeString(age?.[1] || '0/0/130')).format('MM/DD/YYYY')
]

const ageMin = Math.abs(moment(ageToBirthdate[0]).diff(moment(), deidentified ? 'months' : 'days'))
const ageMax = Math.abs(moment(ageToBirthdate[1]).diff(moment(), deidentified ? 'months' : 'days'))

return `${fhirKey}=ge${ageMin}&${fhirKeyMaxValue ? fhirKeyMaxValue : fhirKey}=le${ageMax}`
}

export const unbuildAdvancedCriterias = (
element: RequeteurCriteriaType,
currentCriterion:
Expand Down

0 comments on commit c08bdb4

Please sign in to comment.