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

Archive channel when closing dedicated case channel #4662

Merged
merged 12 commits into from
May 6, 2024
81 changes: 80 additions & 1 deletion src/dispatch/case/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dispatch.database.core import SessionLocal
from dispatch.decorators import background_task
from dispatch.document import flows as document_flows
from dispatch.enums import DocumentResourceTypes, Visibility
from dispatch.enums import DocumentResourceTypes, Visibility, EventType
from dispatch.event import service as event_service
from dispatch.group import flows as group_flows
from dispatch.group.enums import GroupAction, GroupType
Expand Down Expand Up @@ -383,6 +383,60 @@ def case_closed_status_flow(case: Case, db_session=None):
db_session.add(case)
db_session.commit()

if case.dedicated_channel:
# we archive the conversation
conversation_flows.archive_conversation(subject=case, db_session=db_session)

if case.visibility == Visibility.open:
storage_plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=case.project.id, plugin_type="storage"
)
if storage_plugin:
if storage_plugin.configuration.open_on_close:
for document in case.documents:
document_flows.open_document_access(document=document, db_session=db_session)

if storage_plugin.configuration.read_only:
for document in case.documents:
document_flows.mark_document_as_readonly(
document=document, db_session=db_session
)
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved


def reactivate_case_participants(case: Case, db_session: Session):
"""Reactivates all case participants."""
for participant in case.participants:
try:
case_add_or_reactivate_participant_flow(
participant.individual.email, case.id, db_session=db_session
)
except Exception as e:
# don't fail to reactivate all participants if one fails
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Unable to reactivate participant with email {participant.individual.email}",
case_id=case.id,
type=EventType.participant_updated,
)
log.exception(e)

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description="Incident participants reactivated",
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
case_id=case.id,
type=EventType.participant_updated,
)


def case_active_status_flow(case: CaseStatus, db_session=None):
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
"""Runs the case active flow."""
# we un-archive the conversation
if case.dedicated_channel:
conversation_flows.unarchive_conversation(subject=case, db_session=db_session)
reactivate_case_participants(case, db_session)


def case_status_transition_flow_dispatcher(
case: Case,
Expand All @@ -393,6 +447,31 @@ def case_status_transition_flow_dispatcher(
):
"""Runs the correct flows based on the current and previous status of the case."""
match (previous_status, current_status):
case (CaseStatus.closed, CaseStatus.new):
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
# Closed -> New
case_active_status_flow(case, db_session)

case (CaseStatus.closed, CaseStatus.triage):
# Closed -> Triage
case_active_status_flow(case, db_session)
case_triage_status_flow(
case=case,
db_session=db_session,
)

case (CaseStatus.closed, CaseStatus.escalated):
# Closed -> Escalated
case_active_status_flow(case, db_session)
case_triage_status_flow(
case=case,
db_session=db_session,
)
case_escalated_status_flow(
case=case,
organization_slug=organization_slug,
db_session=db_session,
)

case (_, CaseStatus.new):
# Any -> New
pass
Expand Down
80 changes: 52 additions & 28 deletions src/dispatch/conversation/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,53 +146,69 @@ def create_incident_conversation(incident: Incident, db_session: SessionLocal):
return incident.conversation


def archive_conversation(incident: Incident, db_session: SessionLocal):
def archive_conversation(subject: Subject, db_session: SessionLocal):
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
"""Archives a conversation."""
if not incident.conversation:
log.warning("Conversation not archived. No conversation available for this incident.")
if not subject.conversation:
log.warning("Conversation not archived. No conversation available for this subject.")
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 not archived. No conversation plugin enabled.")
return

try:
plugin.instance.archive(incident.conversation.channel_id)
plugin.instance.archive(subject.conversation.channel_id)
wssheldon marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Archiving conversation failed. Reason: {e}",
incident_id=incident.id,
)
if isinstance(subject, Incident):
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Archiving conversation failed. Reason: {e}",
incident_id=subject.id,
)
else:
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Archiving conversation failed. Reason: {e}",
case_id=subject.id,
)
log.exception(e)


def unarchive_conversation(incident: Incident, db_session: SessionLocal):
def unarchive_conversation(subject: Subject, db_session: SessionLocal):
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
"""Unarchives a conversation."""
if not incident.conversation:
log.warning("Conversation not unarchived. No conversation available for this incident.")
if not subject.conversation:
log.warning("Conversation not unarchived. No conversation available for this subject.")
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 not unarchived. No conversation plugin enabled.")
return

try:
plugin.instance.unarchive(incident.conversation.channel_id)
plugin.instance.unarchive(subject.conversation.channel_id)
except Exception as e:
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Unarchiving conversation failed. Reason: {e}",
incident_id=incident.id,
)
if isinstance(subject, Incident):
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Unarchiving conversation failed. Reason: {e}",
incident_id=subject.id,
)
else:
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Unarchiving conversation failed. Reason: {e}",
case_id=subject.id,
)
log.exception(e)


Expand Down Expand Up @@ -268,12 +284,20 @@ def add_conversation_bookmark(
)
)
except Exception as e:
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Adding the {resource.name.lower()} bookmark failed. Reason: {e}",
incident_id=subject.id,
)
if isinstance(subject, Incident):
event_service.log_incident_event(
whitdog47 marked this conversation as resolved.
Show resolved Hide resolved
db_session=db_session,
source="Dispatch Core App",
description=f"Adding the {resource.name.lower()} bookmark failed. Reason: {e}",
incident_id=subject.id,
)
elif isinstance(subject, Case):
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Adding the {resource.name.lower()} bookmark failed. Reason: {e}",
case_id=subject.id,
)
log.exception(e)


Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/incident/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def incident_create_closed_flow(
def incident_active_status_flow(incident: Incident, db_session=None):
"""Runs the incident active flow."""
# we un-archive the conversation
conversation_flows.unarchive_conversation(incident=incident, db_session=db_session)
conversation_flows.unarchive_conversation(subject=incident, db_session=db_session)


def create_incident_review_document(incident: Incident, db_session=None) -> Optional[Document]:
Expand Down Expand Up @@ -535,7 +535,7 @@ def incident_closed_status_flow(incident: Incident, db_session=None):
db_session.commit()

# we archive the conversation
conversation_flows.archive_conversation(incident=incident, db_session=db_session)
conversation_flows.archive_conversation(subject=incident, db_session=db_session)

if incident.visibility == Visibility.open:
storage_plugin = plugin_service.get_active_instance(
Expand Down