Skip to content

Commit

Permalink
fix: [DHIS2-17531] use new image endpoint in search tracked entity re…
Browse files Browse the repository at this point in the history
…sults (#3673)
  • Loading branch information
simonadomnisoru authored Jul 3, 2024
1 parent 3e18c0f commit 184ce8d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ const searchTei = ({
getTrackerProgram(selectedProgramId).attributes :
getTrackedEntityType(selectedTrackedEntityTypeId).attributes;

return from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource)).pipe(
return from(
getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource, selectedProgramId),
).pipe(
map(({ trackedEntityInstanceContainers, pagingData }) =>
searchTeiResultRetrieved(
{ trackedEntityInstanceContainers, currentPage: pagingData.currentPage },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ export const loadSearchGroupDuplicatesForReviewEpic = (
...contextParam,
};
const attributes = getAttributesFromScopeId(selectedScopeId);
const programId = scopeType === scopeTypes.TRACKER_PROGRAM ? selectedScopeId : null;

const stream$: Stream = from(
getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource),
getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource, programId),
);
return stream$.pipe(
map(({ trackedEntityInstanceContainers: searchResults, pagingData }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const searchViaUniqueIdStream = ({
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
}) =>
from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource)).pipe(
from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource, programId)).pipe(
flatMap(({ trackedEntityInstanceContainers }) => {
const searchResults = trackedEntityInstanceContainers;
if (searchResults.length > 0) {
Expand Down Expand Up @@ -92,8 +92,22 @@ const handleErrors = ({ httpStatusCode, message }) => {
return of(showErrorViewOnSearchBox());
};

const searchViaAttributesStream = ({ queryArgs, attributes, triggeredFrom, absoluteApiPath, querySingleResource }) =>
from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource)).pipe(
const searchViaAttributesStream = ({
queryArgs,
attributes,
triggeredFrom,
absoluteApiPath,
querySingleResource,
programId,
}: {
queryArgs: any,
attributes: any,
triggeredFrom: string,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
programId?: string,
}) =>
from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource, programId)).pipe(
map(({ trackedEntityInstanceContainers: searchResults, pagingData }) => {
if (searchResults.length > 0) {
return showSuccessResultsViewOnSearchBox(
Expand Down Expand Up @@ -204,6 +218,7 @@ export const searchViaAttributesOnScopeProgramEpic = (
triggeredFrom,
absoluteApiPath,
querySingleResource,
programId,
});
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ const searchTei = ({
getTrackerProgram(selectedProgramId).attributes :
getTrackedEntityType(selectedTrackedEntityTypeId).attributes;

return from(getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource)).pipe(
return from(
getTrackedEntityInstances(queryArgs, attributes, absoluteApiPath, querySingleResource, selectedProgramId),
).pipe(
map(({ trackedEntityInstanceContainers, pagingData }) =>
searchTeiResultRetrieved(
{ trackedEntityInstanceContainers, currentPage: pagingData.currentPage },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
// @flow
import log from 'loglevel';
import isDefined from 'd2-utilizr/lib/isDefined';
import { errorCreator } from 'capture-core-utils';
import { featureAvailable, FEATURES } from 'capture-core-utils';
import { type DataElement, dataElementTypes } from '../metaData';
import type { QuerySingleResource } from '../utils/api/api.types';

const GET_SUBVALUE_ERROR = 'Could not get subvalue';

const subValueGetterByElementType = {
[dataElementTypes.IMAGE]: ({
value,
teiId,
attributeId,
absoluteApiPath,
querySingleResource,
programId,
}: {
value: any,
teiId: string,
attributeId: string,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
}) =>
querySingleResource({ resource: `fileResources/${value}` })
.then(res =>
({
name: res.name,
value: res.id,
url: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/image`,
}))
.catch((error) => {
log.warn(errorCreator(GET_SUBVALUE_ERROR)({ value, teiId, attributeId, error }));
return null;
}) };
programId: ?string,
}) => {
const buildUrl = () => {
if (featureAvailable(FEATURES.trackerImageEndpoint)) {
if (programId) {
return `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}&dimension=small`;
}
return `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?dimension=small`;
}
return `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/image`;
};
const previewUrl = buildUrl();

return {
previewUrl,
url: previewUrl,
};
},
};

export async function getSubValues({
teiId,
attributes,
values,
absoluteApiPath,
querySingleResource,
programId,
}: {
teiId: string,
attributes: Array<DataElement>,
values?: ?Object,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
programId: ?string,
}) {
if (!values) {
return null;
Expand All @@ -67,6 +69,7 @@ export async function getSubValues({
attributeId,
absoluteApiPath,
querySingleResource,
programId,
});
accValues[attributeId] = subValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function convertToClientTei(
attributes: Array<DataElement>,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
programId: ?string,
) {
const attributeValuesById = getValuesById(apiTei.attributes);
const convertedAttributeValues = convertDataElementsValues(attributeValuesById, attributes, convertValue);
Expand All @@ -44,6 +45,7 @@ async function convertToClientTei(
values: convertedAttributeValues,
absoluteApiPath,
querySingleResource,
programId,
});

return {
Expand All @@ -63,6 +65,7 @@ export async function getTrackedEntityInstances(
attributes: Array<DataElement>,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
selectedProgramId: ?string,
): TrackedEntityInstancesPromise {
const apiResponse = await querySingleResource({
resource: 'tracker/trackedEntities',
Expand All @@ -72,7 +75,13 @@ export async function getTrackedEntityInstances(

const trackedEntityInstanceContainers = await apiTrackedEntities.reduce(async (accTeiPromise, apiTei) => {
const accTeis = await accTeiPromise;
const teiContainer = await convertToClientTei(apiTei, attributes, absoluteApiPath, querySingleResource);
const teiContainer = await convertToClientTei(
apiTei,
attributes,
absoluteApiPath,
querySingleResource,
selectedProgramId,
);
if (teiContainer) {
accTeis.push(teiContainer);
}
Expand Down

0 comments on commit 184ce8d

Please sign in to comment.