diff --git a/src/services/aphp/callApi.ts b/src/services/aphp/callApi.ts index ae5f89b96..749f0d7f8 100644 --- a/src/services/aphp/callApi.ts +++ b/src/services/aphp/callApi.ts @@ -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' @@ -21,7 +23,6 @@ import { DocumentReference, Encounter, Extension, - Group, ImagingStudy, MedicationAdministration, MedicationRequest, @@ -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 => { - 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>(`/Group?${options.reduce(paramsReducer)}`) - - return response -} - -export const deleteGroup = async (groupID: string) => { - return await apiFhir.delete(`/Group/${groupID}`) -} - /** * Patient Resource * @@ -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>(`/cohort/cohorts/?fhir_group_id=${cohortId}`) + return response +} diff --git a/src/services/aphp/serviceCohorts.ts b/src/services/aphp/serviceCohorts.ts index 26ca63acf..8fc0d57b7 100644 --- a/src/services/aphp/serviceCohorts.ts +++ b/src/services/aphp/serviceCohorts.ts @@ -3,7 +3,6 @@ import moment from 'moment' import { CohortComposition, CohortData, - Back_API_Response, Cohort, AgeRepartitionType, GenderRepartitionType, @@ -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' @@ -203,8 +203,7 @@ const servicesCohorts: IServiceCohorts = { fetchCohort: async (cohortId) => { try { const fetchCohortsResults = await Promise.all([ - apiBackend.get>(`/cohort/cohorts/?fhir_group_id=${cohortId}`), - fetchGroup({ _id: cohortId }), + fetchCohortInfo(cohortId), fetchPatient({ pivotFacet: ['age-month_gender', 'deceased_gender'], _list: [cohortId], @@ -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 = '' @@ -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) @@ -288,7 +280,6 @@ const servicesCohorts: IServiceCohorts = { return { name, description, - cohort, totalPatients, originalPatients, genderRepartitionMap, @@ -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, diff --git a/src/services/aphp/servicePatients.ts b/src/services/aphp/servicePatients.ts index b0972acc6..44e92797e 100644 --- a/src/services/aphp/servicePatients.ts +++ b/src/services/aphp/servicePatients.ts @@ -8,7 +8,6 @@ import { } from 'utils/graphUtils' import { getApiResponseResources } from 'utils/apiHelpers' import { - fetchGroup, fetchPatient, fetchEncounter, fetchClaim, @@ -18,7 +17,8 @@ import { fetchMedicationRequest, fetchMedicationAdministration, fetchObservation, - fetchImaging + fetchImaging, + fetchPerimeterFromCohortId } from './callApi' import servicesPerimeters from './servicePerimeters' @@ -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 diff --git a/src/services/aphp/servicePerimeters.ts b/src/services/aphp/servicePerimeters.ts index c862f07fb..9e487231d 100644 --- a/src/services/aphp/servicePerimeters.ts +++ b/src/services/aphp/servicePerimeters.ts @@ -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' @@ -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 @@ -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(','), @@ -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 @@ -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,