Skip to content

Commit

Permalink
Update case timeline on case updates (#2427)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvilanova authored Aug 17, 2022
1 parent 5397631 commit 6147b50
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 10 deletions.
119 changes: 110 additions & 9 deletions src/dispatch/case/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def create(*, db_session, case_in: CaseCreate, current_user: DispatchUser) -> Ca
return case


def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
def update(*, db_session, case: Case, case_in: CaseUpdate, current_user: DispatchUser) -> Case:
"""Updates an existing case."""
update_data = case_in.dict(
skip_defaults=True,
Expand All @@ -221,7 +221,22 @@ def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
for field in update_data.keys():
setattr(case, field, update_data[field])

case.assignee = auth_service.get_by_email(db_session=db_session, email=case_in.assignee.email)
if case.assignee.email != case_in.assignee.email:
case_assignee = auth_service.get_by_email(
db_session=db_session, email=case_in.assignee.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.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.email} not found.")

if case_in.case_type:
if case.case_type.name != case_in.case_type.name:
Expand All @@ -230,7 +245,21 @@ def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
project_id=case.project.id,
name=case_in.case_type.name,
)
case.case_types.append(AssocCaseCaseType(case_type))
if case_type:
case.case_types.append(AssocCaseCaseType(case_type))

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=(
f"Case type changed to {case_in.case_type.name.lower()} "
f"by {current_user.email}"
),
dispatch_user_id=current_user.id,
case_id=case.id,
)
else:
log.warning(f"Case type with name {case_in.case_type.name.lower()} not found.")

if case_in.case_severity:
if case.case_severity.name != case_in.case_severity.name:
Expand All @@ -239,7 +268,23 @@ def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
project_id=case.project.id,
name=case_in.case_severity.name,
)
case.case_severities.append(AssocCaseCaseSeverity(case_severity))
if case_severity:
case.case_severities.append(AssocCaseCaseSeverity(case_severity))

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=(
f"Case severity changed to {case_in.case_severity.name.lower()} "
f"by {current_user.email}"
),
dispatch_user_id=current_user.id,
case_id=case.id,
)
else:
log.warning(
f"Case severity with name {case_in.case_severity.name.lower()} not found."
)

if case_in.case_priority:
if case.case_priority.name != case_in.case_priority.name:
Expand All @@ -248,14 +293,73 @@ def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
project_id=case.project.id,
name=case_in.case_priority.name,
)
case.case_priorities.append(AssocCaseCasePriority(case_priority))
if case_priority:
case.case_priorities.append(AssocCaseCasePriority(case_priority))

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=(
f"Case priority changed to {case_in.case_priority.name.lower()} "
f"by {current_user.email}"
),
dispatch_user_id=current_user.id,
case_id=case.id,
)
else:
log.warning(
f"Case priority with name {case_in.case_priority.name.lower()} not found."
)

if case_in.source:
if case.source.name != case_in.source.name:
case.source = source_service.get_by_name(
case_source = source_service.get_by_name(
db_session=db_session, project_id=case.project.id, name=case_in.source.name
)

if case_source:
case.source = case_source

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

if case.status != case_in.status:
case.status = case_in.status

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=(
f"Case status changed to {case_in.status.lower()} " f"by {current_user.email}"
),
dispatch_user_id=current_user.id,
case_id=case.id,
)

if case.visibility != case_in.visibility:
case.visibility = case_in.visibility

event_service.log_case_event(
db_session=db_session,
source="Dispatch Core App",
description=(
f"Case visibility changed to {case_in.visibility.lower()} "
f"by {current_user.email}"
),
dispatch_user_id=current_user.id,
case_id=case.id,
)

tags = []
for t in case_in.tags:
tags.append(tag_service.get_or_create(db_session=db_session, tag_in=t))
Expand All @@ -276,9 +380,6 @@ def update(*, db_session, case: Case, case_in: CaseUpdate) -> Case:
incidents.append(incident_service.get(db_session=db_session, incident_id=i.id))
case.incidents = incidents

case.status = case_in.status
case.visibility = case_in.visibility

db_session.commit()

return case
Expand Down
4 changes: 3 additions & 1 deletion src/dispatch/case/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def update_case(
previous_case = CaseRead.from_orm(current_case)

# we update the case
case = update(db_session=db_session, case=current_case, case_in=case_in)
case = update(
db_session=db_session, case=current_case, case_in=case_in, current_user=current_user
)

# we run the case update flow
background_tasks.add_task(
Expand Down

0 comments on commit 6147b50

Please sign in to comment.