From ac6c491211634d72c4742b4892cc4a1764fc3b89 Mon Sep 17 00:00:00 2001 From: Beate Quednau Date: Fri, 20 Sep 2024 18:46:50 +0200 Subject: [PATCH] Fix temperature queries with available options --- .../searchModal/forms/DetailSearch.js | 9 ++++--- .../searchModal/forms/SearchModalFunctions.js | 2 +- .../search/conditions_for_advanced_search.rb | 27 ++++++++++--------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/packs/src/components/searchModal/forms/DetailSearch.js b/app/packs/src/components/searchModal/forms/DetailSearch.js index 30f971b232..1f58e7ce4d 100644 --- a/app/packs/src/components/searchModal/forms/DetailSearch.js +++ b/app/packs/src/components/searchModal/forms/DetailSearch.js @@ -725,13 +725,14 @@ const DetailSearch = () => { } const availableOptionsForTemperature = (searchValue, startValue, startUnit) => { - startValue = startValue.replace(/,/g, '.'); - startValue = startValue.slice(-1) === '.' ? `${startValue}0` : startValue; + startValue = startValue.match(/^-?\d+(\.\d+)?$/g); + + if (startValue === null || isNaN(Number(startValue))) { return searchValue; } searchValue.available_options = []; - searchValue.available_options.push({ value: startValue, unit: startUnit }); + searchValue.available_options.push({ value: startValue[0], unit: startUnit }); - let [convertedValue, convertedUnit] = convertTemperature(startValue, startUnit); + let [convertedValue, convertedUnit] = convertTemperature(startValue[0], startUnit); searchValue.available_options.push({ value: convertedValue.trim(), unit: convertedUnit }); [convertedValue, convertedUnit] = convertTemperature(convertedValue, convertedUnit); diff --git a/app/packs/src/components/searchModal/forms/SearchModalFunctions.js b/app/packs/src/components/searchModal/forms/SearchModalFunctions.js index 2765282d23..b44af3b514 100644 --- a/app/packs/src/components/searchModal/forms/SearchModalFunctions.js +++ b/app/packs/src/components/searchModal/forms/SearchModalFunctions.js @@ -137,7 +137,7 @@ const searchValuesByFilters = (store) => { let table = val.field.table || val.table; let value = val.value; table = table.charAt(0).toUpperCase() + table.slice(1, -1).replace('_', ' '); - value = value != true ? value.replace(/[\n\r]/g, ' OR ') : value; + value = value && value !== true ? value.replace(/[\n\r]/g, ' OR ') : value; if (val.field.sub_fields && val.field.sub_fields.length >= 1 && val.sub_values.length >= 1) { let values = searchValuesBySubFields(val, table); diff --git a/app/usecases/search/conditions_for_advanced_search.rb b/app/usecases/search/conditions_for_advanced_search.rb index db785c1aa3..2790e0a5e4 100644 --- a/app/usecases/search/conditions_for_advanced_search.rb +++ b/app/usecases/search/conditions_for_advanced_search.rb @@ -189,12 +189,11 @@ def special_non_generic_field_options(filter) when 'temperature' regex_number = "'^-{0,1}\\d+(\\.\\d+){0,1}\\Z'" is_data_valid = "(#{@table}.temperature ->> 'userText' ~ #{regex_number})" - - @conditions[:field] = - "CASE WHEN #{is_data_valid} THEN (#{@table}.temperature ->> 'userText')::FLOAT ELSE -30000 END " - - @conditions[:first_condition] += " (#{@table}.temperature ->> 'valueUnit')::TEXT != '' AND " - @conditions[:additional_condition] = "AND (#{@table}.temperature ->> 'valueUnit')::TEXT = '#{filter['unit']}'" + field = filter['field']['column'] + @conditions[:field] = "(#{@table}.temperature ->> 'userText')::FLOAT" + @conditions[:first_condition] += + " (#{@table}.temperature ->> 'valueUnit')::TEXT != '' AND #{is_data_valid} AND " + unit_and_available_options_conditions(filter, "#{@table}.temperature", field, 'userText', 'valueUnit') @conditions[:condition_table] = '' when 'duration' time_divisor = duration_interval_by_unit(filter['unit']) @@ -380,7 +379,7 @@ def dataset_tab_options(filter, number) @conditions[:additional_condition] = "AND (#{prop} ->> 'field')::TEXT = '#{field}'" if filter['unit'].present? || filter['available_options'].present? - unit_and_available_options_conditions(filter, prop, field) + unit_and_available_options_conditions(filter, prop, field, 'value', 'value_system') end end end @@ -389,10 +388,14 @@ def remove_degree_from_unit(filter) filter['unit'].remove('°').remove(/ \(.*\)/).tr('/', '_') end - def unit_and_available_options_conditions(filter, prop, field) + def remove_degree_from_property(prop, unit) + "LOWER(replace((#{prop} ->> '#{unit}')::TEXT, '°', ''))" + end + + def unit_and_available_options_conditions(filter, prop, field, number, unit) if filter['unit'].present? @conditions[:additional_condition] += - " AND LOWER((#{prop} ->> 'value_system')::TEXT) = LOWER('#{remove_degree_from_unit(filter)}')" + " AND #{remove_degree_from_property(prop, unit)} = LOWER('#{remove_degree_from_unit(filter)}')" end return if filter['available_options'].blank? @@ -403,10 +406,10 @@ def unit_and_available_options_conditions(filter, prop, field) next if option[:unit] == filter['unit'] @conditions[:additional_condition] += - " OR ((#{prop} ->> 'value')::TEXT >= '#{option[:value]}' - AND LOWER((#{prop} ->> 'value_system')::TEXT) = LOWER('#{remove_degree_from_unit(filter)}'))" + " OR ((#{prop} ->> '#{number}')::TEXT >= '#{option[:value]}' + AND #{remove_degree_from_property(prop, unit)} = LOWER('#{remove_degree_from_unit(option)}'))" else - conditions += " AND (#{prop} ->> 'value')::TEXT NOT ILIKE '%#{option[:value]}%'" + conditions += " AND (#{prop} ->> '#{number}')::TEXT NOT ILIKE '%#{option[:value]}%'" end end