2021-03-11 17:30:15 +11:00
|
|
|
# frozen_string_literal: true
|
2020-11-26 14:05:50 +11:00
|
|
|
class CustomWizard::TemplateValidator
|
2020-11-03 11:24:20 +11:00
|
|
|
include HasErrors
|
2020-11-09 14:32:36 +11:00
|
|
|
include ActiveModel::Model
|
2021-03-11 17:30:15 +11:00
|
|
|
|
|
|
|
def initialize(data, opts = {})
|
2020-11-03 11:24:20 +11:00
|
|
|
@data = data
|
2020-04-13 22:17:22 +10:00
|
|
|
@opts = opts
|
2021-09-24 17:58:42 +08:00
|
|
|
@subscription = CustomWizard::Subscription.new
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
def perform
|
2020-11-03 11:24:20 +11:00
|
|
|
data = @data
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-03 11:24:20 +11:00
|
|
|
check_id(data, :wizard)
|
|
|
|
validate_after_time
|
2022-02-15 12:33:04 +11:00
|
|
|
validate_class(data, :wizard)
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-03 11:24:20 +11:00
|
|
|
data[:steps].each do |step|
|
2022-02-15 12:33:04 +11:00
|
|
|
validate_class(step, :step)
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2021-09-07 20:06:13 +08:00
|
|
|
if step[:fields].present?
|
|
|
|
step[:fields].each do |field|
|
2022-02-15 12:33:04 +11:00
|
|
|
validate_class(field, :field)
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-03 11:24:20 +11:00
|
|
|
if data[:actions].present?
|
|
|
|
data[:actions].each do |action|
|
2022-02-15 12:33:04 +11:00
|
|
|
validate_class(action, :action)
|
2020-11-03 11:24:20 +11:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2022-02-15 12:33:04 +11:00
|
|
|
!errors.any?
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
def self.required
|
|
|
|
{
|
|
|
|
wizard: ['id', 'name', 'steps'],
|
|
|
|
step: ['id'],
|
|
|
|
field: ['id', 'type'],
|
|
|
|
action: ['id', 'type']
|
|
|
|
}
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
private
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2022-02-15 12:33:04 +11:00
|
|
|
def validate_class(object, klass)
|
|
|
|
check_required(object, klass)
|
|
|
|
validate_subscription(object, klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_required(object, klass)
|
|
|
|
self.class.required[klass].each do |attribute|
|
|
|
|
if object[attribute].blank?
|
|
|
|
errors.add :base, I18n.t("wizard.validation.required", attribute: attribute)
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-03 11:24:20 +11:00
|
|
|
|
2022-02-15 12:33:04 +11:00
|
|
|
def validate_subscription(object, klass)
|
|
|
|
object.keys.each do |attribute|
|
|
|
|
if !@subscription.can_use_feature?(klass, attribute, object[attribute])
|
|
|
|
errors.add :base, I18n.t("wizard.validation.subscription", class: klass.to_s, attribute: attribute)
|
2021-08-10 17:00:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
def check_id(object, type)
|
2020-10-31 18:05:50 +11:00
|
|
|
if type === :wizard && @opts[:create] && CustomWizard::Template.exists?(object[:id])
|
2020-11-09 14:32:36 +11:00
|
|
|
errors.add :base, I18n.t("wizard.validation.conflict", wizard_id: object[:id])
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-03 11:24:20 +11:00
|
|
|
def validate_after_time
|
|
|
|
return unless @data[:after_time]
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-03 11:24:20 +11:00
|
|
|
wizard = CustomWizard::Wizard.create(@data[:id]) if !@opts[:create]
|
2020-04-14 09:39:21 +10:00
|
|
|
current_time = wizard.present? ? wizard.after_time_scheduled : nil
|
2020-11-03 11:24:20 +11:00
|
|
|
new_time = @data[:after_time_scheduled]
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-14 15:46:06 +10:00
|
|
|
begin
|
|
|
|
active_time = Time.parse(new_time.present? ? new_time : current_time).utc
|
|
|
|
rescue ArgumentError
|
|
|
|
invalid_time = true
|
|
|
|
end
|
2020-04-13 22:17:22 +10:00
|
|
|
|
2020-04-14 15:46:06 +10:00
|
|
|
if invalid_time || active_time.blank? || active_time < Time.now.utc
|
2020-11-09 14:32:36 +11:00
|
|
|
errors.add :base, I18n.t("wizard.validation.after_time")
|
2020-04-13 22:17:22 +10:00
|
|
|
end
|
|
|
|
end
|
2021-09-14 11:33:16 +08:00
|
|
|
|
|
|
|
def cast_bool(val)
|
|
|
|
ActiveRecord::Type::Boolean.new.cast(val)
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
end
|