From 1fbeaad021c3847631b589c59bc7a7b3e64edbb2 Mon Sep 17 00:00:00 2001 From: Christos Paschalidis Date: Wed, 21 Apr 2021 12:15:36 +0100 Subject: [PATCH] fix: DHIS2-10723 port on 2.36.0 (#1630) * fix: DHIS2-10723 (#1622) * fix: geometry * chore: returns the magic * chore: covers all cases --- .../RegistrationDataEntry.epics.js | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js index ec876ea018..fbda28192c 100644 --- a/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js +++ b/src/core_modules/capture-core/components/Pages/New/RegistrationDataEntry/RegistrationDataEntry.epics.js @@ -12,10 +12,41 @@ import { navigateToTrackedEntityDashboard } from '../../../../utils/navigateToTr import { getTrackerProgramThrowIfNotFound, scopeTypes } from '../../../../metaData'; +const geometryType = (key) => { + const types = ['Point', 'None', 'Polygon']; + return types.find(type => key.toLowerCase().includes(type.toLowerCase())); +}; + +const standardGeoJson = (geometry) => { + if (!geometry) { + return undefined; + } + if (Array.isArray(geometry)) { + return { + type: 'Polygon', + coordinates: geometry, + }; + } else if (geometry.longitude && geometry.latitude) { + return { + type: 'Point', + coordinates: [geometry.longitude, geometry.latitude], + }; + } + return undefined; +}; + + const deriveAttributesFromFormValues = (formValues = {}) => Object.keys(formValues) + .filter(key => !geometryType(key)) .map(key => ({ attribute: key, value: formValues[key] })); +const deriveGeometryFromFormValues = (formValues = {}) => + Object.keys(formValues) + .filter(key => geometryType(key)) + .reduce((acc, currentKey) => (standardGeoJson(formValues[currentKey])), undefined); + + const deriveEvents = ({ stages, enrollmentDate, incidentDate, programId, orgUnitId }) => { // in case we have a program that does not have an incident date, such as Malaria case diagnosis, // we want the incident to default to enrollmentDate @@ -65,6 +96,7 @@ export const startSavingNewTrackedEntityInstanceEpic: Epic = (action$: InputObse return saveNewTrackedEntityInstance( { attributes: deriveAttributesFromFormValues(values), + geometry: deriveGeometryFromFormValues(values), enrollments: [], orgUnit: orgUnitId, trackedEntityType: trackedEntityTypeId, @@ -96,16 +128,19 @@ export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = (acti ofType(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START), map(() => { const { currentSelections: { orgUnitId, programId }, formsValues, dataEntriesFieldsValue } = store.value; - const { incidentDate, enrollmentDate } = dataEntriesFieldsValue['newPageDataEntryId-newEnrollment'] || { }; + const { incidentDate, enrollmentDate, geometry } = dataEntriesFieldsValue['newPageDataEntryId-newEnrollment'] || { }; const { trackedEntityType, stages } = getTrackerProgramThrowIfNotFound(programId); - const values = formsValues['newPageDataEntryId-newEnrollment']; + const values = formsValues['newPageDataEntryId-newEnrollment'] || {}; const events = deriveEvents({ stages, enrollmentDate, incidentDate, programId, orgUnitId }); + return saveNewTrackedEntityInstanceWithEnrollment( { attributes: deriveAttributesFromFormValues(values), + geometry: deriveGeometryFromFormValues(values), enrollments: [ { + geometry: standardGeoJson(geometry), incidentDate, enrollmentDate, program: programId,