Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create new user task form only if user task form referenced #85

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
* 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 @@


/**
* 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 @@

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