Skip to content

Commit

Permalink
fix: create new user task form only if user task form referenced
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Nov 1, 2024
1 parent ba9f4a5 commit 32a85d3
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 98 deletions.
85 changes: 48 additions & 37 deletions lib/camunda-cloud/FormsBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import { createElement } from '../util/ElementUtil';
import { getExtensionElementsList } from '../util/ExtensionElementsUtil';

import {
getBusinessObject,
is
} from 'bpmn-js/lib/util/ModelUtil';
import { is } from 'bpmn-js/lib/util/ModelUtil';

import {
createUserTaskFormId,
formKeyToUserTaskFormId,
getFormDefinition,
getRootElement,
getUserTaskForm,
Expand All @@ -24,7 +22,7 @@ import {
* Zeebe BPMN specific forms behavior.
*/
export default class FormsBehavior extends CommandInterceptor {
constructor(bpmnFactory, eventBus, modeling) {
constructor(bpmnFactory, elementRegistry, eventBus, modeling) {
super(eventBus);

this._modeling = modeling;
Expand Down Expand Up @@ -67,67 +65,79 @@ export default class FormsBehavior extends CommandInterceptor {


/**
* Create new zeebe:FormDefinition and zeebe:UserTaskForm on user task created.
* Create and reference new zeebe:UserTaskForm when user task is created
* that references existing zeebe:UserTaskForm that is already referenced by
* existing user task.
*/
this.postExecute('shape.create', function(context) {
const { shape } = context;

if (!is(shape, 'bpmn:UserTask')) {
return;
}

const oldFormDefinition = getFormDefinition(shape);

if (!is(shape, 'bpmn:UserTask') || !oldFormDefinition) {
if (!oldFormDefinition) {
return;
}

const oldUserTaskForm = getUserTaskForm(shape);

const rootElement = getRootElement(shape);

const businessObject = getBusinessObject(shape);
if (!oldUserTaskForm) {
return;
}

const extensionElements = businessObject.get('extensionElements');
const isReferenced = elementRegistry.filter(element => {
if (element === shape) {
return false;
}

let rootExtensionElements = rootElement.get('extensionElements');
const formDefinition = getFormDefinition(element);

// (1) ensure extension elements exists
if (!rootExtensionElements) {
rootExtensionElements = createElement('bpmn:ExtensionElements', { values: [] }, rootElement, bpmnFactory);
return formDefinition
&& formDefinition.get('formKey')
&& formKeyToUserTaskFormId(formDefinition.get('formKey')) === oldUserTaskForm.get('id');
});

modeling.updateModdleProperties(shape, rootElement, { extensionElements: rootExtensionElements });
if (!isReferenced.length) {
return;
}

// (2) remove existing form definition
let values = extensionElements.get('values').filter((element) => {
return element !== oldFormDefinition;
});
const rootElement = getRootElement(shape);

// (3) create new form definition
const userTaskFormId = createUserTaskFormId();
let extensionElements = rootElement.get('extensionElements');

const newFormDefinition = createElement('zeebe:FormDefinition', {
formKey: userTaskFormIdToFormKey(userTaskFormId)
}, extensionElements, bpmnFactory);
// (1) ensure extension elements exist
if (!extensionElements) {
extensionElements = createElement('bpmn:ExtensionElements', {

Check warning on line 113 in lib/camunda-cloud/FormsBehavior.js

View check run for this annotation

Codecov / codecov/patch

lib/camunda-cloud/FormsBehavior.js#L113

Added line #L113 was not covered by tests
values: []
}, rootElement, bpmnFactory);

values = [
...values,
newFormDefinition
];
modeling.updateModdleProperties(shape, rootElement, {

Check warning on line 117 in lib/camunda-cloud/FormsBehavior.js

View check run for this annotation

Codecov / codecov/patch

lib/camunda-cloud/FormsBehavior.js#L117

Added line #L117 was not covered by tests
extensionElements
});
}

modeling.updateModdleProperties(shape, extensionElements, {
values
});
// (2) create new user task form
const userTaskFormId = createUserTaskFormId();

// (4) create new user task form
const userTaskForm = createElement('zeebe:UserTaskForm', {
id: userTaskFormId,
body: oldUserTaskForm ? oldUserTaskForm.get('body') : ''
}, rootExtensionElements, bpmnFactory);
body: oldUserTaskForm.get('body')
}, extensionElements, bpmnFactory);

modeling.updateModdleProperties(shape, rootExtensionElements, {
modeling.updateModdleProperties(shape, extensionElements, {
values: [
...(rootExtensionElements.get('values') || []),
...(extensionElements.get('values') || []),
userTaskForm
]
});

// (3) reference new user task form
modeling.updateModdleProperties(shape, oldFormDefinition, {
formKey: userTaskFormIdToFormKey(userTaskFormId)
});
}, true);


Expand Down Expand Up @@ -277,6 +287,7 @@ export default class FormsBehavior extends CommandInterceptor {

FormsBehavior.$inject = [
'bpmnFactory',
'elementRegistry',
'eventBus',
'modeling'
];
Expand Down
Loading

0 comments on commit 32a85d3

Please sign in to comment.