Skip to content

Commit

Permalink
Close case when caseclose is created
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierbloemen committed Jun 15, 2021
1 parent fb1ea66 commit 1f2b1b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
21 changes: 20 additions & 1 deletion app/apps/camunda/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ class CamundaService:
def __init__(self, rest_url=settings.CAMUNDA_REST_URL):
self.rest_url = rest_url

def _process_request(self, request_path, request_body=None, post=False, put=False):
def _process_request(
self, request_path, request_body=None, post=False, put=False, delete=False
):
request_path = self.rest_url + request_path

try:
# TODO: There needs to be a better way to write this. Works but it aint pretty
if post:
response = requests.post(
request_path,
Expand All @@ -41,6 +44,15 @@ def _process_request(self, request_path, request_body=None, post=False, put=Fals
"API_KEY": settings.CAMUNDA_REST_AUTH,
},
)
elif delete:
response = requests.delete(
request_path,
data=request_body,
headers={
"content-type": "application/json",
"API_KEY": settings.CAMUNDA_REST_AUTH,
},
)
else:
response = requests.get(
request_path,
Expand Down Expand Up @@ -332,3 +344,10 @@ def send_message_to_process_instance(self, message_name, business_key):
response = self._process_request("/message", request_json_body, post=True)

return response

def delete_instance(self, camunda_process_instance_id):
response = self._process_request(
f"/process-instance/{camunda_process_instance_id}"
)

return response.ok
14 changes: 14 additions & 0 deletions app/apps/cases/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ def add_camunda_id(self, camunda_id, *args, **kwargs):
self.save()
return self

def close_case(self):
# close all states just in case
for state in self.case_states.filter(end_date__isnull=True):
state.end_date = timezone.now().date()
state.save()

for camunda_id in self.camunda_ids:
from apps.camunda.services import CamundaService

CamundaService().delete_instance(camunda_id)

self.end_date = timezone.now().date()
self.save()

class Meta:
ordering = ["-start_date"]

Expand Down
8 changes: 7 additions & 1 deletion app/apps/cases/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys

from apps.camunda.services import CamundaService
from apps.cases.models import Case, CitizenReport
from apps.cases.models import Case, CaseClose, CitizenReport
from apps.cases.tasks import start_camunda_instance
from django.conf import settings
from django.db.models.signals import post_save
Expand Down Expand Up @@ -54,3 +54,9 @@ def complete_citizen_report_task(sender, instance, created, **kwargs):
CamundaService().complete_task(
instance.camunda_task_id,
)


@receiver(post_save, sender=CaseClose)
def close_case(sender, instance, created, **kwargs):
if created:
instance.case.close_case()

0 comments on commit 1f2b1b2

Please sign in to comment.