Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change topic in case channel on update #4726

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/dispatch/case/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ def case_closed_create_flow(*, case_id: int, organization_slug: OrganizationSlug
case_closed_status_flow(case=case, db_session=db_session)


def case_details_changed(case: Case, previous_case: CaseRead) -> bool:
"""Checks if the case details have changed."""
return (
case.case_type.name != previous_case.case_type.name
or case.case_severity.name != previous_case.case_severity.name
or case.case_priority.name != previous_case.case_priority.name
or case.status != previous_case.status
)


@background_task
def case_update_flow(
*,
Expand Down Expand Up @@ -361,6 +371,11 @@ def case_update_flow(
# we send the case updated notification
update_conversation(case, db_session)

if case.has_channel and case.status != CaseStatus.closed:
# determine if case channel topic needs to be updated
if case_details_changed(case, previous_case):
conversation_flows.set_conversation_topic(case, db_session)


def case_delete_flow(case: Case, db_session: SessionLocal):
"""Runs the case delete flow."""
Expand Down Expand Up @@ -743,7 +758,7 @@ def case_assign_role_flow(
case_id: int,
participant_email: str,
participant_role: str,
db_session: SessionLocal,
db_session: Session,
):
"""Runs the case participant role assignment flow."""
# we get the case
Expand All @@ -753,7 +768,14 @@ def case_assign_role_flow(
case_add_or_reactivate_participant_flow(participant_email, case.id, db_session=db_session)

# we run the assign role flow
role_flow.assign_role_flow(case, participant_email, participant_role, db_session)
result = role_flow.assign_role_flow(case, participant_email, participant_role, db_session)

if result in ["assignee_has_role", "role_not_assigned"]:
return

if case.status != CaseStatus.closed and participant_role == ParticipantRoleType.assignee:
# update the conversation topic
conversation_flows.set_conversation_topic(case, db_session)


def case_create_resources_flow(
Expand Down Expand Up @@ -886,6 +908,7 @@ def case_create_resources_flow(
db_session=db_session,
title=title,
)
conversation_flows.set_conversation_topic(case, db_session)

# we update the ticket
ticket_flows.update_case_ticket(case=case, db_session=db_session)
43 changes: 28 additions & 15 deletions src/dispatch/conversation/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,35 +196,48 @@ def unarchive_conversation(subject: Subject, db_session: Session) -> None:
log.exception(e)


def set_conversation_topic(incident: Incident, db_session: SessionLocal):
def get_topic_text(subject: Subject) -> str:
"""Returns the topic details based on subject"""
if isinstance(subject, Incident):
return (
f":helmet_with_white_cross: {subject.commander.individual.name}, {subject.commander.team} | "
f"Status: {subject.status} | "
f"Type: {subject.incident_type.name} | "
f"Severity: {subject.incident_severity.name} | "
f"Priority: {subject.incident_priority.name}"
)
return (
f":helmet_with_white_cross: {subject.assignee.individual.name}, {subject.assignee.team} | "
f"Status: {subject.status} | "
f"Type: {subject.case_type.name} | "
f"Severity: {subject.case_severity.name} | "
f"Priority: {subject.case_priority.name}"
)


def set_conversation_topic(subject: Subject, db_session: SessionLocal):
"""Sets the conversation topic."""
if not incident.conversation:
log.warning("Conversation topic not set. No conversation available for this incident.")
if not subject.conversation:
log.warning("Conversation topic not set. No conversation available for this incident/case.")
return

plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=incident.project.id, plugin_type="conversation"
db_session=db_session, project_id=subject.project.id, plugin_type="conversation"
)
if not plugin:
log.warning("Conversation topic not set. No conversation plugin enabled.")
return

conversation_topic = (
f":helmet_with_white_cross: {incident.commander.individual.name}, {incident.commander.team} | "
f"Status: {incident.status} | "
f"Type: {incident.incident_type.name} | "
f"Severity: {incident.incident_severity.name} | "
f"Priority: {incident.incident_priority.name}"
)
conversation_topic = get_topic_text(subject)

try:
plugin.instance.set_topic(incident.conversation.channel_id, conversation_topic)
plugin.instance.set_topic(subject.conversation.channel_id, conversation_topic)
except Exception as e:
event_service.log_incident_event(
event_service.log_subject_event(
subject=subject,
db_session=db_session,
source="Dispatch Core App",
description=f"Setting the incident conversation topic failed. Reason: {e}",
incident_id=incident.id,
description=f"Setting the incident/case conversation topic failed. Reason: {e}",
)
log.exception(e)

Expand Down
Loading