Skip to content

Commit

Permalink
Merge branch 'master' into update-1508-33
Browse files Browse the repository at this point in the history
  • Loading branch information
islean authored Dec 17, 2024
2 parents 4a094fe + 1fc0655 commit baf390b
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 64.5.28
current_version = 64.5.30
commit = True
tag = True
tag_name = v{new_version}
Expand Down
2 changes: 1 addition & 1 deletion cg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = "cg"
__version__ = "64.5.28"
__version__ = "64.5.30"
4 changes: 0 additions & 4 deletions cg/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,6 @@ class OrderNotFoundError(CgError):
"""Exception raised when an order is not found."""


class OrderExistsError(CgError):
"""Exception raised when cases and samples are added to a pre-existing order."""


class OrderMismatchError(CgError):
"""Exception raised when cases expected to belong to the same order are not part of the same order."""

Expand Down
20 changes: 4 additions & 16 deletions cg/meta/orders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from cg.apps.lims import LimsAPI
from cg.meta.orders.ticket_handler import TicketHandler
from cg.models.orders.order import OrderIn, OrderType
from cg.services.orders.submitters.order_submitter_registry import (
OrderSubmitterRegistry,
)
from cg.services.orders.submitters.order_submitter_registry import OrderSubmitterRegistry
from cg.store.store import Store

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -43,18 +41,8 @@ def submit(self, project: OrderType, order_in: OrderIn, user_name: str, user_mai
"""
submit_handler = self.submitter_registry.get_order_submitter(project)
submit_handler.order_validation_service.validate_order(order_in)
# detect manual ticket assignment
ticket_number: str | None = self.ticket_handler.parse_ticket_number(order_in.name)
if not ticket_number:
ticket_number = self.ticket_handler.create_ticket(
order=order_in, user_name=user_name, user_mail=user_mail, project=project
)
else:
self.ticket_handler.connect_to_ticket(
order=order_in,
user_name=user_name,
project=project,
ticket_number=ticket_number,
)
ticket_number = self.ticket_handler.create_ticket(
order=order_in, user_name=user_name, user_mail=user_mail, project=project
)
order_in.ticket = ticket_number
return submit_handler.submit_order(order_in=order_in)
46 changes: 1 addition & 45 deletions cg/meta/orders/ticket_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
import re
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any

from cg.clients.freshdesk.freshdesk_client import FreshdeskClient
from cg.clients.freshdesk.models import ReplyCreate, TicketCreate, TicketResponse
from cg.clients.freshdesk.models import TicketCreate, TicketResponse
from cg.models.orders.order import OrderIn
from cg.models.orders.samples import Of1508Sample
from cg.store.models import Customer, Sample
Expand All @@ -25,18 +24,6 @@ def __init__(self, db: Store, client: FreshdeskClient, system_email_id: int, env
self.system_email_id: int = system_email_id
self.env: str = env

@staticmethod
def parse_ticket_number(name: str) -> str | None:
"""Try to parse a ticket number from a string"""
# detect manual ticket assignment
ticket_match = re.fullmatch(r"#(\d{6,10})", name)
if ticket_match:
ticket_id = ticket_match.group(1)
LOG.info(f"{ticket_id}: detected ticket in order name")
return ticket_id
LOG.info(f"Could not detected ticket number in name {name}")
return None

def create_ticket(
self, order: OrderIn, user_name: str, user_mail: str, project: str
) -> int | None:
Expand Down Expand Up @@ -105,13 +92,6 @@ def create_xml_sample_list(self, order: OrderIn, user_name: str) -> str:
def create_new_ticket_header(message: str, order: OrderIn, project: str) -> str:
return f"New order with {len(order.samples)} {project} samples:" + message

@staticmethod
def add_existing_ticket_header(message: str, order: OrderIn, project: str) -> str:
return (
f"A new order with {len(order.samples)} {project} samples has been connected to this ticket:"
+ message
)

def add_sample_name_to_message(self, message: str, sample_name: str) -> str:
message += f"{self.NEW_LINE}{sample_name}"
return message
Expand Down Expand Up @@ -188,27 +168,3 @@ def replace_empty_string_with_none(cls, obj: Any) -> Any:
else:
obj[key] = cls.replace_empty_string_with_none(item)
return obj

def connect_to_ticket(
self, order: OrderIn, user_name: str, project: str, ticket_number: str
) -> None:
"""Appends a new order message to the ticket selected by the customer"""
LOG.info(f"Connecting order to ticket {ticket_number}")

message: str = self.add_existing_ticket_header(
message=self.create_xml_sample_list(order=order, user_name=user_name),
order=order,
project=project,
)

with TemporaryDirectory() as temp_dir:
attachments: Path = self.create_attachment_file(order=order, temp_dir=temp_dir)

reply = ReplyCreate(ticket_number=ticket_number, body=message)

self.client.reply_to_ticket(
reply=reply,
attachments=[attachments],
)

LOG.info(f"Connected order to ticket {ticket_number} in Freshdesk")
9 changes: 1 addition & 8 deletions cg/server/endpoints/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from cg.constants.constants import FileFormat
from cg.exc import (
OrderError,
OrderExistsError,
OrderFormError,
OrderNotDeliverableError,
OrderNotFoundError,
Expand All @@ -27,9 +26,7 @@
from cg.meta.orders import OrdersAPI
from cg.models.orders.order import OrderIn, OrderType
from cg.models.orders.orderform_schema import Orderform
from cg.server.dto.delivery_message.delivery_message_response import (
DeliveryMessageResponse,
)
from cg.server.dto.delivery_message.delivery_message_response import DeliveryMessageResponse
from cg.server.dto.orders.order_delivery_update_request import OrderOpenUpdateRequest
from cg.server.dto.orders.order_patch_request import OrderOpenPatch
from cg.server.dto.orders.orders_request import OrdersRequest
Expand Down Expand Up @@ -170,9 +167,6 @@ def submit_order(order_type):
)
project = OrderType(order_type)
order_in = OrderIn.parse_obj(request_json, project=project)
existing_ticket: str | None = ticket_handler.parse_ticket_number(order_in.name)
if existing_ticket and order_service.store.get_order_by_ticket_id(existing_ticket):
raise OrderExistsError(f"Order with ticket id {existing_ticket} already exists.")

result: dict = api.submit(
project=project,
Expand All @@ -183,7 +177,6 @@ def submit_order(order_type):

except ( # user misbehaviour
OrderError,
OrderExistsError,
OrderFormError,
ValidationError,
ValueError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cg.exc import OrderError
from cg.models.orders.constants import OrderType
from cg.models.orders.order import OrderIn
from cg.models.orders.samples import SarsCov2Sample
from cg.models.orders.samples import MicrobialFastqSample, SarsCov2Sample
from cg.services.orders.submitters.order_submitter import ValidateOrderService
from cg.store.models import Customer
from cg.store.store import Store
Expand All @@ -15,18 +15,25 @@ def __init__(self, status_db: Store):
def validate_order(self, order: OrderIn) -> None:
if order.order_type == OrderType.SARS_COV_2:
self._validate_sample_names_are_available(
samples=order.samples, customer_id=order.customer
samples=order.samples, customer_id=order.customer, is_sars_cov_2=True
)
elif order.order_type == OrderType.MICROBIAL_FASTQ:
self._validate_sample_names_are_available(
samples=order.samples, customer_id=order.customer, is_sars_cov_2=False
)

def _validate_sample_names_are_available(
self, samples: list[SarsCov2Sample], customer_id: str
self,
samples: list[SarsCov2Sample] | list[MicrobialFastqSample],
customer_id: str,
is_sars_cov_2: bool,
) -> None:
"""Validate names of all samples are not already in use."""
customer: Customer = self.status_db.get_customer_by_internal_id(
customer_internal_id=customer_id
)
for sample in samples:
if sample.control:
if is_sars_cov_2 and sample.control:
continue
if self.status_db.get_sample_by_customer_and_name(
customer_entry_id=[customer.id], sample_name=sample.name
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "cg"
version = "64.5.28"
version = "64.5.30"
description = "Clinical Genomics command center"
authors = ["Clinical Genomics <[email protected]>"]
readme = "README.md"
Expand Down
11 changes: 0 additions & 11 deletions tests/meta/orders/test_ticket_handler.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
from cg.meta.orders.ticket_handler import TicketHandler


def test_parse_ticket_number(ticket_id: str):
# GIVEN a string with a ticket number
order_name = f"#{ticket_id}"

# WHEN parsing the string
result = TicketHandler.parse_ticket_number(order_name)

# THEN assert that the correct string was parsed
assert result == ticket_id


def test_add_user_name_message(ticket_handler: TicketHandler):
# GIVEN a message string
message = ""
Expand Down

0 comments on commit baf390b

Please sign in to comment.