From 2f0dfa60c8498b6c487a384f0b5136a743ddbe79 Mon Sep 17 00:00:00 2001 From: eliott Date: Mon, 20 Jan 2025 11:45:51 +0100 Subject: [PATCH 01/10] Add utility functions for order data validation and ticket tag generation --- cg/meta/orders/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cg/meta/orders/utils.py diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py new file mode 100644 index 0000000000..0029e4f9a4 --- /dev/null +++ b/cg/meta/orders/utils.py @@ -0,0 +1,26 @@ +from cg.models.orders.constants import OrderType +from cg.services.orders.constants import ORDER_TYPE_WORKFLOW_MAP +from cg.services.orders.validation.models.case import Case +from cg.services.orders.validation.models.order import Order +from cg.services.orders.validation.models.order_with_cases import OrderWithCases + + +def is_order_containing_existing_data(order: OrderWithCases) -> bool: + """Check if the order contains any existing data""" + + for enumerated_case in order.enumerated_cases: + case: Case = enumerated_case[1] + if case.enumerated_existing_samples: + return True + return False + + +def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: + """Generate ticket tags based on the order and order type""" + tags: list[str] = [] + tags.append([ORDER_TYPE_WORKFLOW_MAP[order_type]]) + + if is_order_containing_existing_data(order=order): + tags.append("existing-data") + + return tags From 4062b04719bcc4c21e7a1a47349da01fc90e68d8 Mon Sep 17 00:00:00 2001 From: eliott Date: Mon, 20 Jan 2025 11:49:05 +0100 Subject: [PATCH 02/10] Integrate ticket tag generation into TicketHandler --- cg/meta/orders/utils.py | 1 + cg/services/orders/submitter/ticket_handler.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index 0029e4f9a4..ed3fcd5c04 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -17,6 +17,7 @@ def is_order_containing_existing_data(order: OrderWithCases) -> bool: def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: """Generate ticket tags based on the order and order type""" + tags: list[str] = [] tags.append([ORDER_TYPE_WORKFLOW_MAP[order_type]]) diff --git a/cg/services/orders/submitter/ticket_handler.py b/cg/services/orders/submitter/ticket_handler.py index 54ff979156..8cb2bfe2c5 100644 --- a/cg/services/orders/submitter/ticket_handler.py +++ b/cg/services/orders/submitter/ticket_handler.py @@ -5,6 +5,7 @@ from cg.clients.freshdesk.freshdesk_client import FreshdeskClient from cg.clients.freshdesk.models import TicketCreate, TicketResponse +from cg.meta.orders.utils import get_ticket_tags from cg.models.orders.constants import OrderType from cg.services.orders.constants import ORDER_TYPE_WORKFLOW_MAP from cg.services.orders.validation.models.order import Order @@ -37,6 +38,8 @@ def create_ticket( order_type=order_type, ) + tags: list[str] = get_ticket_tags(order=order, order_type=order_type) + with TemporaryDirectory() as temp_dir: attachments: Path = self.create_attachment_file(order=order, temp_dir=temp_dir) @@ -47,7 +50,7 @@ def create_ticket( name=user_name, subject=order.name, type="Order", - tags=[ORDER_TYPE_WORKFLOW_MAP[order_type]], + tags=tags, custom_fields={ "cf_environment": self.env, }, From 71909ceca3126b510ff345160701400d789d9444 Mon Sep 17 00:00:00 2001 From: eliott Date: Mon, 20 Jan 2025 12:03:18 +0100 Subject: [PATCH 03/10] Add unit test for ticket tag generation based on order type --- .../orders/submitter/test_order_submitter.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/services/orders/submitter/test_order_submitter.py b/tests/services/orders/submitter/test_order_submitter.py index 57edb4458c..6ebea01b86 100644 --- a/tests/services/orders/submitter/test_order_submitter.py +++ b/tests/services/orders/submitter/test_order_submitter.py @@ -5,6 +5,7 @@ from cg.clients.freshdesk.models import TicketResponse from cg.exc import TicketCreationError +from cg.meta.orders.utils import get_ticket_tags from cg.models.orders.constants import OrderType from cg.services.orders.constants import ORDER_TYPE_WORKFLOW_MAP from cg.services.orders.storing.constants import MAF_ORDER_ID @@ -180,3 +181,16 @@ def test_submit_ticketexception( order_submitter.submit( raw_order=raw_order, user=user, order_type=mip_dna_order.order_type ) + + +def test_get_ticket_tags(order_fixture: str, order_type: OrderType, request: pytest.FixtureRequest): + """Test that the correct tags are generated based on the order and order type.""" + # GIVEN an order with existing data + order: OrderWithCases = request.getfixturevalue(order_fixture) + order.cases[0].samples[0].existing_sample = True + + # WHEN getting the ticket tags + tags = get_ticket_tags(order=order, order_type=order_type) + + # THEN the tags should be correct + assert tags == [ORDER_TYPE_WORKFLOW_MAP[order_type]] From da49a5c3d4e270bd0b9c0de40c79363cf9358083 Mon Sep 17 00:00:00 2001 From: eliott Date: Mon, 20 Jan 2025 16:10:08 +0100 Subject: [PATCH 04/10] Refactor ticket tag generation and enhance unit tests for accuracy --- cg/meta/orders/utils.py | 2 +- .../orders/submitter/test_order_submitter.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index ed3fcd5c04..1e47ec7d25 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -19,7 +19,7 @@ def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: """Generate ticket tags based on the order and order type""" tags: list[str] = [] - tags.append([ORDER_TYPE_WORKFLOW_MAP[order_type]]) + tags.append(ORDER_TYPE_WORKFLOW_MAP[order_type]) if is_order_containing_existing_data(order=order): tags.append("existing-data") diff --git a/tests/services/orders/submitter/test_order_submitter.py b/tests/services/orders/submitter/test_order_submitter.py index 6ebea01b86..a42292d9d9 100644 --- a/tests/services/orders/submitter/test_order_submitter.py +++ b/tests/services/orders/submitter/test_order_submitter.py @@ -183,14 +183,26 @@ def test_submit_ticketexception( ) -def test_get_ticket_tags(order_fixture: str, order_type: OrderType, request: pytest.FixtureRequest): +@pytest.mark.parametrize( + "order_fixture, order_type, expected_tags", + [ + ("mip_dna_order_with_existing_samples", OrderType.MIP_DNA, ["mip-dna", "existing-data"]), + ("mip_dna_order", OrderType.MIP_DNA, ["mip-dna"]), + ], +) +def test_get_ticket_tags( + request: pytest.FixtureRequest, + order_fixture: str, + order_type: OrderType, + expected_tags: list[str], +): """Test that the correct tags are generated based on the order and order type.""" + # GIVEN an order with existing data order: OrderWithCases = request.getfixturevalue(order_fixture) - order.cases[0].samples[0].existing_sample = True # WHEN getting the ticket tags tags = get_ticket_tags(order=order, order_type=order_type) # THEN the tags should be correct - assert tags == [ORDER_TYPE_WORKFLOW_MAP[order_type]] + assert tags == expected_tags From 5555920c73dcb8e1ca9dd75b21e64d5cafe0eec5 Mon Sep 17 00:00:00 2001 From: eliott Date: Tue, 21 Jan 2025 10:43:40 +0100 Subject: [PATCH 05/10] Rename function and update references in ticket tag generation --- cg/meta/orders/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index 1e47ec7d25..97542c587c 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -5,7 +5,7 @@ from cg.services.orders.validation.models.order_with_cases import OrderWithCases -def is_order_containing_existing_data(order: OrderWithCases) -> bool: +def does_order_contain_existing_data(order: OrderWithCases) -> bool: """Check if the order contains any existing data""" for enumerated_case in order.enumerated_cases: @@ -21,7 +21,7 @@ def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: tags: list[str] = [] tags.append(ORDER_TYPE_WORKFLOW_MAP[order_type]) - if is_order_containing_existing_data(order=order): + if does_order_contain_existing_data(order=order): tags.append("existing-data") return tags From 19efd0fccf58d3089e7dc25826012a69c242f4f8 Mon Sep 17 00:00:00 2001 From: eliott Date: Tue, 21 Jan 2025 10:43:48 +0100 Subject: [PATCH 06/10] Add fixture for MIP DNA order with existing samples --- .../orders_fixtures/order_fixtures.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/fixture_plugins/orders_fixtures/order_fixtures.py b/tests/fixture_plugins/orders_fixtures/order_fixtures.py index 3b290c0aa1..159adee784 100644 --- a/tests/fixture_plugins/orders_fixtures/order_fixtures.py +++ b/tests/fixture_plugins/orders_fixtures/order_fixtures.py @@ -2,6 +2,7 @@ import pytest +from cg.services.orders.validation.models.existing_sample import ExistingSample from cg.services.orders.validation.workflows.balsamic.models.order import BalsamicOrder from cg.services.orders.validation.workflows.fastq.models.order import FastqOrder from cg.services.orders.validation.workflows.fluffy.models.order import FluffyOrder @@ -135,3 +136,14 @@ def tomte_order(tomte_order_to_submit: dict, ticket_id_as_int: int) -> TomteOrde tomte_order = TomteOrder.model_validate(tomte_order_to_submit) tomte_order._generated_ticket_id = ticket_id_as_int return tomte_order + + +@pytest.fixture +def mip_dna_order_with_existing_samples(mip_dna_order_to_submit: dict) -> MipDnaOrder: + """Returns a MIP DNA order containing an existing sample.""" + mip_dna_order_to_submit["user_id"] = 1 + mip_dna_order_with_existing_samples = MipDnaOrder.model_validate(mip_dna_order_to_submit) + mip_dna_order_with_existing_samples.cases[0].samples.append( + ExistingSample(internal_id="ABC123") + ) + return mip_dna_order_with_existing_samples From 0af8611a80757f8fa1b77b7d51a01428b865300c Mon Sep 17 00:00:00 2001 From: eliott Date: Tue, 21 Jan 2025 10:44:40 +0100 Subject: [PATCH 07/10] Update internal_id for existing sample in MIP DNA order fixture --- tests/fixture_plugins/orders_fixtures/order_fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixture_plugins/orders_fixtures/order_fixtures.py b/tests/fixture_plugins/orders_fixtures/order_fixtures.py index 159adee784..c92d1f15a3 100644 --- a/tests/fixture_plugins/orders_fixtures/order_fixtures.py +++ b/tests/fixture_plugins/orders_fixtures/order_fixtures.py @@ -144,6 +144,6 @@ def mip_dna_order_with_existing_samples(mip_dna_order_to_submit: dict) -> MipDna mip_dna_order_to_submit["user_id"] = 1 mip_dna_order_with_existing_samples = MipDnaOrder.model_validate(mip_dna_order_to_submit) mip_dna_order_with_existing_samples.cases[0].samples.append( - ExistingSample(internal_id="ABC123") + ExistingSample(internal_id="ACC1234") ) return mip_dna_order_with_existing_samples From 5fad3d05ab5066357be0e6980484c8622b63ad1c Mon Sep 17 00:00:00 2001 From: eliott Date: Thu, 23 Jan 2025 14:36:04 +0100 Subject: [PATCH 08/10] Rename function for clarity and update ticket tag generation logic to check for existing data in orders --- cg/meta/orders/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index 97542c587c..bb2e45cd0e 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -5,7 +5,7 @@ from cg.services.orders.validation.models.order_with_cases import OrderWithCases -def does_order_contain_existing_data(order: OrderWithCases) -> bool: +def contains_existing_data(order: OrderWithCases) -> bool: """Check if the order contains any existing data""" for enumerated_case in order.enumerated_cases: @@ -21,7 +21,8 @@ def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: tags: list[str] = [] tags.append(ORDER_TYPE_WORKFLOW_MAP[order_type]) - if does_order_contain_existing_data(order=order): - tags.append("existing-data") + if isinstance(order, OrderWithCases): + if contains_existing_data(order=order): + tags.append("existing-data") return tags From 538e712b0eb177e3b46ef67bffa322a917b30fc2 Mon Sep 17 00:00:00 2001 From: EliottBo <112384714+eliottBo@users.noreply.github.com> Date: Thu, 23 Jan 2025 15:12:13 +0100 Subject: [PATCH 09/10] Update cg/meta/orders/utils.py Co-authored-by: Vincent Janvid <69356202+Vince-janv@users.noreply.github.com> --- cg/meta/orders/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index bb2e45cd0e..cb51f51ac5 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -22,7 +22,7 @@ def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: tags.append(ORDER_TYPE_WORKFLOW_MAP[order_type]) if isinstance(order, OrderWithCases): - if contains_existing_data(order=order): + if contains_existing_data(order): tags.append("existing-data") return tags From 8473da44c3599db295c243242fa7f7c1a193f9a9 Mon Sep 17 00:00:00 2001 From: EliottBo <112384714+eliottBo@users.noreply.github.com> Date: Thu, 23 Jan 2025 15:12:28 +0100 Subject: [PATCH 10/10] Update cg/meta/orders/utils.py Co-authored-by: Vincent Janvid <69356202+Vince-janv@users.noreply.github.com> --- cg/meta/orders/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py index cb51f51ac5..2663271610 100644 --- a/cg/meta/orders/utils.py +++ b/cg/meta/orders/utils.py @@ -18,8 +18,7 @@ def contains_existing_data(order: OrderWithCases) -> bool: def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]: """Generate ticket tags based on the order and order type""" - tags: list[str] = [] - tags.append(ORDER_TYPE_WORKFLOW_MAP[order_type]) + tags: list[str] = [ORDER_TYPE_WORKFLOW_MAP[order_type]] if isinstance(order, OrderWithCases): if contains_existing_data(order):