Fix workflow assignment of customfield fails if field exists

This commit is contained in:
shamoon 2024-01-07 10:42:26 -08:00
parent 9f6613fe05
commit 17c2dfa5bf
2 changed files with 46 additions and 4 deletions

View File

@ -610,10 +610,18 @@ def run_workflow(
if action.assign_custom_fields is not None: if action.assign_custom_fields is not None:
for field in action.assign_custom_fields.all(): for field in action.assign_custom_fields.all():
if (
CustomFieldInstance.objects.filter(
field=field,
document=document,
).count()
== 0
):
# can be triggered on existing docs, so only add the field if it doesnt already exist
CustomFieldInstance.objects.create( CustomFieldInstance.objects.create(
field=field, field=field,
document=document, document=document,
) # adds to document )
document.save() document.save()

View File

@ -13,6 +13,7 @@ from documents.data_models import DocumentSource
from documents.matching import document_matches_workflow from documents.matching import document_matches_workflow
from documents.models import Correspondent from documents.models import Correspondent
from documents.models import CustomField from documents.models import CustomField
from documents.models import CustomFieldInstance
from documents.models import Document from documents.models import Document
from documents.models import DocumentType from documents.models import DocumentType
from documents.models import MatchingModel from documents.models import MatchingModel
@ -997,6 +998,39 @@ class TestWorkflows(DirectoriesMixin, FileSystemAssertsMixin, APITestCase):
self.assertEqual(doc.custom_fields.all().count(), 1) self.assertEqual(doc.custom_fields.all().count(), 1)
def test_document_updated_workflow_existing_custom_field(self):
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
filter_has_document_type=self.dt,
)
action = WorkflowAction.objects.create()
action.assign_custom_fields.add(self.cf1)
w = Workflow.objects.create(
name="Workflow 1",
order=0,
)
w.triggers.add(trigger)
w.actions.add(action)
w.save()
doc = Document.objects.create(
title="sample test",
correspondent=self.c,
original_filename="sample.pdf",
)
CustomFieldInstance.objects.create(document=doc, field=self.cf1)
superuser = User.objects.create_superuser("superuser")
self.client.force_authenticate(user=superuser)
self.client.patch(
f"/api/documents/{doc.id}/",
{"document_type": self.dt.id},
format="json",
)
self.assertEqual(doc.custom_fields.all().count(), 1)
def test_workflow_enabled_disabled(self): def test_workflow_enabled_disabled(self):
trigger = WorkflowTrigger.objects.create( trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED, type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,