Skip to content

Commit

Permalink
Merge pull request #841 from uktrade/LTD-1357-Mgmt-command-change-status
Browse files Browse the repository at this point in the history
LTD-1357: Add management command to change case status
  • Loading branch information
saruniitr authored Oct 22, 2021
2 parents c04d466 + 64a5d6d commit 62223e9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
67 changes: 67 additions & 0 deletions api/support/management/commands/change_case_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging

from django.core.management.base import BaseCommand

from api.audit_trail import service as audit_trail_service
from api.audit_trail.enums import AuditType
from api.applications.helpers import get_application_update_serializer
from api.applications.libraries.get_applications import get_application
from api.cases.models import Case
from api.staticdata.statuses.libraries.get_case_status import get_case_status_by_status
from api.users.models import BaseUser


class Command(BaseCommand):
help = """
Command to change the status of a Case.
This can be changed from UI but not possible for some status values (eg finalised).
Also when changed from UI the system runs all routing rules but in this case we only
update the status and not change the routing rules.
"""

def add_arguments(self, parser):
parser.add_argument(
"case_reference", type=str, help="Reference number of the application",
)
parser.add_argument(
"status", type=str, help="Required status of the application after update",
)
parser.add_argument(
"--dry_run", action="store_true", help="Print out what action will happen without applying any changes"
)

def handle(self, *args, **options):
case_reference = options.pop("case_reference")
status = options.pop("status")
dry_run = options["dry_run"]
logging.info(f"Given case reference is: {case_reference}")

try:
case = Case.objects.get(reference_code=case_reference)
application = get_application(case.id)
except Case.DoesNotExist:
logging.error(f"Case ({case_reference}) not found, please provide valid case reference")
return

prev_status = application.status.status
serializer = get_application_update_serializer(application)
case_status = get_case_status_by_status(status)
data = {"status": str(case_status.pk)}
serializer = serializer(application, data=data, partial=True)
if not serializer.is_valid():
logging.error(f"Error updating the status for {case_reference}: {serializer.errors}")
return

if not dry_run:
application = serializer.save()

system_user = BaseUser.objects.get(id="00000000-0000-0000-0000-000000000001")
audit_trail_service.create(
actor=system_user,
verb=AuditType.UPDATED_STATUS,
target=application.get_case(),
payload={"status": {"new": status, "old": prev_status,}, "additional_text": "",},
)

logging.info(f"Case {case_reference} status changed from {prev_status} to {status}")
23 changes: 23 additions & 0 deletions api/support/management/commands/tests/test_change_case_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.core.management import call_command

from test_helpers.clients import DataTestClient


class ChangeCaseStatusMgmtCommandTests(DataTestClient):
def test_case_status_change_command(self):
self.application = self.create_draft_standard_application(self.organisation)
self.submit_application(self.application)
self.assertEqual(self.application.status.status, "submitted")

call_command("change_case_status", self.application.reference_code, "finalised")
self.application.refresh_from_db()
self.assertEqual(self.application.status.status, "finalised")

def test_case_status_change_command_dry_run(self):
self.application = self.create_draft_standard_application(self.organisation)
self.submit_application(self.application)
self.assertEqual(self.application.status.status, "submitted")

call_command("change_case_status", self.application.reference_code, "finalised", "--dry_run")
self.application.refresh_from_db()
self.assertEqual(self.application.status.status, "submitted")

0 comments on commit 62223e9

Please sign in to comment.