diff --git a/cg/meta/orders/utils.py b/cg/meta/orders/utils.py new file mode 100644 index 0000000000..2663271610 --- /dev/null +++ b/cg/meta/orders/utils.py @@ -0,0 +1,27 @@ +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 contains_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] = [ORDER_TYPE_WORKFLOW_MAP[order_type]] + + if isinstance(order, OrderWithCases): + if contains_existing_data(order): + tags.append("existing-data") + + return tags 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, }, diff --git a/tests/fixture_plugins/orders_fixtures/order_fixtures.py b/tests/fixture_plugins/orders_fixtures/order_fixtures.py index 3b290c0aa1..c92d1f15a3 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="ACC1234") + ) + return mip_dna_order_with_existing_samples diff --git a/tests/services/orders/submitter/test_order_submitter.py b/tests/services/orders/submitter/test_order_submitter.py index 57edb4458c..a42292d9d9 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,28 @@ def test_submit_ticketexception( order_submitter.submit( raw_order=raw_order, user=user, order_type=mip_dna_order.order_type ) + + +@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) + + # WHEN getting the ticket tags + tags = get_ticket_tags(order=order, order_type=order_type) + + # THEN the tags should be correct + assert tags == expected_tags