Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lambdas/backend/src/controller/parameter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def process_optional_params(
"""Parse optional params (date.from, date.to, _include).
Raises ParameterExceptionError for any validation error.
"""
errors = []
include = None
date_from = None
date_to = None
Expand All @@ -117,36 +118,31 @@ def process_optional_params(

if date_froms:
if len(date_froms) != 1:
raise ParameterExceptionError(
f"Search parameter {ImmunizationSearchParameterName.DATE_FROM} may have one value at most."
)
errors.append(f"Search parameter {ImmunizationSearchParameterName.DATE_FROM} may have one value at most.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check with Krupa if these messages need to be appended of if they need to be on their own. Only because they're not mentioned in the ticket

try:
date_from = datetime.datetime.strptime(date_froms[0], "%Y-%m-%d").date()
except ValueError:
raise ParameterExceptionError(
f"Search parameter {ImmunizationSearchParameterName.DATE_FROM} must be in format: YYYY-MM-DD"
)
errors.append(f"Search parameter {ImmunizationSearchParameterName.DATE_FROM} must be in format: YYYY-MM-DD")

if date_tos:
if len(date_tos) != 1:
raise ParameterExceptionError(
f"Search parameter {ImmunizationSearchParameterName.DATE_TO} may have one value at most."
)
errors.append(f"Search parameter {ImmunizationSearchParameterName.DATE_TO} may have one value at most.")
try:
date_to = datetime.datetime.strptime(date_tos[0], "%Y-%m-%d").date()
except ValueError:
raise ParameterExceptionError(
f"Search parameter {ImmunizationSearchParameterName.DATE_TO} must be in format: YYYY-MM-DD"
)
errors.append(f"Search parameter {ImmunizationSearchParameterName.DATE_TO} must be in format: YYYY-MM-DD")

if includes:
if includes[0].lower() != "immunization:patient":
raise ParameterExceptionError(
errors.append(
f"Search parameter {ImmunizationSearchParameterName.INCLUDE} may only be "
f"'Immunization:patient' if provided."
)
include = includes[0]

if errors:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be clearer to be explicit here and check if the length of the list != 0. This is subjective though.

Copy link
Contributor

@JamesW1-NHS JamesW1-NHS Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for both None and []

raise ParameterExceptionError("; ".join(errors))

return date_from, date_to, include


Expand Down
6 changes: 4 additions & 2 deletions lambdas/backend/tests/controller/test_parameter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ def test_process_search_params_raises_date_errors(self):
self.date_to_key: ["invalid-date"], # invalid format
}
)

expected = f"Search parameter {self.date_from_key} may have one value at most."
expected = (
f"Search parameter {self.date_from_key} may have one value at most.; "
f"Search parameter {self.date_to_key} must be in format: YYYY-MM-DD"
)
self.assertEqual(str(e.exception), expected)

def test_process_search_params_invalid_nhs_number_is_rejected(self):
Expand Down