From 45fc38454059e2353d1c57454747130edc08d685 Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Fri, 9 Aug 2024 15:05:18 +0300 Subject: [PATCH] fix: trim the ahjo case title to 100 characters (#3201) --- .../applications/services/ahjo_payload.py | 30 +++++++++++++++---- .../applications/tests/test_ahjo_payload.py | 19 ++++++++---- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/backend/benefit/applications/services/ahjo_payload.py b/backend/benefit/applications/services/ahjo_payload.py index 5142a5f021..a709ab2e79 100644 --- a/backend/benefit/applications/services/ahjo_payload.py +++ b/backend/benefit/applications/services/ahjo_payload.py @@ -43,17 +43,35 @@ def _prepare_record_title( return f"{AhjoRecordTitle.APPLICATION} {formatted_date}, liite {current}/{total}, {application.application_number}" -def _prepare_case_title(application: Application) -> str: +def prepare_case_title(application: Application, company_name: str) -> str: + """Prepare the case title for Ahjo""" full_title = f"Avustukset työnantajille, työllisyyspalvelut, \ -Helsinki-lisä, {_truncate_company_name(application.company_name)}, \ +Helsinki-lisä, {company_name}, \ hakemus {application.application_number}" return full_title -def _truncate_company_name(company_name: str) -> str: - """Truncate the company name to 100 characters, \ +def prepare_final_case_title(application: Application) -> str: + """Prepare the final case title for Ahjo, if the full title is over 100 characters, \ + truncate the company name to fit the limit.""" + limit = 100 + full_case_title = prepare_case_title(application, application.company_name) + length_of_full_title = len(full_case_title) + + if length_of_full_title <= limit: + return full_case_title + else: + over_limit = length_of_full_title - limit + truncated_company_name = truncate_company_name( + application.company_name, over_limit + ) + return prepare_case_title(application, truncated_company_name) + + +def truncate_company_name(company_name: str, chars_to_truncate: int) -> str: + """Truncate the company name to a number of characters, \ because Ahjo has a technical limitation of 100 characters for the company name.""" - return company_name[:100] + return company_name[:-chars_to_truncate] def resolve_payload_language(application: Application) -> str: @@ -235,7 +253,7 @@ def prepare_open_case_payload( ) -> dict: "Prepare the complete dictionary payload that is sent to Ahjo" case_records = _prepare_case_records(application, pdf_summary) - case_title = _prepare_case_title(application) + case_title = prepare_final_case_title(application) payload = _prepare_top_level_dict(application, case_records, case_title) return payload diff --git a/backend/benefit/applications/tests/test_ahjo_payload.py b/backend/benefit/applications/tests/test_ahjo_payload.py index 5c12669bb9..56ca475c03 100644 --- a/backend/benefit/applications/tests/test_ahjo_payload.py +++ b/backend/benefit/applications/tests/test_ahjo_payload.py @@ -13,14 +13,15 @@ from applications.models import Attachment from applications.services.ahjo_payload import ( _prepare_case_records, - _prepare_case_title, _prepare_record, _prepare_record_document_dict, _prepare_record_title, _prepare_top_level_dict, - _truncate_company_name, + prepare_case_title, + prepare_final_case_title, prepare_update_application_payload, resolve_payload_language, + truncate_company_name, ) from common.utils import hash_file @@ -30,7 +31,7 @@ def test_prepare_case_title(decided_application): wanted_title = f"Avustukset työnantajille, työllisyyspalvelut, \ Helsinki-lisä, {application.company_name}, \ hakemus {application.application_number}" - got = _prepare_case_title(application) + got = prepare_case_title(application, decided_application.company_name) assert wanted_title == got @@ -38,9 +39,15 @@ def test_truncate_company_name(): short = "a" * 50 too_long = "a" * 105 not_too_long = "a" * 100 - assert len(_truncate_company_name(too_long)) == 100 - assert len(_truncate_company_name(not_too_long)) == 100 - assert len(_truncate_company_name(short)) == 50 + assert len(truncate_company_name(too_long, 5)) == 100 + assert len(truncate_company_name(not_too_long, 5)) == 95 + assert len(truncate_company_name(short, 5)) == 45 + + +def test_prepare_final_case_title_truncate(decided_application): + application = decided_application + application.company_name = "a" * 105 + assert len(prepare_final_case_title(application)) == 100 @pytest.mark.parametrize(