diff --git a/src/dispatch/conversation/service.py b/src/dispatch/conversation/service.py index 0c2ab2240841..9a27410d1c5f 100644 --- a/src/dispatch/conversation/service.py +++ b/src/dispatch/conversation/service.py @@ -17,22 +17,21 @@ def get_by_channel_id_ignoring_channel_type( """ channel_id_without_type = channel_id[1:] - # For cases - if thread_id: - conversation = ( - db_session.query(Conversation) - .filter(Conversation.channel_id.contains(channel_id_without_type)) - .filter(Conversation.thread_id == thread_id) - .filter(Conversation.case_id.isnot(None)) - .one_or_none() - ) - # For incidents - else: - conversation = ( - db_session.query(Conversation) - .filter(Conversation.channel_id.contains(channel_id_without_type)) - .one_or_none() - ) + conversation = None + + query = db_session.query(Conversation).filter( + Conversation.channel_id.contains(channel_id_without_type) + ) + + # The code below disambiguates between incident threads, case threads, and incident messages + if not thread_id: + conversation = query.one_or_none() + + if not conversation: + conversation = query.filter(Conversation.thread_id == thread_id).one_or_none() + + if not conversation: + conversation = query.one_or_none() if conversation: if channel_id[0] != conversation.channel_id[0]: