0
0
Fork 1
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:
merefield 2021-11-09 14:57:33 +00:00
Ursprung 14a337e00c
Commit c6b8e08e38
12 geänderte Dateien mit 66 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -17,9 +17,8 @@ class ::CustomWizard::CustomField
category: ["basic_category"],
post: ["post"]
}
SUBSCRIPTION_CLASSES ||= ['category', 'group']
TYPES ||= ["string", "boolean", "integer", "json"]
SUBSCRIPTION_TYPES ||= ["json"]
LIST_CACHE_KEY ||= 'custom_field_list'
def self.serializers
@ -84,8 +83,7 @@ class ::CustomWizard::CustomField
add_error(I18n.t("#{i18n_key}.unsupported_class", class: value))
next
end
if attr == 'klass' && SUBSCRIPTION_CLASSES.include?(value) && !@subscription.subscribed?
if attr == 'klass' && @subscription.requires_additional_subscription("custom_fields", "klass").include?(value)
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
end
@ -100,7 +98,7 @@ class ::CustomWizard::CustomField
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
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))
end

Datei anzeigen

@ -6,6 +6,23 @@ class CustomWizard::Subscription
attr_accessor :authentication,
: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
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
@ -39,6 +56,31 @@ class CustomWizard::Subscription
"discourse-subscription-server:user_subscription"
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
if @authentication.active?
response = Excon.get(
@ -127,6 +169,10 @@ class CustomWizard::Subscription
self.new.type
end
def self.requires_additional_subscription(kategory, sub_kategory)
self.new.requires_additional_subscription(kategory, sub_kategory)
end
def self.authorized?
self.new.authorized?
end

Datei anzeigen

@ -179,7 +179,7 @@ describe CustomWizard::Action do
context "subscription actions" do
before do
enable_subscription
enable_subscription("standard")
end
it '#send_message' do

Datei anzeigen

@ -176,7 +176,7 @@ describe CustomWizard::Builder do
context "restart is enabled" do
before do
enable_subscription
enable_subscription("standard")
@template[:restart_on_revisit] = true
CustomWizard::Template.save(@template.as_json)
end
@ -205,7 +205,7 @@ describe CustomWizard::Builder do
context 'with required data' do
before do
enable_subscription
enable_subscription("standard")
@template[:steps][0][:required_data] = required_data_json['required_data']
@template[:steps][0][:required_data_message] = required_data_json['required_data_message']
CustomWizard::Template.save(@template.as_json)
@ -241,7 +241,7 @@ describe CustomWizard::Builder do
context "with permitted params" do
before do
enable_subscription
enable_subscription("standard")
@template[:steps][0][:permitted_params] = permitted_param_json['permitted_params']
CustomWizard::Template.save(@template.as_json)
end
@ -256,7 +256,7 @@ describe CustomWizard::Builder do
context "with condition" do
before do
enable_subscription
enable_subscription("standard")
@template[:steps][0][:condition] = user_condition_json['condition']
CustomWizard::Template.save(@template.as_json)
end
@ -295,7 +295,7 @@ describe CustomWizard::Builder do
context "with condition" do
before do
enable_subscription
enable_subscription("standard")
@template[:steps][0][:fields][0][:condition] = user_condition_json['condition']
CustomWizard::Template.save(@template.as_json)
end
@ -327,7 +327,7 @@ describe CustomWizard::Builder do
context 'save submissions disabled' do
before do
enable_subscription
enable_subscription("standard")
@template[:save_submissions] = false
CustomWizard::Template.save(@template.as_json)
@wizard = CustomWizard::Builder.new(@template[:id], user).build

Datei anzeigen

@ -217,7 +217,7 @@ describe CustomWizard::CustomField do
context "with a subscription" do
before do
enable_subscription
enable_subscription("business")
end
it "saves subscription field types" do

Datei anzeigen

@ -359,7 +359,7 @@ describe CustomWizard::Mapper do
context "with a subscription" do
before do
enable_subscription
enable_subscription("standard")
end
it "treats replaced values as string literals" do

Datei anzeigen

@ -75,7 +75,7 @@ describe CustomWizard::TemplateValidator do
context "with subscription" do
before do
enable_subscription
enable_subscription("standard")
end
it "validates wizard attributes" do

Datei anzeigen

@ -74,7 +74,7 @@ describe "custom field extensions" do
context "subscription custom fields" do
before do
enable_subscription
enable_subscription("business")
subscription_custom_field_json['custom_fields'].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json)

Datei anzeigen

@ -29,8 +29,10 @@ def authenticate_subscription
CustomWizard::Subscription::Authentication.any_instance.stubs(:active?).returns(true)
end
def enable_subscription
def enable_subscription(type)
# CustomWizard::Subscription.new
CustomWizard::Subscription.any_instance.stubs(:subscribed?).returns(true)
CustomWizard::Subscription.any_instance.stubs(:type).returns(type)
end
def disable_subscription

Datei anzeigen

@ -40,8 +40,7 @@ describe "custom field extensions" do
context "with a subscription" do
before do
enable_subscription
enable_subscription("business")
subscription_custom_field_json['custom_fields'].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save

Datei anzeigen

@ -121,7 +121,7 @@ describe CustomWizard::StepsController do
context "subscription" do
before do
enable_subscription
enable_subscription("standard")
end
it "raises an error when user cant see the step due to conditions" do

Datei anzeigen

@ -34,7 +34,7 @@ describe CustomWizard::StepSerializer do
context 'with required data' 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_message'] = required_data_json['required_data_message']
CustomWizard::Template.save(wizard_template)