From a4e49e7bbce7efa5f2db62fa5b7a72a404ab5345 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 15 Mar 2023 13:33:07 +0100 Subject: [PATCH 1/4] Ensure submission users are serialized properly --- .../custom_wizard/submission_serializer.rb | 2 +- lib/custom_wizard/submission.rb | 8 +++++++- plugin.rb | 2 +- .../submission_serializer_spec.rb | 20 +++++++++++++------ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/serializers/custom_wizard/submission_serializer.rb b/app/serializers/custom_wizard/submission_serializer.rb index 48892c21..ed9ad411 100644 --- a/app/serializers/custom_wizard/submission_serializer.rb +++ b/app/serializers/custom_wizard/submission_serializer.rb @@ -10,7 +10,7 @@ class CustomWizard::SubmissionSerializer < ApplicationSerializer end def user - ::BasicUserSerializer.new(object.wizard.user).as_json + ::BasicUserSerializer.new(object.wizard.user, root: false).as_json end def fields diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index fc10cf31..edd0e1c9 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -120,15 +120,21 @@ class CustomWizard::Submission end def self.list(wizard, order_by: nil, page: nil) + list_actor_id = wizard.actor_id + list_user = wizard.user if wizard.user.present? + params = { plugin_name: "#{wizard.id}_#{KEY}" } - params[:key] = wizard.actor_id if wizard.actor_id + params[:key] = list_actor_id if list_actor_id query = PluginStoreRow.where(params) result = OpenStruct.new(submissions: [], total: nil) query.each do |record| if (submission_data = ::JSON.parse(record.value)).any? + submission_user = list_user || User.find_by(id: record.key.to_i) + submission_data.each do |data| + wizard.user = submission_user if submission_user.present? result.submissions.push(new(wizard, data)) end end diff --git a/plugin.rb b/plugin.rb index 7f6829eb..6b13632a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.2.7 +# version: 2.2.8 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/serializers/custom_wizard/submission_serializer_spec.rb b/spec/serializers/custom_wizard/submission_serializer_spec.rb index 02d8be8a..7853f768 100644 --- a/spec/serializers/custom_wizard/submission_serializer_spec.rb +++ b/spec/serializers/custom_wizard/submission_serializer_spec.rb @@ -3,7 +3,8 @@ require_relative '../../plugin_helper' describe CustomWizard::SubmissionSerializer do - fab!(:user) { Fabricate(:user) } + fab!(:user1) { Fabricate(:user) } + fab!(:user2) { Fabricate(:user) } let(:template_json) { JSON.parse(File.open( @@ -13,29 +14,36 @@ describe CustomWizard::SubmissionSerializer do before do CustomWizard::Template.save(template_json, skip_jobs: true) - wizard = CustomWizard::Wizard.create(template_json["id"], user) + wizard = CustomWizard::Wizard.create(template_json["id"], user1) CustomWizard::Submission.new(wizard, step_1_field_1: "I am user submission", submitted_at: Time.now.iso8601 ).save - @list = CustomWizard::Submission.list(wizard, page: 0) end it 'should return submission attributes' do + wizard = CustomWizard::Wizard.create(template_json["id"]) + list = CustomWizard::Submission.list(wizard, page: 0) + json_array = ActiveModel::ArraySerializer.new( - @list.submissions, + list.submissions, each_serializer: described_class ).as_json expect(json_array.length).to eq(1) expect(json_array[0][:id].present?).to eq(true) - expect(json_array[0][:user].present?).to eq(true) + expect(json_array[0][:user]).to eq( + BasicUserSerializer.new(user1, root: false).as_json + ) expect(json_array[0][:submitted_at].present?).to eq(true) end it "should return field values, types and labels" do + wizard = CustomWizard::Wizard.create(template_json["id"]) + list = CustomWizard::Submission.list(wizard, page: 0) + json_array = ActiveModel::ArraySerializer.new( - @list.submissions, + list.submissions, each_serializer: described_class ).as_json From ef1a8d1457485b8076f04f826f0abf91f440e879 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 16 Mar 2023 17:44:10 +0100 Subject: [PATCH 2/4] Ensure each wizard has the right user --- lib/custom_wizard/submission.rb | 5 +++-- .../submission_serializer_spec.rb | 20 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index edd0e1c9..38cad982 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -134,8 +134,9 @@ class CustomWizard::Submission submission_user = list_user || User.find_by(id: record.key.to_i) submission_data.each do |data| - wizard.user = submission_user if submission_user.present? - result.submissions.push(new(wizard, data)) + _wizard = wizard.clone + _wizard.user = submission_user if submission_user.present? + result.submissions.push(new(_wizard, data)) end end end diff --git a/spec/serializers/custom_wizard/submission_serializer_spec.rb b/spec/serializers/custom_wizard/submission_serializer_spec.rb index 7853f768..2fa000e8 100644 --- a/spec/serializers/custom_wizard/submission_serializer_spec.rb +++ b/spec/serializers/custom_wizard/submission_serializer_spec.rb @@ -14,11 +14,12 @@ describe CustomWizard::SubmissionSerializer do before do CustomWizard::Template.save(template_json, skip_jobs: true) + wizard = CustomWizard::Wizard.create(template_json["id"], user1) - CustomWizard::Submission.new(wizard, - step_1_field_1: "I am user submission", - submitted_at: Time.now.iso8601 - ).save + CustomWizard::Submission.new(wizard, step_1_field_1: "I am user1 submission", submitted_at: Time.now.iso8601).save + + wizard = CustomWizard::Wizard.create(template_json["id"], user2) + CustomWizard::Submission.new(wizard, step_1_field_1: "I am user2 submission", submitted_at: Time.now.iso8601).save end it 'should return submission attributes' do @@ -30,12 +31,11 @@ describe CustomWizard::SubmissionSerializer do each_serializer: described_class ).as_json - expect(json_array.length).to eq(1) + expect(json_array.length).to eq(2) expect(json_array[0][:id].present?).to eq(true) - expect(json_array[0][:user]).to eq( - BasicUserSerializer.new(user1, root: false).as_json - ) expect(json_array[0][:submitted_at].present?).to eq(true) + expect(json_array[0][:user]).to eq(BasicUserSerializer.new(user2, root: false).as_json) + expect(json_array[1][:user]).to eq(BasicUserSerializer.new(user1, root: false).as_json) end it "should return field values, types and labels" do @@ -47,10 +47,10 @@ describe CustomWizard::SubmissionSerializer do each_serializer: described_class ).as_json - expect(json_array.length).to eq(1) + expect(json_array.length).to eq(2) expect(json_array[0][:fields].as_json).to eq({ "step_1_field_1": { - "value": "I am user submission", + "value": "I am user2 submission", "type": "text", "label": "Text" } From 33abececd4dc211158296ab28013b325eb4a83d7 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 16 Mar 2023 17:44:24 +0100 Subject: [PATCH 3/4] Bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 2b8d1bd5..0bb34914 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.2.10 +# version: 2.2.11 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From c6dc80f02ca1fb61cde842dcad09b1b667c3004d Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 17 Mar 2023 09:05:53 +0100 Subject: [PATCH 4/4] FIX: template guest validation not working with validation conditions --- lib/custom_wizard/validators/template.rb | 2 +- plugin.rb | 2 +- .../custom_wizard/template_validator_spec.rb | 8 ++++++++ .../condition/validation_condition.json | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/condition/validation_condition.json diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index 60652322..f2a4feb0 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -84,7 +84,7 @@ class CustomWizard::TemplateValidator def validate_guests(object, type) guests_permitted = @data[:permitted] && @data[:permitted].any? do |m| - m["output"].include?(CustomWizard::Wizard::GUEST_GROUP_ID) + m["output"]&.include?(CustomWizard::Wizard::GUEST_GROUP_ID) end return unless guests_permitted diff --git a/plugin.rb b/plugin.rb index 0bb34914..5b5c7980 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.2.11 +# version: 2.2.12 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index b9b257e3..fe61be91 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -9,6 +9,7 @@ describe CustomWizard::TemplateValidator do let(:composer_preview) { get_wizard_fixture("field/composer_preview") } let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") } let(:upload_field) { get_wizard_fixture("field/upload") } + let(:validation_condition) { get_wizard_fixture("condition/validation_condition") } let(:valid_liquid_template) { <<-LIQUID.strip @@ -182,6 +183,13 @@ describe CustomWizard::TemplateValidator do CustomWizard::TemplateValidator.new(template).perform ).to eq(true) end + + it "validates settings with validation conditions" do + template[:permitted] = validation_condition["condition"] + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(true) + end end context "steps" do diff --git a/spec/fixtures/condition/validation_condition.json b/spec/fixtures/condition/validation_condition.json new file mode 100644 index 00000000..695c25c9 --- /dev/null +++ b/spec/fixtures/condition/validation_condition.json @@ -0,0 +1,17 @@ +{ + "condition": [ + { + "type": "validation", + "pairs": [ + { + "index": 0, + "key": "trust_level", + "key_type": "user_field", + "value": "2", + "value_type": "text", + "connector": "greater_or_equal" + } + ] + } + ] +} \ No newline at end of file