From be731e86ccacf71dd9e1847c90daa78f5851800e Mon Sep 17 00:00:00 2001 From: Shaelyn Tolkamp <46355612+tolkamps1@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:48:19 -0800 Subject: [PATCH] [Track-297] RF update work title in model to remove hanging dash (#2467) --- epictrack-api/src/api/models/work.py | 11 ++++++++++- epictrack-api/tests/unit/apis/test_works.py | 2 +- epictrack-web/src/components/work/WorkForm/index.tsx | 10 +++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/epictrack-api/src/api/models/work.py b/epictrack-api/src/api/models/work.py index b97f5ca62..e99fd1e08 100644 --- a/epictrack-api/src/api/models/work.py +++ b/epictrack-api/src/api/models/work.py @@ -29,6 +29,7 @@ String, Text, and_, + case, exists, func, or_ @@ -143,7 +144,15 @@ def title(self): def title(self): """SQL expression for title.""" from api.models.work_type import WorkType # pylint:disable=import-outside-toplevel - return func.concat(Project.name, " - ", WorkType.name, " - ", self.simple_title) # pylint:disable=not-callable + return ( + case( + ( + func.coalesce(self.simple_title, "") != "", + func.concat(Project.name, " - ", WorkType.name, " - ", self.simple_title), # pylint:disable=not-callable + ), + else_=func.concat(Project.name, " - ", WorkType.name) + ) + ) @hybrid_property def anticipated_referral_date(self): diff --git a/epictrack-api/tests/unit/apis/test_works.py b/epictrack-api/tests/unit/apis/test_works.py index d2952d3cf..56bcd3fef 100644 --- a/epictrack-api/tests/unit/apis/test_works.py +++ b/epictrack-api/tests/unit/apis/test_works.py @@ -108,7 +108,7 @@ def test_validate_work(client, auth_header): del payload["work_id"] response = client.get(url, query_string=payload, headers=auth_header) assert response.status_code == HTTPStatus.OK - assert response.json["exists"] is False + assert response.json["exists"] # Scenario 3: Creating new work with new name payload = TestWorkInfo.validation_work.value diff --git a/epictrack-web/src/components/work/WorkForm/index.tsx b/epictrack-web/src/components/work/WorkForm/index.tsx index 422ae4ca3..7e4fc34b2 100644 --- a/epictrack-web/src/components/work/WorkForm/index.tsx +++ b/epictrack-web/src/components/work/WorkForm/index.tsx @@ -287,7 +287,15 @@ export default function WorkForm({ }, [workTypeId, projectId, projects, workTypes]); React.useEffect(() => { - setValue("title", `${titlePrefix}${simple_title}`); + if (simple_title) { + setValue("title", `${titlePrefix}${simple_title}`); + } else { + // If simple_title is not set, remove the hanging separator + const trimmedPrefix = titlePrefix.endsWith(titleSeparator) + ? titlePrefix.slice(0, -titleSeparator.length) + : titlePrefix; + setValue("title", trimmedPrefix); + } }, [titlePrefix, simple_title]); const handleProjectChange = async (id: string) => {