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

Allow cases to be re-assigned after addition of participant model #2949

Merged
merged 16 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
57 changes: 43 additions & 14 deletions src/dispatch/case/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from dispatch.incident.models import IncidentCreate
from dispatch.individual.models import IndividualContactRead
from dispatch.models import OrganizationSlug, PrimaryKey
from dispatch.participant import flows as participant_flows
from dispatch.participant_role.models import ParticipantRoleType
from dispatch.participant import service as participant_service
from dispatch.participant import flows as participant_flows
from dispatch.participant.models import ParticipantUpdate
from dispatch.participant_role.models import ParticipantRoleType
from dispatch.plugin import service as plugin_service
from dispatch.storage import flows as storage_flows
from dispatch.storage.enums import StorageAction
Expand Down Expand Up @@ -92,7 +92,7 @@ def add_participants_to_conversation(


@background_task
def case_add_or_reactive_participant_flow(
def case_add_or_reactivate_participant_flow(
user_email: str,
case_id: int,
participant_role: ParticipantRoleType = ParticipantRoleType.participant,
Expand Down Expand Up @@ -368,6 +368,7 @@ def case_update_flow(
*,
case_id: int,
previous_case: CaseRead,
assignee_email: str,
user_email: str,
organization_slug: OrganizationSlug,
db_session=None,
Expand All @@ -389,20 +390,30 @@ def case_update_flow(
ticket_flows.update_case_ticket(case=case, db_session=db_session)

# we update the tactical group if we have a new assignee
if previous_case.assignee.individual.email != case.assignee.individual.email:
group_flows.update_group(
subject=case,
group=case.tactical_group,
group_action=GroupAction.add_member,
group_member=case.assignee.individual.email,
db_session=db_session,
if previous_case.assignee.individual.email != assignee_email:
log.debug(
f"{user_email} updated Case {case_id} assignee from {previous_case.assignee.individual.email} to {assignee_email}"
)
event_service.log_case_event(
case_assign_role_flow(
case_id=case.id,
assignee_email=assignee_email,
assignee_role=ParticipantRoleType.assignee,
db_session=db_session,
source="Dispatch Core App",
description="Case group updated",
case_id=case_id,
)
if case.tactical_group:
group_flows.update_group(
subject=case,
group=case.tactical_group,
group_action=GroupAction.add_member,
group_member=case.assignee.individual.email,
db_session=db_session,
)
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description="Case group updated",
case_id=case_id,
)

# we send the case updated notification
update_conversation(case, db_session)
Expand Down Expand Up @@ -639,3 +650,21 @@ def case_to_incident_endpoint_escalate_flow(
description=f"The members of the incident's tactical group {incident.tactical_group.email} have been given permission to access the case's storage folder",
case_id=case.id,
)


def case_assign_role_flow(
case_id: int,
assignee_email: str,
assignee_role: str,
db_session: SessionLocal,
):
"""Runs the case participant role assignment flow."""

from dispatch.participant_role import flows as role_flow

case = get(case_id=case_id, db_session=db_session)

# we add the assignee to the incident if they're not a participant
case_add_or_reactivate_participant_flow(assignee_email, case.id, db_session=db_session)

role_flow.assign_role_flow(case, assignee_email, assignee_role, db_session)
21 changes: 0 additions & 21 deletions src/dispatch/case/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pydantic.error_wrappers import ErrorWrapper, ValidationError
from typing import List, Optional

from dispatch.auth import service as auth_service
from dispatch.auth.models import DispatchUser
from dispatch.case.priority import service as case_priority_service
from dispatch.case.severity import service as case_severity_service
Expand Down Expand Up @@ -228,26 +227,6 @@ def update(*, db_session, case: Case, case_in: CaseUpdate, current_user: Dispatc
for field in update_data.keys():
setattr(case, field, update_data[field])

if case_in.assignee:
if case.assignee.individual.email != case_in.assignee.individual.email:
case_assignee = auth_service.get_by_email(
db_session=db_session, email=case_in.assignee.individual.email
)
if case_assignee:
case.assignee = case_assignee

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"Case assigned to {case_in.assignee.individual.email} by {current_user.email}",
dispatch_user_id=current_user.id,
case_id=case.id,
)
else:
log.warning(
f"Dispatch user with email address {case_in.assignee.individual.email} not found."
)

if case_in.case_type:
if case.case_type.name != case_in.case_type.name:
case_type = case_type_service.get_by_name(
Expand Down
1 change: 1 addition & 0 deletions src/dispatch/case/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def update_case(
case_update_flow,
case_id=case_id,
previous_case=previous_case,
assignee_email=case_in.assignee.individual.email,
user_email=current_user.email,
organization_slug=organization,
)
Expand Down
6 changes: 4 additions & 2 deletions src/dispatch/participant/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from dispatch.individual import service as individual_service
from dispatch.participant.models import Participant
from dispatch.participant_role import service as participant_role_service
from dispatch.participant_role.models import ParticipantRoleType, ParticipantRoleCreate
from dispatch.participant_role.models import (
ParticipantRoleType,
ParticipantRoleCreate,
)
from dispatch.service import service as service_service

from .service import get_or_create, get_by_incident_id_and_email


Expand Down
24 changes: 24 additions & 0 deletions src/dispatch/participant/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ def get_by_incident_id_and_role(
)


def get_by_case_id_and_role(*, db_session, case_id: int, role: str) -> Optional[Participant]:
"""Get a participant by case id and role name."""
return (
db_session.query(Participant)
.join(ParticipantRole)
.filter(Participant.case_id == case_id)
.filter(ParticipantRole.renounced_at.is_(None))
.filter(ParticipantRole.role == role)
.one_or_none()
)


def get_by_incident_id_and_email(
*, db_session, incident_id: int, email: str
) -> Optional[Participant]:
Expand Down Expand Up @@ -67,6 +79,18 @@ def get_by_incident_id_and_service_id(
)


def get_by_case_id_and_service_id(
*, db_session, case_id: int, service_id: int
) -> Optional[Participant]:
"""Get participant by incident and service id."""
return (
db_session.query(Participant)
.filter(Participant.case_id == case_id)
.filter(Participant.service_id == service_id)
.one_or_none()
)


def get_by_incident_id_and_conversation_id(
*, db_session, incident_id: int, user_conversation_id: str
) -> Optional[Participant]:
Expand Down
77 changes: 49 additions & 28 deletions src/dispatch/participant_role/flows.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import logging
from typing import TYPE_CHECKING
from typing import Any

if TYPE_CHECKING:
from dispatch.models import Incident

from dispatch.database.core import SessionLocal
from dispatch.database.core import SessionLocal, get_table_name_by_class_instance
from dispatch.event import service as event_service
from dispatch.participant import service as participant_service
from dispatch.participant_role.models import ParticipantRoleType

from .models import ParticipantRoleType
from .service import get_all_active_roles, add_role, renounce_role


log = logging.getLogger(__name__)


def assign_role_flow(
incident: "Incident", assignee_email: str, assignee_role: str, db_session: SessionLocal
subject: Any, assignee_email: str, assignee_role: str, db_session: SessionLocal
):
"""Attempts to assign a role to a participant.

Expand All @@ -28,10 +25,17 @@ def assign_role_flow(

"""
# we get the participant for the assignee
assignee_participant = participant_service.get_by_incident_id_and_email(
db_session=db_session, incident_id=incident.id, email=assignee_email
)
subject_type = get_table_name_by_class_instance(subject)
if subject_type == "incident":
assignee_participant = participant_service.get_by_incident_id_and_email(
db_session=db_session, incident_id=subject.id, email=assignee_email
)
if subject_type == "case":
assignee_participant = participant_service.get_by_case_id_and_email(
db_session=db_session, case_id=subject.id, email=assignee_email
)

# Cases don't have observers, so we don't need to handle this
if assignee_role == ParticipantRoleType.observer:
# we make the assignee renounce to the participant role
participant_active_roles = get_all_active_roles(
Expand All @@ -53,18 +57,26 @@ def assign_role_flow(
db_session=db_session,
source="Dispatch Core App",
description=f"{assignee_participant.individual.name} has been assigned the role of {assignee_role}",
incident_id=incident.id,
incident_id=subject.id,
)

return "role_assigned"

# we get the participant that holds the role assigned to the assignee
participant_with_assignee_role = participant_service.get_by_incident_id_and_role(
db_session=db_session, incident_id=incident.id, role=assignee_role
)

if participant_with_assignee_role is assignee_participant:
return "assignee_has_role"
if subject_type == "incident":
participant_with_assignee_role = participant_service.get_by_incident_id_and_role(
db_session=db_session, incident_id=subject.id, role=assignee_role
)
if subject_type == "case":
participant_with_assignee_role = participant_service.get_by_case_id_and_role(
db_session=db_session, case_id=subject.id, role=assignee_role
)

if participant_with_assignee_role and assignee_participant:
if participant_with_assignee_role is assignee_participant:
log.debug(f"{assignee_participant.individual.email} already has role: {assignee_role}")
return "assignee_has_role"

if participant_with_assignee_role:
# we make the participant renounce to the role that has been given to the assignee
Expand Down Expand Up @@ -111,25 +123,34 @@ def assign_role_flow(

# we update the commander, reporter, scribe, or liaison foreign key
if assignee_role == ParticipantRoleType.incident_commander:
incident.commander_id = assignee_participant.id
subject.commander_id = assignee_participant.id
elif assignee_role == ParticipantRoleType.reporter:
incident.reporter_id = assignee_participant.id
subject.reporter_id = assignee_participant.id
elif assignee_role == ParticipantRoleType.scribe:
incident.scribe_id = assignee_participant.id
subject.scribe_id = assignee_participant.id
elif assignee_role == ParticipantRoleType.liaison:
incident.liaison_id = assignee_participant.id
subject.liaison_id = assignee_participant.id
elif assignee_role == ParticipantRoleType.assignee:
subject.assignee_id = assignee_participant.id

# we add and commit the changes
db_session.add(incident)
db_session.add(subject)
db_session.commit()

event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{assignee_participant.individual.name} has been assigned the role of {assignee_role}",
incident_id=incident.id,
)

if subject_type == "incident":
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{assignee_participant.individual.name} has been assigned the role of {assignee_role}",
incident_id=subject.id,
)
if subject_type == "case":
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{assignee_participant.individual.name} has been assigned the role of {assignee_role}",
case_id=subject.id,
)
return "role_assigned"

log.debug(f"We were not able to assign the {assignee_role} role to {assignee_email}.")
Expand Down