A platform for community discussion. Free, open, simple.
FIX: improves and simplifies updating workflow (#41026)
Joffrey JAFFEUX8aab01e755ed
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjsindex e3712bfdf45..9c4115ece7b 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs@@ -119,7 +119,6 @@ export default class WorkflowCanvas extends Component {type: "PUT",data: {workflow: {- name: this.args.workflowName,published: true,},},@@ -180,7 +179,6 @@ export default class WorkflowCanvas extends Component {type: "PUT",data: {workflow: {- name: this.args.workflowName,published: false,},},diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjsindex 84402c9ae46..0c298bcdc57 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs@@ -1403,7 +1403,6 @@ export default class WorkflowsEditor extends Component {async #saveWorkflow(options = {}) {this.saving = true;try {- const name = this.formApi.get("name");const nodes = this.formApi.get("nodes");const connections = normalizeConnectionsForNodes(this.formApi.get("connections"),@@ -1413,14 +1412,21 @@ export default class WorkflowsEditor extends Component {const stickyNotes = this.formApi.get("stickyNotes");this.formApi.set("connections", connections);- this.args.workflow.setProperties({- name,+ const workflowProperties = {nodes,connections,stickyNotes: stickyNotes || [],- });+ };++ if (this.args.isNew) {+ workflowProperties.name = this.formApi.get("name");+ }++ this.args.workflow.setProperties(workflowProperties);- const saveProperties = this.args.workflow.updateProperties();+ const saveProperties = this.args.isNew+ ? this.args.workflow.createProperties()+ : this.args.workflow.graphProperties();if (options.staticData !== undefined) {saveProperties.static_data = options.staticData;}@@ -1503,7 +1509,6 @@ export default class WorkflowsEditor extends Component {@connections={{transientData.connections}}@stickyNotes={{transientData.stickyNotes}}@workflowId={{@workflow.id}}- @workflowName={{@workflow.name}}@autoArrangeRequest={{this.autoArrangeRequest}}@onUpdateNodePosition={{this.updateNodePosition}}@onEditNode={{this.editNode}}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/settings.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/settings.gjsindex 5eb792f59f2..495ae362dfa 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/settings.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/settings.gjs@@ -57,7 +57,6 @@ export default class WorkflowSettings extends Component {type: "PUT",data: {workflow: {- name: this.args.workflow.name,error_workflow_id: data.errorWorkflowId,timezone: data.timezone,},diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/models/discourse-workflows-workflow.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/models/discourse-workflows-workflow.jsindex b7033133a42..c72d1f9b4d9 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/models/discourse-workflows-workflow.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/models/discourse-workflows-workflow.js@@ -73,11 +73,8 @@ export default class DiscourseWorkflowsWorkflow extends RestModel {@tracked connections = [];@tracked stickyNotes = [];- updateProperties() {+ graphProperties() {return {- name: this.name,- error_workflow_id: this.errorWorkflowId,- static_data: this.staticData,nodes: [...this.nodes.map(WorkflowNode.serialize),...(this.stickyNotes || []).map(StickyNote.serialize),@@ -90,6 +87,9 @@ export default class DiscourseWorkflowsWorkflow extends RestModel {}createProperties() {- return this.updateProperties();+ return {+ name: this.name,+ ...this.graphProperties(),+ };}}diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/update.rb b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/update.rbindex c7d03f695e8..14e016d32c4 100644--- a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/update.rb+++ b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/update.rb@@ -4,42 +4,45 @@ module DiscourseWorkflowsclass Workflow::Updateinclude Service::Base- TIMEZONE_NOT_PROVIDED = Object.new.freeze- STATIC_DATA_NOT_PROVIDED = Object.new.freeze- ERROR_WORKFLOW_ID_NOT_PROVIDED = Object.new.freeze+ NOT_PROVIDED = Object.new.freezeparams doattribute :workflow_id, :integer- attribute :name, :string- attribute :error_workflow_id, default: -> { ERROR_WORKFLOW_ID_NOT_PROVIDED }- attribute :timezone, default: -> { TIMEZONE_NOT_PROVIDED }- attribute :static_data, default: -> { STATIC_DATA_NOT_PROVIDED }+ attribute :name, default: -> { NOT_PROVIDED }+ attribute :error_workflow_id, default: -> { NOT_PROVIDED }+ attribute :timezone, default: -> { NOT_PROVIDED }+ attribute :static_data, default: -> { NOT_PROVIDED }attribute :nodesattribute :connectionsattribute :autosaved, :boolean, default: falsevalidates :workflow_id, presence: true- validates :name, presence: true, length: { maximum: 100 }+ validates :name, presence: true, length: { maximum: 100 }, if: :name_provided?validate :timezone_is_validvalidate :static_data_is_valid_mapdef updatable_attributes- attrs = { name: }+ attrs = {}+ attrs[:name] = name if name_provided?attrs[:error_workflow_id] = error_workflow_id if error_workflow_id_provided?attrs[:static_data] = static_data if static_data_provided?attrsend+ def name_provided?+ name != NOT_PROVIDED+ end+def error_workflow_id_provided?- error_workflow_id != ERROR_WORKFLOW_ID_NOT_PROVIDED+ error_workflow_id != NOT_PROVIDEDenddef timezone_provided?- timezone != TIMEZONE_NOT_PROVIDED+ timezone != NOT_PROVIDEDenddef static_data_provided?- static_data != STATIC_DATA_NOT_PROVIDED+ static_data != NOT_PROVIDEDenddef graph_data_provided?diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/workflows_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/workflows_controller_spec.rbindex d92af8ee945..701cc0d08f4 100644--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/workflows_controller_spec.rb+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/workflows_controller_spec.rb@@ -184,7 +184,6 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doput "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- name: workflow.name,published: true,},}@@ -201,7 +200,6 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doput "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- name: workflow.name,published: false,},}@@ -218,7 +216,6 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doput "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- name: workflow.name,error_workflow_id: error_wf.id,},}@@ -234,7 +231,6 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doput "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- name: workflow.name,error_workflow_id: workflow.id,},}@@ -251,7 +247,6 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doput "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- name: workflow.name,error_workflow_id: nil,},}@@ -287,13 +282,30 @@ RSpec.describe DiscourseWorkflows::WorkflowsController doexpect(workflow.reload.error_workflow_id).to eq(error_wf.id)end- it "returns 400 when name is missing" do+ it "updates graph data without changing the name when name is omitted" do+ workflow = Fabricate(:discourse_workflows_workflow, created_by: admin, name: "Original")++ put "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",+ params: {+ workflow: {+ nodes: [{ id: "t1", type: "trigger:manual", name: "Manual" }],+ connections: {+ },+ },+ }++ expect(response).to have_http_status(:ok)+ expect(response.parsed_body["workflow"]).to include("name" => "Original")+ expect(workflow.reload.nodes.first["name"]).to eq("Manual")+ end++ it "returns 400 when name is blank" doworkflow = Fabricate(:discourse_workflows_workflow, created_by: admin)put "/admin/plugins/discourse-workflows/workflows/#{workflow.id}.json",params: {workflow: {- nodes: [],+ name: "",},}diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/update_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/update_spec.rbindex 5dd890a0333..c9cd16d9498 100644--- a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/update_spec.rb+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/update_spec.rb@@ -3,8 +3,19 @@RSpec.describe DiscourseWorkflows::Workflow::Update dodescribe described_class::Contract, type: :model doit { is_expected.to validate_presence_of(:workflow_id) }- it { is_expected.to validate_presence_of(:name) }it { is_expected.to validate_length_of(:name).is_at_most(100) }++ it "allows name to be omitted" do+ contract = described_class.new(workflow_id: 1, nodes: [], connections: {})++ expect(contract).to be_valid+ end++ it "requires name to be present when provided" do+ contract = described_class.new(workflow_id: 1, name: "")++ expect(contract).not_to be_valid+ endenddescribe ".call" dodiff --git a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas-test.js b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas-test.jsindex a73fb3c7801..c739e58602a 100644--- a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas-test.js@@ -61,7 +61,6 @@ module("Unit | Component | workflows canvas", function (hooks) {value: {workflow,workflowId: 1,- workflowName: "Workflow",nodes: [{ clientId: "node-1", type: "trigger:manual" }],get workflowPublished() {return workflow.activeVersionId;@@ -131,7 +130,6 @@ module("Unit | Component | workflows canvas", function (hooks) {value: {workflow,workflowId: 1,- workflowName: "Workflow",nodes: [{ clientId: "node-1", type: "trigger:manual" }],get workflowPublished() {return workflow.activeVersionId;@@ -246,7 +244,6 @@ module("Unit | Component | workflows canvas", function (hooks) {Object.defineProperty(canvas, "args", {value: {workflowId: 1,- workflowName: "Workflow",nodes: [],workflowPublished: null,hasUnpublishedChanges: true,diff --git a/plugins/discourse-workflows/test/javascripts/unit/models/discourse-workflows-workflow-test.js b/plugins/discourse-workflows/test/javascripts/unit/models/discourse-workflows-workflow-test.jsindex d235cc5fe01..320cf442f24 100644--- a/plugins/discourse-workflows/test/javascripts/unit/models/discourse-workflows-workflow-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/models/discourse-workflows-workflow-test.js@@ -43,20 +43,16 @@ module("Unit | model | discourse-workflows-workflow", function () {});});- test("updateProperties maps model fields to API snake_case fields", function (assert) {+ test("createProperties maps model fields to API snake_case fields", function (assert) {const workflow = DiscourseWorkflowsWorkflow.create({name: "Workflow",- errorWorkflowId: 2,- staticData: { global: { cursor: "abc" } },nodes: [],stickyNotes: [],connections: [],});- assert.deepEqual(workflow.updateProperties(), {+ assert.deepEqual(workflow.createProperties(), {name: "Workflow",- error_workflow_id: 2,- static_data: { global: { cursor: "abc" } },nodes: [],connections: {},});