Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [DHIS2-17537] use new endpoints for FILE #3799

Open
wants to merge 3 commits into
base: DHIS2-17530
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const FEATURES = Object.freeze({
exportablePayload: 'exportablePayload',
changelogs: 'changelogs',
trackerImageEndpoint: 'trackerImageEndpoint',
trackerFileEndpoint: 'trackerFileEndpoint',
trackedEntitiesCSV: 'trackedEntitiesCSV',
newAocApiSeparator: 'newAocApiSeparator',
});
Expand All @@ -18,6 +19,7 @@ const MINOR_VERSION_SUPPORT = Object.freeze({
[FEATURES.customIcons]: 41,
[FEATURES.exportablePayload]: 41,
[FEATURES.trackerImageEndpoint]: 41,
[FEATURES.trackerFileEndpoint]: 41,
[FEATURES.newTransferQueryParam]: 41,
[FEATURES.changelogs]: 41,
[FEATURES.trackedEntitiesCSV]: 40,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,27 @@ type SubValueFunctionParams = {
minorServerVersion: number,
};

const buildTEAFileUrl = (attribute) => {
const { absoluteApiPath, teiId, id } = attribute;
return `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/file`;
const buildTEAFileUrl = (attribute, minorServerVersion) => {
const { absoluteApiPath, teiId, programId, id } = attribute;

return hasAPISupportForFeature(minorServerVersion, FEATURES.trackerFileEndpoint)
? `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${id}/file?program=${programId}`
: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${id}/file`;
};

const getFileResourceSubvalue = async ({ attribute, querySingleResource }: SubValueFunctionParams) => {
const getFileResourceSubvalue = async ({
attribute,
querySingleResource,
minorServerVersion,
}: SubValueFunctionParams) => {
if (!attribute.value) return null;

const { id, displayName: name } = await querySingleResource({ resource: 'fileResources', id: attribute.value });
return {
id,
name,
value: id,
url: buildTEAFileUrl(attribute),
url: buildTEAFileUrl(attribute, minorServerVersion),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const getFileResourceSubvalue = async (keys: Object, querySingleResource: QueryS
return {
id,
name,
url: `${absoluteApiPath}/events/files?dataElementUid=${key}&eventUid=${eventId}`,
url: featureAvailable(FEATURES.trackerFileEndpoint)
? `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${key}/file`
: `${absoluteApiPath}/events/files?dataElementUid=${key}&eventUid=${eventId}`,
};
}
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const getFileResourceSubvalue = async ({ dataElement, querySingleResource, event
return {
id,
name,
url: `${absoluteApiPath}/events/files?dataElementUid=${dataElement.id}&eventUid=${eventId}`,
url: featureAvailable(FEATURES.trackerFileEndpoint)
? `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElement.id}/file`
: `${absoluteApiPath}/events/files?dataElementUid=${dataElement.id}&eventUid=${eventId}`,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import { dataElementTypes } from '../../../../../../../metaData';
const buildTEAUrlByElementType: {|
[string]: Function,
|} = {
[dataElementTypes.FILE_RESOURCE]: ({ trackedEntity, id }: { trackedEntity: string, id: string }) => ({
fileUrl: `/trackedEntityInstances/${trackedEntity}/${id}/file`,
}),
[dataElementTypes.FILE_RESOURCE]: ({
trackedEntity,
id,
programId,
}: {
trackedEntity: string,
id: string,
programId: string,
}) =>
(featureAvailable(FEATURES.trackerFileEndpoint)
? { fileUrl: `/tracker/trackedEntities/${trackedEntity}/attributes/${id}/file?program=${programId}` }
: { fileUrl: `/trackedEntityInstances/${trackedEntity}/${id}/file` }
),
[dataElementTypes.IMAGE]: ({
trackedEntity,
id,
Expand All @@ -31,9 +41,11 @@ const buildTEAUrlByElementType: {|
const buildDataElementUrlByElementType: {|
[string]: Function,
|} = {
[dataElementTypes.FILE_RESOURCE]: ({ event, id }: { event: string, id: string }) => ({
fileUrl: `/events/files?dataElementUid=${id}&eventUid=${event}`,
}),
[dataElementTypes.FILE_RESOURCE]: ({ event, id }: { event: string, id: string }) =>
(featureAvailable(FEATURES.trackerFileEndpoint)
? { fileUrl: `/tracker/events/${event}/dataValues/${id}/file` }
: { fileUrl: `/events/files?dataElementUid=${id}&eventUid=${event}` }
),
[dataElementTypes.IMAGE]: ({ event, id }: { event: string, id: string }) =>
(featureAvailable(FEATURES.trackerImageEndpoint)
? {
Expand Down
4 changes: 3 additions & 1 deletion src/core_modules/capture-core/events/getSubValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const subValueGetterByElementType = {
({
name: res.name,
value: res.id,
url: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}`,
url: featureAvailable(FEATURES.trackerFileEndpoint)
? `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${metaElementId}/file`
: `${absoluteApiPath}/events/files?dataElementUid=${metaElementId}&eventUid=${eventId}`,
}))
.catch((error) => {
log.warn(errorCreator(GET_SUBVALUE_ERROR)({ value, eventId, metaElementId, error }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ const subValueGetterByElementType = {
attributeId,
absoluteApiPath,
querySingleResource,
programId,
}: {
value: string,
teiId: string,
attributeId: string,
absoluteApiPath: string,
querySingleResource: QuerySingleResource,
programId: ?string,
}) =>
querySingleResource({ resource: `fileResources/${value}` })
.then(res => ({
name: res.name,
url: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/file`,
}))
.then((res) => {
const fileUrl = featureAvailable(FEATURES.trackerFileEndpoint)
? `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/file`
: `${absoluteApiPath}/trackedEntityInstances/${teiId}/${attributeId}/file`;
const url = programId ? `${fileUrl}?program=${programId}` : fileUrl;

return {
name: res.name,
url,
};
})
.catch((error) => {
log.warn(errorCreator('Could not get subvalue')({ value, teiId, attributeId, error }));
return null;
Expand Down
Loading