Skip to content

Commit

Permalink
refactor: remove and replace all call to fhir resource Group - Ref ge…
Browse files Browse the repository at this point in the history
…stion-de-projet#2379

* refactor: remove and replace all call to fhir resource Group - Ref gestion-de-projet#2379

* refactor: remove api call from services and put them into callApi.ts - Ref gestion-de-projet#2379
  • Loading branch information
Mehdi-BOUYAHIA authored Dec 7, 2023
1 parent 8910cea commit 7aa1087
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 80 deletions.
52 changes: 23 additions & 29 deletions src/services/aphp/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
FHIR_Bundle_Promise_Response,
HierarchyElement,
HierarchyElementWithSystem,
IScope
IScope,
Back_API_Response,
Cohort
} from 'types'

import { FHIR_Bundle_Response } from 'types'
Expand All @@ -21,7 +23,6 @@ import {
DocumentReference,
Encounter,
Extension,
Group,
ImagingStudy,
MedicationAdministration,
MedicationRequest,
Expand Down Expand Up @@ -50,33 +51,6 @@ const paramsReducer = (accumulator: string, currentValue: string): string =>

const uniq = (item: string, index: number, array: string[]) => array.indexOf(item) === index && item

/**
* Group Resource
*
*/

type fetchGroupProps = {
_id?: string | (string | undefined)[] // ID of Group
managingEntity?: string[] // ID List of organization
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const fetchGroup = async (args: fetchGroupProps): FHIR_Bundle_Promise_Response<Group> => {
const { _id, managingEntity } = args

let options: string[] = []
if (_id) options = [...options, `_id=${_id}`] // eslint-disable-line
if (managingEntity && managingEntity.length > 0)
options = [...options, `managing-entity=${managingEntity.filter(uniq).reduce(paramValuesReducer)}`] // eslint-disable-line

const response = await apiFhir.get<FHIR_Bundle_Response<Group>>(`/Group?${options.reduce(paramsReducer)}`)

return response
}

export const deleteGroup = async (groupID: string) => {
return await apiFhir.delete(`/Group/${groupID}`)
}

/**
* Patient Resource
*
Expand Down Expand Up @@ -1000,3 +974,23 @@ export const fetchAccessExpirations: (
)
return response
}

export const fetchPerimeterAccesses = async (perimeter: string) => {
const response = await apiBackend.get(`accesses/accesses/my-rights/?care-site-ids=${perimeter}`)
return response
}

export const fetchCohortAccesses = async (cohortIds: string[]) => {
const response = await apiBackend.get(`cohort/cohorts/cohort-rights/?fhir_group_id=${cohortIds}`)
return response
}

export const fetchPerimeterFromCohortId = async (cohortId: string) => {
const response = await apiBackend.get(`accesses/perimeters/?cohort_id=${cohortId}`)
return response
}

export const fetchCohortInfo = async (cohortId: string) => {
const response = await apiBackend.get<Back_API_Response<Cohort>>(`/cohort/cohorts/?fhir_group_id=${cohortId}`)
return response
}
29 changes: 11 additions & 18 deletions src/services/aphp/serviceCohorts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import moment from 'moment'
import {
CohortComposition,
CohortData,
Back_API_Response,
Cohort,
AgeRepartitionType,
GenderRepartitionType,
Expand All @@ -19,13 +18,14 @@ import {
import { getApiResponseResource, getApiResponseResources } from 'utils/apiHelpers'

import {
fetchGroup,
fetchPatient,
fetchEncounter,
fetchDocumentReference,
fetchDocumentReferenceContent,
fetchBinary,
fetchCheckDocumentSearchInput
fetchCheckDocumentSearchInput,
fetchCohortInfo,
fetchCohortAccesses
} from './callApi'

import apiBackend from '../apiBackend'
Expand Down Expand Up @@ -203,8 +203,7 @@ const servicesCohorts: IServiceCohorts = {
fetchCohort: async (cohortId) => {
try {
const fetchCohortsResults = await Promise.all([
apiBackend.get<Back_API_Response<Cohort>>(`/cohort/cohorts/?fhir_group_id=${cohortId}`),
fetchGroup({ _id: cohortId }),
fetchCohortInfo(cohortId),
fetchPatient({
pivotFacet: ['age-month_gender', 'deceased_gender'],
_list: [cohortId],
Expand All @@ -221,9 +220,8 @@ const servicesCohorts: IServiceCohorts = {
])

const cohortInfo = fetchCohortsResults[0]
const cohortResp = fetchCohortsResults[1]
const patientsResp = fetchCohortsResults[2]
const encountersResp = fetchCohortsResults[3]
const patientsResp = fetchCohortsResults[1]
const encountersResp = fetchCohortsResults[2]

let name = ''
let description = ''
Expand All @@ -240,15 +238,9 @@ const servicesCohorts: IServiceCohorts = {
favorite = cohortInfo.data.results[0].favorite ?? false
uuid = cohortInfo.data.results[0].uuid ?? ''
} else {
throw new Error('This cohort is not your or invalid')
throw new Error('This cohort is not yours or invalid')
}

if (!name) {
name = cohortResp.data.resourceType === 'Bundle' ? cohortResp.data.entry?.[0].resource?.name ?? '-' : '-'
}

const cohort = cohortResp.data.resourceType === 'Bundle' ? cohortResp.data.entry?.[0].resource : undefined

const totalPatients = patientsResp.data.resourceType === 'Bundle' ? patientsResp.data.total : 0

const originalPatients = getApiResponseResources(patientsResp)
Expand Down Expand Up @@ -288,7 +280,6 @@ const servicesCohorts: IServiceCohorts = {
return {
name,
description,
cohort,
totalPatients,
originalPatients,
genderRepartitionMap,
Expand Down Expand Up @@ -559,9 +550,11 @@ const servicesCohorts: IServiceCohorts = {

fetchCohortsRights: async (cohorts) => {
try {
const ids = cohorts.map((cohort) => cohort.fhir_group_id).filter((id) => id !== '')
const ids = cohorts
.map((cohort) => cohort.fhir_group_id)
.filter((id) => id !== '' || id !== undefined) as string[]
if (ids.length === 0) return []
const rightsResponse = await apiBackend.get(`cohort/cohorts/cohort-rights/?fhir_group_id=${ids}`)
const rightsResponse = await fetchCohortAccesses(ids)
return cohorts.map((cohort) => {
return {
...cohort,
Expand Down
19 changes: 7 additions & 12 deletions src/services/aphp/servicePatients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from 'utils/graphUtils'
import { getApiResponseResources } from 'utils/apiHelpers'
import {
fetchGroup,
fetchPatient,
fetchEncounter,
fetchClaim,
Expand All @@ -18,7 +17,8 @@ import {
fetchMedicationRequest,
fetchMedicationAdministration,
fetchObservation,
fetchImaging
fetchImaging,
fetchPerimeterFromCohortId
} from './callApi'

import servicesPerimeters from './servicePerimeters'
Expand Down Expand Up @@ -745,24 +745,19 @@ const servicesPatients: IServicePatients = {
},

fetchRights: async (groupId) => {
const groups = await fetchGroup({ _id: groupId })
const groupsData = getApiResponseResources(groups)
const perimeter = await fetchPerimeterFromCohortId(groupId)

if (!groupsData) return false
const isPerimeter = perimeter.data.results

const isPerimeter = groupsData.some((group) => group.managingEntity?.display?.search('Organization/') !== -1)

if (isPerimeter) {
const perimeterRights = await servicesPerimeters.fetchPerimetersRights(groupsData)
if (isPerimeter.length > 0) {
const perimeterRights = await servicesPerimeters.fetchPerimetersRights(isPerimeter)
return perimeterRights.some((perimeterRight) =>
perimeterRight.extension?.some(
({ url, valueString }) => url === 'READ_ACCESS' && valueString === 'DATA_PSEUDOANONYMISED'
)
)
} else {
const cohortRights = await servicesCohorts.fetchCohortsRights(
groupsData.map((groupData) => ({ fhir_group_id: groupData.id ?? '' }))
)
const cohortRights = await servicesCohorts.fetchCohortsRights([{ fhir_group_id: groupId }])
return cohortRights?.[0]?.rights?.read_patient_pseudo
? cohortRights?.[0]?.rights?.read_patient_nomi
? false
Expand Down
38 changes: 17 additions & 21 deletions src/services/aphp/servicePerimeters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import {
getVisitRepartitionMapAphp
} from 'utils/graphUtils'

import { fetchAccessExpirations, fetchEncounter, fetchGroup, fetchPatient, fetchScope } from './callApi'
import {
fetchAccessExpirations,
fetchEncounter,
fetchPatient,
fetchPerimeterAccesses,
fetchPerimeterFromCohortId,
fetchScope
} from './callApi'

import { AxiosResponse } from 'axios'
import { Group } from 'fhir/r4'
Expand Down Expand Up @@ -174,7 +181,7 @@ const servicesPerimeters: IServicePerimeters = {
.filter((item: any, index: number, array: any[]) => item && array.indexOf(item) === index)
.join(',')

const rightResponse = await apiBackend.get(`accesses/accesses/my-rights/?care-site-ids=${caresiteIds}`)
const rightResponse = await fetchPerimeterAccesses(caresiteIds)
const rightsData = (rightResponse.data as any[]) ?? []

let allowSearchIpp = false
Expand All @@ -189,10 +196,8 @@ const servicesPerimeters: IServicePerimeters = {
},

fetchPerimetersInfos: async (perimetersId) => {
const [perimetersResp, patientsResp, encountersResp] = await Promise.all([
fetchGroup({
_id: perimetersId
}),
const [djangoResponse, patientsResp, encountersResp] = await Promise.all([
fetchPerimeterFromCohortId(perimetersId),
fetchPatient({
pivotFacet: ['age-month_gender', 'deceased_gender'],
_list: perimetersId.split(','),
Expand All @@ -208,7 +213,9 @@ const servicesPerimeters: IServicePerimeters = {
})
])

const cohort = await servicesPerimeters.fetchPerimetersRights(getApiResponseResources(perimetersResp) ?? [])
const perimeters = djangoResponse.data.results

const cohort = await servicesPerimeters.fetchPerimetersRights(perimeters)

const totalPatients = patientsResp?.data?.resourceType === 'Bundle' ? patientsResp.data.total : 0

Expand Down Expand Up @@ -400,24 +407,13 @@ const servicesPerimeters: IServicePerimeters = {
},

fetchPerimetersRights: async (perimeters) => {
const caresiteIds = perimeters
.map((perimeter) =>
perimeter.managingEntity?.display?.search('Organization/') !== -1
? perimeter.managingEntity?.display?.replace('Organization/', '')
: ''
)
.filter((item: any, index: number, array: any[]) => item && array.indexOf(item) === index)
.join(',')
const caresiteIds = perimeters.map((perimeter) => perimeter.id).join(',')

const rightResponse = await apiBackend.get(`accesses/accesses/my-rights/?care-site-ids=${caresiteIds}`)
const rightResponse = await fetchPerimeterAccesses(caresiteIds)
const rightsData = (rightResponse.data as any[]) ?? []

return perimeters.map((perimeter) => {
const caresiteId =
perimeter.managingEntity?.display?.search('Organization/') !== -1
? perimeter.managingEntity?.display?.replace('Organization/', '')
: ''
const foundRight = rightsData.find((rightData) => rightData.care_site_id === +(caresiteId ?? '0'))
const foundRight = rightsData.find((rightData) => rightData.care_site_id === +(perimeter.id ?? '0'))

return {
...perimeter,
Expand Down

0 comments on commit 7aa1087

Please sign in to comment.