Skip to content

Commit

Permalink
Merge pull request #122 from ssi-dk/ejl/fix-searchTranslation
Browse files Browse the repository at this point in the history
Ejl/fix search translation
  • Loading branch information
sjkp authored Nov 29, 2024
2 parents 5b7c697 + 673d2d4 commit 15a1960
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions web/src/SAP/src/services/search/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def coerce_term(term: str):


def structure_wildcard(field, node):
if field in pii_columns():
if field == "cpr_nr": # because cpr is saved as a string in the DB, so don't convert even when consists of only digits
return node.term
return coerce_term(node.term)
coerced = coerce_term(node.term)
if isinstance(coerced, str):
return check_for_wildcard(field, coerced)
Expand All @@ -78,45 +82,22 @@ def structure_wildcard(field, node):
return {"$in": [coerced, node.term]}


def is_date_string(value):
try:
datetime.fromisoformat(value)
return True
except ValueError:
return False

def is_float_string(value):
try:
float(value)
return True
except ValueError:
return False

def convert_type(value):
if is_float_string(value):
return float(value)
elif value.isdigit():
return int(value)
elif is_date_string(value):
return datetime.fromisoformat(value)
return value


def structure_ranged(field, node):
min_op = "$gte" if node.inclusive == "left" or node.inclusive == "both" else "$gt"
max_op = "$lte" if node.inclusive == "right" or node.inclusive == "both" else "$lt"
if node.term_min == "*":
return {max_op: convert_type(node.term_max)}
if node.term_max == "*":
return {min_op: convert_type(node.term_min)}

#if the hours, minutes and seconds are not in the search like this 2022-04-08T09:01:07 it is assumed that the entire day is intended to be included
#default with the specific time of day not specified it is as if they are 00, which would exclude all records from during that day, which is not the behavior we expect is wanted
max_term = convert_type(node.term_max)
#default with the specific time of day not specified it is as if they are 00, which would exclude all records from during that day, which is not the behavior we expect is wanted
max_term = coerce_term(node.term_max)
if type(max_term) == datetime and max_op =="$lte" and max_term.hour == 0 and max_term.minute == 0 and max_term.second == 0:
max_term = max_term + timedelta(days = 1 ) - timedelta(seconds = 1)

return {min_op: convert_type(node.term_min), max_op: max_term}
if node.term_min == "*":
return {max_op: max_term}
if node.term_max == "*":
return {min_op: coerce_term(node.term_min)}

return {min_op: coerce_term(node.term_min), max_op: max_term}


def structure_leaf(node, is_negated):
Expand Down

0 comments on commit 15a1960

Please sign in to comment.