0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-24 10:20:28 +01:00
discourse-custom-wizard/app/controllers/custom_wizard/steps.rb

111 Zeilen
3,4 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2023-01-18 19:53:36 +01:00
class CustomWizard::StepsController < ::CustomWizard::WizardClientController
2020-10-31 08:05:50 +01:00
before_action :ensure_can_update
2024-10-16 14:18:23 +02:00
requires_plugin "discourse-custom-wizard"
2017-09-25 16:47:40 +02:00
def update
2020-11-03 01:24:20 +01:00
update = update_params.to_h
2021-03-11 07:30:15 +01:00
2020-11-06 06:39:26 +01:00
update[:fields] = {}
if params[:fields]
field_ids = @builder.wizard.field_ids
2024-10-16 13:52:03 +02:00
params[:fields].each { |k, v| update[:fields][k] = v if field_ids.include? k }
end
2021-03-11 07:30:15 +01:00
@builder.build
updater = @builder.wizard.create_updater(update[:step_id], update[:fields])
2017-09-25 16:47:40 +02:00
updater.update
@result = updater.result
2021-03-11 07:30:15 +01:00
2017-09-25 16:47:40 +02:00
if updater.success?
wizard_id = update_params[:wizard_id]
2023-01-18 19:53:36 +01:00
builder = CustomWizard::Builder.new(wizard_id, current_user, guest_id)
@wizard = builder.build(force: true)
current_step = @wizard.find_step(update[:step_id])
current_submission = @wizard.current_submission
result = {}
if current_step.conditional_final_step && !current_step.last_step
current_step.force_final = true
end
if current_step.final?
builder.template.actions.each do |action_template|
2024-10-16 13:52:03 +02:00
if action_template["run_after"] === "wizard_completion"
action_result =
CustomWizard::Action.new(
action: action_template,
wizard: @wizard,
submission: current_submission,
).perform
current_submission = action_result.submission if action_result.success?
end
end
current_submission.save
if redirect = get_redirect
updater.result[:redirect_on_complete] = redirect
end
@wizard.cleanup_on_complete!
result[:final] = true
else
current_submission.save
result[:final] = false
result[:next_step_id] = current_step.next.id
end
result.merge!(updater.result) if updater.result.present?
2017-09-25 16:47:40 +02:00
result[:refresh_required] = true if updater.refresh_required?
result[:wizard] = ::CustomWizard::WizardSerializer.new(
@wizard,
scope: Guardian.new(current_user),
2024-10-16 13:52:03 +02:00
root: false,
).as_json
2021-03-11 07:30:15 +01:00
2017-09-25 16:47:40 +02:00
render json: result
else
errors = []
updater.errors.messages.each do |field, msg|
2024-10-16 13:52:03 +02:00
errors << { field: field, description: msg.join(",") }
2017-09-25 16:47:40 +02:00
end
render json: { errors: errors }, status: 422
end
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
private
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
def ensure_can_update
raise Discourse::InvalidParameters.new(:wizard_id) if @builder.template.nil?
raise Discourse::InvalidAccess.new if !@builder.wizard || !@builder.wizard.can_access?
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
@step_template = @builder.template.steps.select { |s| s["id"] == update_params[:step_id] }.first
raise Discourse::InvalidParameters.new(:step_id) if !@step_template
raise Discourse::InvalidAccess.new if !@builder.check_condition(@step_template)
end
2021-03-11 07:30:15 +01:00
def update_params
2024-10-16 13:52:03 +02:00
@update_params ||
begin
params.require(:step_id)
params.require(:wizard_id)
params.permit(:wizard_id, :step_id).transform_values { |v| v.underscore }
end
2020-10-31 08:05:50 +01:00
end
2021-03-11 07:30:15 +01:00
def get_redirect
return @result[:redirect_on_next] if @result[:redirect_on_next].present?
submission = @wizard.current_submission
2024-10-16 13:52:03 +02:00
return nil if submission.blank?
## route_to set by actions, redirect_on_complete set by actions, redirect_to set at wizard entry
submission.route_to || submission.redirect_on_complete || submission.redirect_to
2020-10-31 08:05:50 +01:00
end
2017-09-25 16:47:40 +02:00
end