Skip to content

Commit

Permalink
fix: trim the ahjo case title to 100 characters (#3201)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Aug 9, 2024
1 parent 2773d78 commit 45fc384
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
30 changes: 24 additions & 6 deletions backend/benefit/applications/services/ahjo_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
19 changes: 13 additions & 6 deletions backend/benefit/applications/tests/test_ahjo_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -30,17 +31,23 @@ 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


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(
Expand Down

0 comments on commit 45fc384

Please sign in to comment.