Skip to content

Commit

Permalink
Merge pull request #592 from maykinmedia/fix/544-missing-zaak
Browse files Browse the repository at this point in the history
[#544] Delete destruction list items no longer related to a zaak
  • Loading branch information
SilviaAmAm authored Jan 7, 2025
2 parents 0411a9c + 20eabd2 commit ace1f2b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
16 changes: 14 additions & 2 deletions backend/src/openarchiefbeheer/destruction/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase

from timeline_logger.models import TimelineLog

from openarchiefbeheer.accounts.tests.factories import UserFactory
from openarchiefbeheer.zaken.tests.factories import ZaakFactory

from ..constants import ListRole
from ..models import DestructionListItem
from ..utils import process_new_reviewer, resync_items_and_zaken
from .factories import (
DestructionListAssigneeFactory,
Expand Down Expand Up @@ -48,12 +52,20 @@ def test_resync_zaken_missing(self):
ZaakFactory.create(url="http://zaken.nl/2")
item1 = DestructionListItemFactory.create(_zaak_url="http://zaken.nl/1")
item2 = DestructionListItemFactory.create(_zaak_url="http://zaken.nl/3")
item2_pk = item2.pk
destruction_list2 = item2.destruction_list

resync_items_and_zaken()

item1.refresh_from_db()
item2.refresh_from_db()

self.assertIsNotNone(item1.zaak)
self.assertEqual(item1.zaak.url, "http://zaken.nl/1")
self.assertIsNone(item2.zaak)
with self.assertRaises(ObjectDoesNotExist):
DestructionListItem.objects.get(pk=item2_pk)

logs = TimelineLog.objects.for_object(destruction_list2)

self.assertEqual(logs.count(), 1)
self.assertEqual(logs[0].extra_data["number_deleted_items"], 1)
self.assertEqual(logs[0].extra_data["number_of_zaken"], 0)
19 changes: 19 additions & 0 deletions backend/src/openarchiefbeheer/destruction/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from openarchiefbeheer.config.models import ArchiveConfig
from openarchiefbeheer.emails.models import EmailConfig
from openarchiefbeheer.emails.render_backend import get_sandboxed_backend
from openarchiefbeheer.logging import logevent
from openarchiefbeheer.selection.models import SelectionItem
from openarchiefbeheer.utils.results_store import ResultStore
from openarchiefbeheer.zaken.models import Zaak
Expand Down Expand Up @@ -137,12 +138,30 @@ def process_new_reviewer(


def resync_items_and_zaken() -> None:
# Using the _zaak_url field, link the item to the zaak again
DestructionListItem.objects.filter(~Q(_zaak_url=""), zaak__isnull=True).update(
zaak_id=Subquery(
Zaak.objects.filter(url=OuterRef("_zaak_url")).values("pk")[:1]
)
)

# If the cases could not be linked again, the zaak does not exist anymore.
# We need to delete them and log the deletion from the destruction list
orphan_items = DestructionListItem.objects.filter(
~Q(_zaak_url=""), zaak__isnull=True
)
destruction_lists = DestructionList.objects.filter(
pk__in=orphan_items.distinct("destruction_list").values_list(
"destruction_list", flat=True
)
)
for destruction_list in destruction_lists:
items_in_list = orphan_items.filter(destruction_list=destruction_list)

number_deleted_items, _ = items_in_list.delete()

logevent.destruction_list_items_deleted(destruction_list, number_deleted_items)


def create_zaak_for_report(
destruction_list: DestructionList, store: ResultStore
Expand Down
15 changes: 15 additions & 0 deletions backend/src/openarchiefbeheer/logging/logevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ def destruction_list_deletion_triggered(
)


def destruction_list_items_deleted(
destruction_list: DestructionList, number_deleted_items: int
) -> None:
_create_log(
model=destruction_list,
event="destruction_list_items_deleted",
extra_data={
"number_deleted_items": number_deleted_items,
"number_of_zaken": destruction_list.items.filter(
status=ListItemStatus.suggested
).count(),
},
)


def resync_started() -> None:
return TimelineLog.objects.create(
template="logging/resync_started.txt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load i18n %}{% blocktranslate trimmed with list_name=log.content_object.name %}
List "{{ list_name }}" has been updated after resyncing the cases with Open Zaak.
{% endblocktranslate %}
{% blocktranslate trimmed with number_deleted_items=log.extra_data.number_deleted_items count counter=log.extra_data.number_deleted_items %}
One item was deleted.{% plural %}{{ number_deleted_items }} items were deleted.
{% endblocktranslate %}
{% blocktranslate trimmed with number_of_zaken=log.extra_data.number_of_zaken count counter=log.extra_data.number_of_zaken %}
There is now one zaak on the list.{% plural %}There are now {{ number_of_zaken }} zaken on the list.
{% endblocktranslate %}

0 comments on commit ace1f2b

Please sign in to comment.