|
| 1 | +from datetime import UTC, datetime, timedelta |
| 2 | +from typing import ClassVar |
| 3 | + |
| 4 | +from eligibility_signposting_api.services.processors.derived_values.base import ( |
| 5 | + DerivedValueContext, |
| 6 | + DerivedValueHandler, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class AddDaysHandler(DerivedValueHandler): |
| 11 | + """Handler for adding days to a date value. |
| 12 | +
|
| 13 | + This handler calculates derived dates by adding a configurable number of days |
| 14 | + to a source date attribute. It supports: |
| 15 | + - Default days value for all vaccine types |
| 16 | + - Vaccine-specific days configuration |
| 17 | + - Configurable mapping of derived attributes to source attributes |
| 18 | +
|
| 19 | + Example token: [[TARGET.COVID.NEXT_DOSE_DUE:ADD_DAYS(91)]] |
| 20 | + This would add 91 days to COVID's LAST_SUCCESSFUL_DATE to calculate NEXT_DOSE_DUE. |
| 21 | +
|
| 22 | + The number of days can be specified in three ways (in order of precedence): |
| 23 | + 1. In the token itself: :ADD_DAYS(91) |
| 24 | + 2. In the vaccine_type_days configuration |
| 25 | + 3. Using the default_days value |
| 26 | + """ |
| 27 | + |
| 28 | + function_name: str = "ADD_DAYS" |
| 29 | + |
| 30 | + # Mapping of derived attribute names to their source attributes |
| 31 | + DERIVED_ATTRIBUTE_SOURCES: ClassVar[dict[str, str]] = { |
| 32 | + "NEXT_DOSE_DUE": "LAST_SUCCESSFUL_DATE", |
| 33 | + } |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + default_days: int = 91, |
| 38 | + vaccine_type_days: dict[str, int] | None = None, |
| 39 | + ) -> None: |
| 40 | + """Initialize the AddDaysHandler. |
| 41 | +
|
| 42 | + Args: |
| 43 | + default_days: Default number of days to add when not specified |
| 44 | + in token or vaccine_type_days. Defaults to 91. |
| 45 | + vaccine_type_days: Dictionary mapping vaccine types to their |
| 46 | + specific days values. E.g., {"COVID": 91, "FLU": 365} |
| 47 | + """ |
| 48 | + self.default_days = default_days |
| 49 | + self.vaccine_type_days = vaccine_type_days or {} |
| 50 | + |
| 51 | + def get_source_attribute(self, target_attribute: str) -> str: |
| 52 | + """Get the source attribute for a derived attribute. |
| 53 | +
|
| 54 | + Args: |
| 55 | + target_attribute: The derived attribute name (e.g., 'NEXT_DOSE_DUE') |
| 56 | +
|
| 57 | + Returns: |
| 58 | + The source attribute name (e.g., 'LAST_SUCCESSFUL_DATE') |
| 59 | + """ |
| 60 | + return self.DERIVED_ATTRIBUTE_SOURCES.get(target_attribute, target_attribute) |
| 61 | + |
| 62 | + def calculate(self, context: DerivedValueContext) -> str: |
| 63 | + """Calculate a date with added days. |
| 64 | +
|
| 65 | + Args: |
| 66 | + context: DerivedValueContext containing: |
| 67 | + - person_data: List of attribute dictionaries |
| 68 | + - attribute_name: Vaccine type (e.g., 'COVID') |
| 69 | + - source_attribute: The source date attribute |
| 70 | + - function_args: Optional days override from token |
| 71 | + - date_format: Optional output date format |
| 72 | +
|
| 73 | + Returns: |
| 74 | + The calculated date as a formatted string |
| 75 | +
|
| 76 | + Raises: |
| 77 | + ValueError: If source date is not found or invalid |
| 78 | + """ |
| 79 | + source_date = self._find_source_date(context) |
| 80 | + if not source_date: |
| 81 | + return "" |
| 82 | + |
| 83 | + days_to_add = self._get_days_to_add(context) |
| 84 | + calculated_date = self._add_days_to_date(source_date, days_to_add) |
| 85 | + |
| 86 | + return self._format_date(calculated_date, context.date_format) |
| 87 | + |
| 88 | + def _find_source_date(self, context: DerivedValueContext) -> str | None: |
| 89 | + """Find the source date value from person data. |
| 90 | +
|
| 91 | + Args: |
| 92 | + context: The derived value context |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The source date string or None if not found |
| 96 | + """ |
| 97 | + source_attr = context.source_attribute |
| 98 | + if not source_attr: |
| 99 | + return None |
| 100 | + |
| 101 | + for attribute in context.person_data: |
| 102 | + if attribute.get("ATTRIBUTE_TYPE") == context.attribute_name: |
| 103 | + return attribute.get(source_attr) |
| 104 | + |
| 105 | + return None |
| 106 | + |
| 107 | + def _get_days_to_add(self, context: DerivedValueContext) -> int: |
| 108 | + """Determine the number of days to add. |
| 109 | +
|
| 110 | + Priority: |
| 111 | + 1. Function argument from token (e.g., :ADD_DAYS(91)) |
| 112 | + 2. Vaccine-specific configuration |
| 113 | + 3. Default days |
| 114 | +
|
| 115 | + Args: |
| 116 | + context: The derived value context |
| 117 | +
|
| 118 | + Returns: |
| 119 | + Number of days to add |
| 120 | + """ |
| 121 | + # Priority 1: Token argument |
| 122 | + if context.function_args: |
| 123 | + try: |
| 124 | + return int(context.function_args) |
| 125 | + except ValueError: |
| 126 | + pass |
| 127 | + |
| 128 | + # Priority 2: Vaccine-specific configuration |
| 129 | + if context.attribute_name in self.vaccine_type_days: |
| 130 | + return self.vaccine_type_days[context.attribute_name] |
| 131 | + |
| 132 | + # Priority 3: Default |
| 133 | + return self.default_days |
| 134 | + |
| 135 | + def _add_days_to_date(self, date_str: str, days: int) -> datetime: |
| 136 | + """Parse a date string and add days. |
| 137 | +
|
| 138 | + Args: |
| 139 | + date_str: Date in YYYYMMDD format |
| 140 | + days: Number of days to add |
| 141 | +
|
| 142 | + Returns: |
| 143 | + The calculated datetime |
| 144 | +
|
| 145 | + Raises: |
| 146 | + ValueError: If date format is invalid |
| 147 | + """ |
| 148 | + try: |
| 149 | + date_obj = datetime.strptime(date_str, "%Y%m%d").replace(tzinfo=UTC) |
| 150 | + return date_obj + timedelta(days=days) |
| 151 | + except ValueError as e: |
| 152 | + message = f"Invalid date format: {date_str}" |
| 153 | + raise ValueError(message) from e |
| 154 | + |
| 155 | + def _format_date(self, date_obj: datetime, date_format: str | None) -> str: |
| 156 | + """Format a datetime object. |
| 157 | +
|
| 158 | + Args: |
| 159 | + date_obj: The datetime to format |
| 160 | + date_format: Optional strftime format string |
| 161 | +
|
| 162 | + Returns: |
| 163 | + Formatted date string. If no format specified, returns YYYYMMDD. |
| 164 | + """ |
| 165 | + if date_format: |
| 166 | + return date_obj.strftime(date_format) |
| 167 | + return date_obj.strftime("%Y%m%d") |
0 commit comments