Skip to content

Commit

Permalink
Fixed data import when record has date/time keys (#3092)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Oct 19, 2023
1 parent 26d721b commit 46fc1d5
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions core/record/nodeValues.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as R from 'ramda'

import { Objects } from '@openforis/arena-core'
import { DateFormats, Dates, Objects } from '@openforis/arena-core'

import * as StringUtils from '@core/stringUtils'
import * as Survey from '@core/survey/survey'
Expand Down Expand Up @@ -37,12 +37,21 @@ const extractCategoryItemUuidFromValue = ({ survey, nodeDef, record, parentNode,
return null
}

const dateTimeComparator =
({ dateTimeFormats }) =>
({ value, valueSearch }) => {
const toISODateTime = (val) => {
const fromFormat = dateTimeFormats.find((format) => Dates.isValidDateInFormat(val, format))
return fromFormat ? Dates.convertDate(val, fromFormat, DateFormats.datetimeStorage) : null
}
const dateTime = toISODateTime(value)
const dateTimeSearch = toISODateTime(valueSearch)
return dateTime === dateTimeSearch
}

const valueComparatorByNodeDefType = {
[NodeDef.nodeDefType.boolean]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.code]: ({ survey, nodeDef, record, parentNode, value, valueSearch, strict }) => {
if (value === valueSearch) return true
if (Objects.isEmpty(value) || Objects.isEmpty(valueSearch)) return false

const itemUuid = extractCategoryItemUuidFromValue({ survey, nodeDef, record, parentNode, value, strict })
const itemUuidSearch = extractCategoryItemUuidFromValue({
survey,
Expand All @@ -55,7 +64,9 @@ const valueComparatorByNodeDefType = {
return itemUuidSearch === itemUuid
},
[NodeDef.nodeDefType.coordinate]: ({ value, valueSearch }) => R.equals(value, valueSearch),
[NodeDef.nodeDefType.date]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.date]: dateTimeComparator({
dateTimeFormats: [DateFormats.dateDisplay, DateFormats.dateStorage],
}),
[NodeDef.nodeDefType.decimal]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.integer]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.taxon]: ({ value, valueSearch }) => {
Expand All @@ -65,7 +76,9 @@ const valueComparatorByNodeDefType = {
return value[Node.valuePropsTaxon.taxonUuid] === valueSearch[Node.valuePropsTaxon.taxonUuid]
},
[NodeDef.nodeDefType.text]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.time]: singlePropValueEqualComparator,
[NodeDef.nodeDefType.time]: dateTimeComparator({
dateTimeFormats: [DateFormats.timeStorage, 'HH:mm:ss'],
}),
}

/**
Expand All @@ -82,6 +95,9 @@ const valueComparatorByNodeDefType = {
* @returns {boolean} - True if the values are equal.
*/
const isValueEqual = ({ survey, nodeDef, value, valueSearch, record = null, parentNode = null, strict = false }) => {
if (value === valueSearch) return true
if (Objects.isEmpty(value) || Objects.isEmpty(valueSearch)) return false

const valueComparator = valueComparatorByNodeDefType[NodeDef.getType(nodeDef)]
return valueComparator && valueComparator({ survey, nodeDef, record, parentNode, value, valueSearch, strict })
}
Expand Down

0 comments on commit 46fc1d5

Please sign in to comment.