Skip to content

Commit

Permalink
Remove feature to link order to an existing ticket (#4013)
Browse files Browse the repository at this point in the history
## Description
It was agreed by sysdev and prod to remove the option to link an existing ticket to an order in the order portal. This PR implements the backend changes.


### Changed

- Remove the feature to link orders to an existing ticket

---------

Co-authored-by: Sebastian Diaz <[email protected]>
  • Loading branch information
ahdamin and diitaz93 authored Dec 16, 2024
1 parent c14fb70 commit abb504d
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 84 deletions.
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
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 abb504d

Please sign in to comment.