Skip to content

Commit 448ae65

Browse files
committed
eli-5779 fixing person level application of function
1 parent 5ef2949 commit 448ae65

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/eligibility_signposting_api/services/processors/derived_values/add_days_handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ def _find_source_date(self, context: DerivedValueContext) -> str | None:
108108
if not source_attr:
109109
return None
110110

111+
# For PERSON-level attributes, look for ATTRIBUTE_TYPE == "PERSON"
112+
# For TARGET-level attributes, look for ATTRIBUTE_TYPE == context.attribute_name (e.g., "COVID")
113+
attribute_type_to_match = "PERSON" if context.attribute_level == "PERSON" else context.attribute_name
114+
111115
for attribute in context.person_data:
112-
if attribute.get("ATTRIBUTE_TYPE") == context.attribute_name:
116+
if attribute.get("ATTRIBUTE_TYPE") == attribute_type_to_match:
113117
return attribute.get(source_attr)
114118

115119
return None

src/eligibility_signposting_api/services/processors/derived_values/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ class DerivedValueContext:
99
1010
Attributes:
1111
person_data: List of person attribute dictionaries
12-
attribute_name: The condition/vaccine type (e.g., 'COVID', 'RSV')
12+
attribute_name: The condition/vaccine type (e.g., 'COVID', 'RSV') or person attribute (e.g., 'DATE_OF_BIRTH')
1313
source_attribute: The source attribute to derive from (e.g., 'LAST_SUCCESSFUL_DATE')
1414
function_args: Arguments passed to the function (e.g., number of days)
1515
date_format: Optional date format string for output formatting
16+
attribute_level: The level of the attribute ('TARGET' or 'PERSON')
1617
"""
1718

1819
person_data: list[dict[str, Any]]
1920
attribute_name: str
2021
source_attribute: str | None
2122
function_args: str | None
2223
date_format: str | None
24+
attribute_level: str = "TARGET"
2325

2426

2527
class DerivedValueHandler(ABC):

src/eligibility_signposting_api/services/processors/token_processor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def get_derived_value(
167167
source_attribute=source_attribute,
168168
function_args=parsed_token.function_args,
169169
date_format=parsed_token.format,
170+
attribute_level=parsed_token.attribute_level,
170171
)
171172

172173
return registry.calculate(

tests/unit/services/processors/test_token_processor.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,47 @@ def test_non_derived_token_with_valid_target_attribute_works(self):
608608
result = TokenProcessor.find_and_replace_tokens(person, condition)
609609

610610
assert result.status_text == "Last date: 20260128"
611+
612+
def test_person_level_attribute_with_add_days_without_explicit_source(self):
613+
"""Test that ADD_DAYS works on PERSON-level attributes without explicit source."""
614+
person = Person(
615+
[
616+
{"ATTRIBUTE_TYPE": "PERSON", "DATE_OF_BIRTH": "19900327"},
617+
]
618+
)
619+
620+
condition = Condition(
621+
condition_name=ConditionName("Test"),
622+
status=Status.actionable,
623+
status_text=StatusText("Future date: [[PERSON.DATE_OF_BIRTH:ADD_DAYS(91)]]"),
624+
cohort_results=[],
625+
suitability_rules=[],
626+
actions=[],
627+
)
628+
629+
result = TokenProcessor.find_and_replace_tokens(person, condition)
630+
631+
# 1990-03-27 + 91 days = 1990-06-26
632+
assert result.status_text == "Future date: 19900626"
633+
634+
def test_person_level_attribute_with_add_days_explicit_source(self):
635+
"""Test that ADD_DAYS works on PERSON-level attributes with explicit source."""
636+
person = Person(
637+
[
638+
{"ATTRIBUTE_TYPE": "PERSON", "DATE_OF_BIRTH": "19900327"},
639+
]
640+
)
641+
642+
condition = Condition(
643+
condition_name=ConditionName("Test"),
644+
status=Status.actionable,
645+
status_text=StatusText("Future date: [[PERSON.DATE_OF_BIRTH:ADD_DAYS(91, DATE_OF_BIRTH)]]"),
646+
cohort_results=[],
647+
suitability_rules=[],
648+
actions=[],
649+
)
650+
651+
result = TokenProcessor.find_and_replace_tokens(person, condition)
652+
653+
# 1990-03-27 + 91 days = 1990-06-26
654+
assert result.status_text == "Future date: 19900626"

0 commit comments

Comments
 (0)