Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +01:00
Add subscription level logic to back-end validation
Dieser Commit ist enthalten in:
Ursprung
14a337e00c
Commit
c6b8e08e38
12 geänderte Dateien mit 66 neuen und 21 gelöschten Zeilen
|
@ -17,9 +17,8 @@ class ::CustomWizard::CustomField
|
||||||
category: ["basic_category"],
|
category: ["basic_category"],
|
||||||
post: ["post"]
|
post: ["post"]
|
||||||
}
|
}
|
||||||
SUBSCRIPTION_CLASSES ||= ['category', 'group']
|
|
||||||
TYPES ||= ["string", "boolean", "integer", "json"]
|
TYPES ||= ["string", "boolean", "integer", "json"]
|
||||||
SUBSCRIPTION_TYPES ||= ["json"]
|
|
||||||
LIST_CACHE_KEY ||= 'custom_field_list'
|
LIST_CACHE_KEY ||= 'custom_field_list'
|
||||||
|
|
||||||
def self.serializers
|
def self.serializers
|
||||||
|
@ -84,8 +83,7 @@ class ::CustomWizard::CustomField
|
||||||
add_error(I18n.t("#{i18n_key}.unsupported_class", class: value))
|
add_error(I18n.t("#{i18n_key}.unsupported_class", class: value))
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
if attr == 'klass' && @subscription.requires_additional_subscription("custom_fields", "klass").include?(value)
|
||||||
if attr == 'klass' && SUBSCRIPTION_CLASSES.include?(value) && !@subscription.subscribed?
|
|
||||||
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
|
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -100,7 +98,7 @@ class ::CustomWizard::CustomField
|
||||||
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
|
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
|
||||||
end
|
end
|
||||||
|
|
||||||
if attr == 'type' && SUBSCRIPTION_TYPES.include?(value) && !@subscription.subscribed?
|
if attr == 'type' && @subscription.requires_additional_subscription("custom_fields", "type").include?(value)
|
||||||
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
|
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,23 @@ class CustomWizard::Subscription
|
||||||
attr_accessor :authentication,
|
attr_accessor :authentication,
|
||||||
:subscription
|
:subscription
|
||||||
|
|
||||||
|
SUBSCRIPTION_LEVELS = {
|
||||||
|
standard: {
|
||||||
|
actions: ["send_message", "add_to_group", "watch_categories"],
|
||||||
|
custom_fields: {
|
||||||
|
klass: [],
|
||||||
|
type: ["json"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
business: {
|
||||||
|
actions: ["create_category", "create_group", "send_to_api"],
|
||||||
|
custom_fields: {
|
||||||
|
klass: ["group", "category"],
|
||||||
|
type: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
|
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
|
||||||
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
|
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
|
||||||
|
@ -39,6 +56,31 @@ class CustomWizard::Subscription
|
||||||
"discourse-subscription-server:user_subscription"
|
"discourse-subscription-server:user_subscription"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def requires_additional_subscription(kategory, sub_kategory)
|
||||||
|
case kategory
|
||||||
|
when "actions"
|
||||||
|
case self.type
|
||||||
|
when "business"
|
||||||
|
return []
|
||||||
|
when "standard"
|
||||||
|
return SUBSCRIPTION_LEVELS[:business][kategory.to_sym]
|
||||||
|
else
|
||||||
|
return SUBSCRIPTION_LEVELS[:standard][kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym]
|
||||||
|
end
|
||||||
|
when "custom_fields"
|
||||||
|
case self.type
|
||||||
|
when "business"
|
||||||
|
return []
|
||||||
|
when "standard"
|
||||||
|
return SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym];
|
||||||
|
else
|
||||||
|
return SUBSCRIPTION_LEVELS[:standard][kategory.to_sym][sub_kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return []
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
if @authentication.active?
|
if @authentication.active?
|
||||||
response = Excon.get(
|
response = Excon.get(
|
||||||
|
@ -127,6 +169,10 @@ class CustomWizard::Subscription
|
||||||
self.new.type
|
self.new.type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.requires_additional_subscription(kategory, sub_kategory)
|
||||||
|
self.new.requires_additional_subscription(kategory, sub_kategory)
|
||||||
|
end
|
||||||
|
|
||||||
def self.authorized?
|
def self.authorized?
|
||||||
self.new.authorized?
|
self.new.authorized?
|
||||||
end
|
end
|
||||||
|
|
|
@ -179,7 +179,7 @@ describe CustomWizard::Action do
|
||||||
|
|
||||||
context "subscription actions" do
|
context "subscription actions" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#send_message' do
|
it '#send_message' do
|
||||||
|
|
|
@ -176,7 +176,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context "restart is enabled" do
|
context "restart is enabled" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:restart_on_revisit] = true
|
@template[:restart_on_revisit] = true
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
end
|
end
|
||||||
|
@ -205,7 +205,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context 'with required data' do
|
context 'with required data' do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:steps][0][:required_data] = required_data_json['required_data']
|
@template[:steps][0][:required_data] = required_data_json['required_data']
|
||||||
@template[:steps][0][:required_data_message] = required_data_json['required_data_message']
|
@template[:steps][0][:required_data_message] = required_data_json['required_data_message']
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
|
@ -241,7 +241,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context "with permitted params" do
|
context "with permitted params" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:steps][0][:permitted_params] = permitted_param_json['permitted_params']
|
@template[:steps][0][:permitted_params] = permitted_param_json['permitted_params']
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
end
|
end
|
||||||
|
@ -256,7 +256,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context "with condition" do
|
context "with condition" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:steps][0][:condition] = user_condition_json['condition']
|
@template[:steps][0][:condition] = user_condition_json['condition']
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
end
|
end
|
||||||
|
@ -295,7 +295,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context "with condition" do
|
context "with condition" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:steps][0][:fields][0][:condition] = user_condition_json['condition']
|
@template[:steps][0][:fields][0][:condition] = user_condition_json['condition']
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
end
|
end
|
||||||
|
@ -327,7 +327,7 @@ describe CustomWizard::Builder do
|
||||||
|
|
||||||
context 'save submissions disabled' do
|
context 'save submissions disabled' do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
@template[:save_submissions] = false
|
@template[:save_submissions] = false
|
||||||
CustomWizard::Template.save(@template.as_json)
|
CustomWizard::Template.save(@template.as_json)
|
||||||
@wizard = CustomWizard::Builder.new(@template[:id], user).build
|
@wizard = CustomWizard::Builder.new(@template[:id], user).build
|
||||||
|
|
|
@ -217,7 +217,7 @@ describe CustomWizard::CustomField do
|
||||||
|
|
||||||
context "with a subscription" do
|
context "with a subscription" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("business")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "saves subscription field types" do
|
it "saves subscription field types" do
|
||||||
|
|
|
@ -359,7 +359,7 @@ describe CustomWizard::Mapper do
|
||||||
|
|
||||||
context "with a subscription" do
|
context "with a subscription" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "treats replaced values as string literals" do
|
it "treats replaced values as string literals" do
|
||||||
|
|
|
@ -75,7 +75,7 @@ describe CustomWizard::TemplateValidator do
|
||||||
|
|
||||||
context "with subscription" do
|
context "with subscription" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "validates wizard attributes" do
|
it "validates wizard attributes" do
|
||||||
|
|
|
@ -74,7 +74,7 @@ describe "custom field extensions" do
|
||||||
|
|
||||||
context "subscription custom fields" do
|
context "subscription custom fields" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("business")
|
||||||
|
|
||||||
subscription_custom_field_json['custom_fields'].each do |field_json|
|
subscription_custom_field_json['custom_fields'].each do |field_json|
|
||||||
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
||||||
|
|
|
@ -29,8 +29,10 @@ def authenticate_subscription
|
||||||
CustomWizard::Subscription::Authentication.any_instance.stubs(:active?).returns(true)
|
CustomWizard::Subscription::Authentication.any_instance.stubs(:active?).returns(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def enable_subscription
|
def enable_subscription(type)
|
||||||
|
# CustomWizard::Subscription.new
|
||||||
CustomWizard::Subscription.any_instance.stubs(:subscribed?).returns(true)
|
CustomWizard::Subscription.any_instance.stubs(:subscribed?).returns(true)
|
||||||
|
CustomWizard::Subscription.any_instance.stubs(:type).returns(type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable_subscription
|
def disable_subscription
|
||||||
|
|
|
@ -40,8 +40,7 @@ describe "custom field extensions" do
|
||||||
|
|
||||||
context "with a subscription" do
|
context "with a subscription" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("business")
|
||||||
|
|
||||||
subscription_custom_field_json['custom_fields'].each do |field_json|
|
subscription_custom_field_json['custom_fields'].each do |field_json|
|
||||||
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
||||||
custom_field.save
|
custom_field.save
|
||||||
|
|
|
@ -121,7 +121,7 @@ describe CustomWizard::StepsController do
|
||||||
|
|
||||||
context "subscription" do
|
context "subscription" do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an error when user cant see the step due to conditions" do
|
it "raises an error when user cant see the step due to conditions" do
|
||||||
|
|
|
@ -34,7 +34,7 @@ describe CustomWizard::StepSerializer do
|
||||||
|
|
||||||
context 'with required data' do
|
context 'with required data' do
|
||||||
before do
|
before do
|
||||||
enable_subscription
|
enable_subscription("standard")
|
||||||
wizard_template['steps'][0]['required_data'] = required_data_json['required_data']
|
wizard_template['steps'][0]['required_data'] = required_data_json['required_data']
|
||||||
wizard_template['steps'][0]['required_data_message'] = required_data_json['required_data_message']
|
wizard_template['steps'][0]['required_data_message'] = required_data_json['required_data_message']
|
||||||
CustomWizard::Template.save(wizard_template)
|
CustomWizard::Template.save(wizard_template)
|
||||||
|
|
Laden …
In neuem Issue referenzieren