@@ -27,7 +27,6 @@ class AddDaysHandler(DerivedValueHandler):
2727
2828 function_name : str = "ADD_DAYS"
2929
30- # Mapping of derived attribute names to their source attributes
3130 DERIVED_ATTRIBUTE_SOURCES : ClassVar [dict [str , str ]] = {
3231 "NEXT_DOSE_DUE" : "LAST_SUCCESSFUL_DATE" ,
3332 }
@@ -62,7 +61,6 @@ def get_source_attribute(self, target_attribute: str, function_args: str | None
6261 The source attribute name (e.g., 'LAST_SUCCESSFUL_DATE')
6362 """
6463 if function_args and "," in function_args :
65- # Extract source from args if present (second argument)
6664 parts = [p .strip () for p in function_args .split ("," )]
6765 if len (parts ) > 1 and parts [1 ]:
6866 return parts [1 ].upper ()
@@ -98,6 +96,9 @@ def calculate(self, context: DerivedValueContext) -> str:
9896 def _find_source_date (self , context : DerivedValueContext ) -> str | None :
9997 """Find the source date value from person data.
10098
99+ For PERSON/COHORT-level attributes, looks for ATTRIBUTE_TYPE == attribute_level.
100+ For TARGET-level attributes, looks for ATTRIBUTE_TYPE == context.attribute_name (e.g., "COVID").
101+
101102 Args:
102103 context: The derived value context
103104
@@ -108,8 +109,13 @@ def _find_source_date(self, context: DerivedValueContext) -> str | None:
108109 if not source_attr :
109110 return None
110111
112+ if context .attribute_level in ("PERSON" , "COHORT" ):
113+ attribute_type_to_match = context .attribute_level
114+ else :
115+ attribute_type_to_match = context .attribute_name
116+
111117 for attribute in context .person_data :
112- if attribute .get ("ATTRIBUTE_TYPE" ) == context . attribute_name :
118+ if attribute .get ("ATTRIBUTE_TYPE" ) == attribute_type_to_match :
113119 return attribute .get (source_attr )
114120
115121 return None
@@ -128,7 +134,6 @@ def _get_days_to_add(self, context: DerivedValueContext) -> int:
128134 Returns:
129135 Number of days to add
130136 """
131- # Priority 1: Token argument (if non-empty)
132137 if context .function_args :
133138 args = context .function_args .split ("," )[0 ].strip ()
134139 if args :
@@ -138,11 +143,9 @@ def _get_days_to_add(self, context: DerivedValueContext) -> int:
138143 message = f"Invalid days argument '{ args } ' for ADD_DAYS function. Expected an integer."
139144 raise ValueError (message ) from e
140145
141- # Priority 2: Vaccine-specific configuration
142146 if context .attribute_name in self .vaccine_type_days :
143147 return self .vaccine_type_days [context .attribute_name ]
144148
145- # Priority 3: Default
146149 return self .default_days
147150
148151 def _add_days_to_date (self , date_str : str , days : int ) -> datetime :
0 commit comments