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 7 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
39 changes: 22 additions & 17 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 All @@ -14,6 +13,7 @@
from dispatch.exceptions import NotFoundError
from dispatch.incident import service as incident_service
from dispatch.participant import flows as participant_flows
from dispatch.participant_role import flows as role_flows
from dispatch.participant_role.models import ParticipantRoleType
from dispatch.project import service as project_service
from dispatch.service import flows as service_flows
Expand Down Expand Up @@ -230,23 +230,28 @@ 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
# We change the current assignee role to participant
role_flows.change_role_flow(
db_session=db_session,
case=case,
wssheldon marked this conversation as resolved.
Show resolved Hide resolved
user_email=case.assignee.individual.email,
from_role=ParticipantRoleType.assignee,
to_role=ParticipantRoleType.participant,
)
# We add the new participant as the assignee
participant_flows.add_participant(
case_in.assignee.individual.email,
case,
db_session,
role=ParticipantRoleType.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,
)
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
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
11 changes: 11 additions & 0 deletions 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 case id and email."""
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
56 changes: 52 additions & 4 deletions src/dispatch/participant_role/flows.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import logging
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from dispatch.models import Incident

from dispatch.database.core import SessionLocal
from dispatch.database.core import Session, SessionLocal, get_table_name_by_class_instance
from dispatch.event import service as event_service
from dispatch.participant import service as participant_service

from .models import ParticipantRoleType
from dispatch.participant_role import service as participant_role_service
from dispatch.participant_role.models import (
ParticipantRoleType,
ParticipantRoleUpdate,
)
from .service import get_all_active_roles, add_role, renounce_role


Expand Down Expand Up @@ -135,3 +138,48 @@ def assign_role_flow(
log.debug(f"We were not able to assign the {assignee_role} role to {assignee_email}.")

return "role_not_assigned"


def change_role_flow(
db_session: Session,
subject: Any,
user_email: str,
from_role: ParticipantRoleType,
to_role: ParticipantRoleType = ParticipantRoleType.participant,
):
"""Changes a participants role."""
subject_type = get_table_name_by_class_instance(subject)
if subject_type == "case":
participant = participant_service.get_by_case_id_and_email(
db_session=db_session,
case_id=subject.id,
email=user_email,
)
if subject_type == "incident":
participant = participant_service.get_by_incident_id_and_email(
db_session=db_session,
incident_id=subject.id,
email=user_email,
)

log.debug(f"Changing {participant.individual.name}'s role from {from_role} to {to_role}...")

participant_roles = [r.role for r in participant.participant_roles]
if from_role not in participant_roles:
log.debug(
f"{participant.individual.name}'s does not have the role {from_role} that was set to be changed."
)
return

for role in participant.participant_roles:
if from_role == role.role:
from_participant_role = role

log.debug(f"{participant.individual.name}'s role changed from {from_role} to {to_role}...")

participant_role_in = ParticipantRoleUpdate(role=to_role)
participant_role_service.update(
db_session=db_session,
participant_role=from_participant_role,
participant_role_in=participant_role_in,
)