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 2 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
33 changes: 17 additions & 16 deletions src/dispatch/case/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,24 @@ def update(*, db_session, case: Case, case_in: CaseUpdate, current_user: Dispatc

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
participant_flows.add_participant(
case_in.assignee.individual.email,
case,
db_session,
role=ParticipantRoleType.assignee,
)
participant_flows.remove_participant(
case.assignee.individual.email,
case,
db_session,
)
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,
)
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:
Expand Down
82 changes: 55 additions & 27 deletions src/dispatch/participant/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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
from .service import get_or_create, get_by_incident_id_and_email, get_by_case_id_and_email


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -89,43 +89,63 @@ def add_participant(
return participant


def remove_participant(user_email: str, incident: Incident, db_session: SessionLocal):
def remove_participant(user_email: str, subject: Subject, db_session: SessionLocal):
"""Removes a participant."""
inactivated = inactivate_participant(user_email, incident, db_session)
inactivated = inactivate_participant(user_email, subject, db_session)

subject_type = get_table_name_by_class_instance(subject)
if inactivated:
participant = get_by_incident_id_and_email(
db_session=db_session, incident_id=incident.id, email=user_email
)
if subject_type == "incident":
participant = get_by_incident_id_and_email(
db_session=db_session, incident_id=subject.id, email=user_email
)
if subject_type == "case":
participant = get_by_case_id_and_email(
db_session=db_session, case_id=subject.id, email=user_email
)

log.debug(f"Removing {participant.individual.name} from {incident.name} incident...")
log.debug(f"Removing {participant.individual.name} from {subject.name}...")

participant.service = None

db_session.add(participant)
db_session.commit()

event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been removed",
incident_id=incident.id,
)


def inactivate_participant(user_email: str, incident: Incident, db_session: SessionLocal):
if subject_type == "incident":
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been removed",
incident_id=subject.id,
)
if subject_type == "case":
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been removed",
case_id=subject.id,
)


def inactivate_participant(user_email: str, subject: Subject, db_session: SessionLocal):
"""Inactivates a participant."""
participant = get_by_incident_id_and_email(
db_session=db_session, incident_id=incident.id, email=user_email
)
subject_type = get_table_name_by_class_instance(subject)
if subject_type == "incident":
participant = get_by_incident_id_and_email(
db_session=db_session, incident_id=subject.id, email=user_email
)
if subject_type == "case":
participant = get_by_case_id_and_email(
db_session=db_session, case_id=subject.id, email=user_email
)

if not participant:
log.debug(
f"Can't inactivate participant with {user_email} email. They're not a participant of {incident.name} incident."
f"Can't inactivate participant with {user_email} email. They're not a participant of {subject.name}."
)
return False

log.debug(f"Inactivating {participant.individual.name} from {incident.name} incident...")
log.debug(f"Inactivating {participant.individual.name} from {subject.name}...")

participant_active_roles = participant_role_service.get_all_active_roles(
db_session=db_session, participant_id=participant.id
Expand All @@ -135,12 +155,20 @@ def inactivate_participant(user_email: str, incident: Incident, db_session: Sess
db_session=db_session, participant_role=participant_active_role
)

event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been inactivated",
incident_id=incident.id,
)
if subject_type == "incident":
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been inactivated",
incident_id=subject.id,
)
if subject_type == "case":
event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=f"{participant.individual.name} has been inactivated",
case_id=subject.id,
)

return True

Expand Down
13 changes: 12 additions & 1 deletion src/dispatch/participant/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ def get_by_incident_id_and_email(
)


def get_by_case_id_and_email(*, db_session, case_id: int, email: str) -> Optional[Participant]:
"""Get a participant by incident id and email."""
wssheldon marked this conversation as resolved.
Show resolved Hide resolved
return (
db_session.query(Participant)
.join(IndividualContact)
.filter(Participant.case_id == case_id)
.filter(IndividualContact.email == email)
.one_or_none()
)


def get_by_incident_id_and_service_id(
*, db_session, incident_id: int, service_id: int
) -> Optional[Participant]:
Expand Down Expand Up @@ -84,7 +95,7 @@ def get_or_create(
subject_id: int,
subject_type: str,
individual_id: int,
service_id: int,
service_id: int = None,
participant_roles: List[ParticipantRoleCreate],
) -> Participant:
"""Gets an existing participant object or creates a new one."""
Expand Down