diff --git a/app/controllers/spree/admin/kyc_controller.rb b/app/controllers/spree/admin/kyc_controller.rb index cf1bc6248..570550eb6 100644 --- a/app/controllers/spree/admin/kyc_controller.rb +++ b/app/controllers/spree/admin/kyc_controller.rb @@ -11,8 +11,18 @@ class KycController < Spree::Admin::ResourceController # @overrided def permitted_resource_params kyc_result = calculate_kyc_value(params[:product]) + params.require(:product).permit(:allowed_upload_later, dynamic_kyc: %i[key label attype]).merge(kyc: kyc_result) + end + + def remove_field + key = params[:key] + @product.dynamic_kyc.reject! { |field| field['key'] == key } - params.require(:product).permit(:allowed_upload_later).merge(kyc: kyc_result) + if @product.save + render json: { success: true } + else + render json: { success: false, errors: @product.errors.full_messages }, status: :unprocessable_entity + end end def flash_error diff --git a/app/controllers/spree/api/v2/storefront/guests_controller.rb b/app/controllers/spree/api/v2/storefront/guests_controller.rb index 323d67fcb..e1259234e 100644 --- a/app/controllers/spree/api/v2/storefront/guests_controller.rb +++ b/app/controllers/spree/api/v2/storefront/guests_controller.rb @@ -73,7 +73,8 @@ def guest_params :address, :other_organization, :expectation, - :upload_later + :upload_later, + :dynamic_field ) end end diff --git a/app/models/spree_cm_commissioner/guest.rb b/app/models/spree_cm_commissioner/guest.rb index 1c795a8af..34bd2f212 100644 --- a/app/models/spree_cm_commissioner/guest.rb +++ b/app/models/spree_cm_commissioner/guest.rb @@ -1,12 +1,12 @@ module SpreeCmCommissioner - class Guest < SpreeCmCommissioner::Base + class Guest < SpreeCmCommissioner::Base # rubocop:disable Metrics/ClassLength include SpreeCmCommissioner::KycBitwise delegate :kyc, to: :line_item, allow_nil: true delegate :allowed_upload_later?, to: :line_item, allow_nil: true - enum gender: { :other => 0, :male => 1, :female => 2 } - enum social_contact_platform: { + enum :gender, { :other => 0, :male => 1, :female => 2 } + enum :social_contact_platform, { :other => 0, :telegram => 1, :facebook => 2, @@ -14,7 +14,7 @@ class Guest < SpreeCmCommissioner::Base :whatsapp => 4, :line => 5, :viber => 6 - }, _prefix: true + }, prefix: true scope :complete, -> { joins(:line_item).merge(Spree::LineItem.complete) } scope :complete_or_canceled, -> { joins(:line_item).merge(Spree::LineItem.complete_or_canceled) } @@ -56,7 +56,13 @@ def self.csv_importable_columns # no validation for each field as we allow user to save data to model partially. def allowed_checkout? - kyc_fields.all? { |field| allowed_checkout_for?(field) } + kyc_fields.all? { |field| allowed_checkout_for?(field) } && dynamic_fields_present? + end + + def dynamic_fields_present? + return true if dynamic_field.empty? + + dynamic_field.all? { |entry| entry['value'].present? } end def allowed_checkout_for?(field) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity @@ -77,6 +83,12 @@ def allowed_checkout_for?(field) # rubocop:disable Metrics/AbcSize, Metrics/Perc false end + def require_dynamic_kyc_field? + return false if dynamic_field.empty? + + dynamic_field.any? { |entry| entry['value'].blank? || entry['value'].empty? } + end + def require_kyc_field? # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength kyc_fields.any? do |field| case field diff --git a/app/models/spree_cm_commissioner/product_decorator.rb b/app/models/spree_cm_commissioner/product_decorator.rb index 459892c2b..63c81e3b3 100644 --- a/app/models/spree_cm_commissioner/product_decorator.rb +++ b/app/models/spree_cm_commissioner/product_decorator.rb @@ -1,6 +1,8 @@ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize module SpreeCmCommissioner module ProductDecorator + DYNAMIC_KYC_ATTRTYPES = %i[string integer float datetime date].freeze + def self.prepended(base) base.include SpreeCmCommissioner::ProductType base.include SpreeCmCommissioner::KycBitwise diff --git a/app/serializers/spree/v2/storefront/product_serializer_decorator.rb b/app/serializers/spree/v2/storefront/product_serializer_decorator.rb index d5d0b8727..525939de5 100644 --- a/app/serializers/spree/v2/storefront/product_serializer_decorator.rb +++ b/app/serializers/spree/v2/storefront/product_serializer_decorator.rb @@ -12,7 +12,7 @@ def self.prepended(base) base.has_one :venue, serializer: ::SpreeCmCommissioner::V2::Storefront::ProductPlaceSerializer base.attributes :need_confirmation, :product_type, :kyc, :kyc_fields, :allowed_upload_later, :allow_anonymous_booking, :use_video_as_default - base.attributes :reveal_description, :discontinue_on, :public_metadata, :purchasable_on + base.attributes :reveal_description, :discontinue_on, :public_metadata, :purchasable_on, :dynamic_kyc base.attribute :purchasable_on_app do |product| product.purchasable_on == 'app' || product.purchasable_on == 'both' diff --git a/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb b/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb index ac8028f35..b7a95ec7b 100644 --- a/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb +++ b/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb @@ -10,6 +10,7 @@ class GuestSerializer < BaseSerializer attribute :allowed_checkout, &:allowed_checkout? attribute :require_kyc_field, &:require_kyc_field? + attribute :require_dynamic_kyc_field, &:require_dynamic_kyc_field? belongs_to :occupation, serializer: Spree::V2::Storefront::TaxonSerializer diff --git a/app/serializers/spree_cm_commissioner/v2/storefront/guest_serializer.rb b/app/serializers/spree_cm_commissioner/v2/storefront/guest_serializer.rb index 38d1b6e5c..bae962a13 100644 --- a/app/serializers/spree_cm_commissioner/v2/storefront/guest_serializer.rb +++ b/app/serializers/spree_cm_commissioner/v2/storefront/guest_serializer.rb @@ -6,7 +6,8 @@ class GuestSerializer < BaseSerializer attributes :first_name, :last_name, :dob, :gender, :kyc_fields, :other_occupation, :created_at, :updated_at, :qr_data, :social_contact_platform, :social_contact, :available_social_contact_platforms, - :age, :emergency_contact, :other_organization, :expectation, :upload_later, :address, :formatted_bib_number, :phone_number + :age, :emergency_contact, :other_organization, :expectation, :upload_later, :address, :formatted_bib_number, :phone_number, + :dynamic_field # temporary, once app release after cm-app v1.9.1, we no longer need this. attribute :seat_number do |object| @@ -16,6 +17,7 @@ class GuestSerializer < BaseSerializer attribute :allowed_checkout, &:allowed_checkout? attribute :allowed_upload_later, &:allowed_upload_later? attribute :require_kyc_field, &:require_kyc_field? + attribute :require_dynamic_kyc_field, &:require_dynamic_kyc_field? belongs_to :occupation, serializer: Spree::V2::Storefront::TaxonSerializer belongs_to :nationality, serializer: Spree::V2::Storefront::TaxonSerializer diff --git a/app/views/spree/admin/kyc/_dynamic_kyc_fields.html.erb b/app/views/spree/admin/kyc/_dynamic_kyc_fields.html.erb new file mode 100644 index 000000000..32b9f3e7b --- /dev/null +++ b/app/views/spree/admin/kyc/_dynamic_kyc_fields.html.erb @@ -0,0 +1,12 @@ + + +