diff --git a/app/controllers/spree/admin/google_wallets_controller.rb b/app/controllers/spree/admin/google_wallets_controller.rb index c050acfee..1c1eab577 100644 --- a/app/controllers/spree/admin/google_wallets_controller.rb +++ b/app/controllers/spree/admin/google_wallets_controller.rb @@ -1,35 +1,18 @@ module Spree module Admin class GoogleWalletsController < Spree::Admin::ResourceController - before_action :product + skip_before_action :load_resource + before_action :product, :object + update.before :clear_verify_status - # POST /google_wallets - def create - wallet = model_class.new(product_id: product.id, review_status: permitted_resource_params[:review_status]) - if wallet.save - redirect_to edit_admin_product_google_wallet_path(product, wallet.id) - else - render :new - end - end + helper 'spree/admin/google_wallets' - # POST /google_wallets/:id/create_google_wallet_class - def create_google_wallet_class - service = object.class_creator.call - if service[:status] == '200' - redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_created') + # POST /google_wallets/:id/verify_with_google + def verify_with_google + if object.preferred_response.blank? + create_google_wallet_class else - redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_create_fail') - end - end - - # PATCH /google_wallets/:id/update_google_wallet_class - def update_google_wallet_class - service = object.class_updater.call - if service[:status] == '200' - redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_updated') - else - redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_update_fail') + update_google_wallet_class end end @@ -50,7 +33,15 @@ def product end def object - @object ||= product.google_wallet + @object ||= if new_actions.include?(action) + model_class.new(product_id: product.id) + elsif params[:id] + product.google_wallet + end + end + + def clear_verify_status + permitted_resource_params[:preferred_verified_at] = nil end def object_name @@ -61,6 +52,26 @@ def location_after_save edit_admin_product_google_wallet_path(product, object.id) end + def create_google_wallet_class + service = object.class_creator.call + if service[:status] == '200' + object.verify_create!(service[:status]) + redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_created') + else + redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_create_fail') + end + end + + def update_google_wallet_class + service = object.class_updater.call + if service[:status] == '200' + object.verify! + redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_updated') + else + redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_update_fail') + end + end + # @overrided def model_class SpreeCmCommissioner::EventTicketGoogleWallet diff --git a/app/helpers/spree/admin/google_wallets_helper.rb b/app/helpers/spree/admin/google_wallets_helper.rb new file mode 100644 index 000000000..ecb05abfa --- /dev/null +++ b/app/helpers/spree/admin/google_wallets_helper.rb @@ -0,0 +1,73 @@ +module Spree + module Admin + module GoogleWalletsHelper + # override + def preference_fields(object, form) + return unless object.respond_to?(:preferences) + + fields = object.preferences.keys.map do |key| + next unless object.has_preference?(key) + next if %i[response verified_at].include?(key) + + render_preference_field(object, form, key) + end + safe_join(fields) + end + + private + + def render_preference_field(object, form, key) + case key + when :background_color + render_background_color_field(object, form, key) + when :start_date, :end_date + render_date_field(object, form, key) + when object.preference_type(key).to_sym == :boolean + render_boolean_field(object, form, key) + else + render_default_field(object, form, key) + end + end + + def render_background_color_field(object, form, key) + content_tag(:div, class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-')) do + form.label("preferred_#{key}", Spree.t(key), class: 'mb-2') + + content_tag(:div, class: 'd-flex') do + preference_field_for(form, "preferred_#{key}", type: object.preference_type(key), class: 'form-control mr-2') + + content_tag(:div, '', class: 'color-preview', + style: "background-color: #{object.preferred_background_color}; + height: 40px; width: 50px; margin-left: 10px; + border-radius: 5px; border: 1px solid #ccc;" + ) + end + end + end + + def render_boolean_field(object, form, key) + content_tag(:div, preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)) + + form.label("preferred_#{key}", Spree.t(key), class: 'form-check-label'), + class: 'form-group form-check', id: [object.class.to_s.parameterize, 'preference', key].join('-') + ) + end + + def render_date_field(object, form, key) + content_tag(:div, class: 'form-group', id: "#{object.class.to_s.parameterize}-preference-#{key}") do + form.label("preferred_#{key}", Spree.t(key), class: 'mb-2') + + content_tag(:div, class: 'input-group datePickerTo', data: { + wrap: 'true', 'alt-input': 'true', 'min-date': object.preferred_start_date, 'enable-time': 'true' + } + ) do + form.text_field("preferred_#{key}", value: object.send("preferred_#{key}"), class: 'form-control shadow-none', 'data-input' => '') + end + end + end + + def render_default_field(object, form, key) + content_tag(:div, form.label("preferred_#{key}", Spree.t(key)) + + preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)), + class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-') + ) + end + end + end +end diff --git a/app/models/spree_cm_commissioner/event_ticket_google_wallet.rb b/app/models/spree_cm_commissioner/event_ticket_google_wallet.rb index 3c1e65e32..f9397151c 100644 --- a/app/models/spree_cm_commissioner/event_ticket_google_wallet.rb +++ b/app/models/spree_cm_commissioner/event_ticket_google_wallet.rb @@ -7,6 +7,8 @@ class EventTicketGoogleWallet < GoogleWallet preference :start_date, :string preference :end_date, :string preference :background_color, :string + preference :response, :string + preference :verified_at, :string validates :preferred_issuer_name, presence: true, on: :update validates :preferred_event_name, presence: true, on: :update @@ -22,11 +24,32 @@ class EventTicketGoogleWallet < GoogleWallet def set_default_preferences self.preferred_issuer_name ||= product.vendor.name self.preferred_event_name ||= product.name - self.preferred_venue_name ||= product.vendor.stock_location.city - self.preferred_venue_address ||= product.vendor.stock_location.address1 + self.preferred_venue_name ||= product.venue.place.name + self.preferred_venue_address ||= product.venue.place.vicinity self.preferred_start_date = event_start_date unless event_start_date.nil? self.preferred_end_date = event_end_date unless event_end_date.nil? self.preferred_background_color = '#000000' + self.preferred_response = nil + self.preferred_verified_at = nil + end + + def verified? + preferred_verified_at.present? && preferred_response.present? + end + + def verify_create!(response) + self.preferred_verified_at = DateTime.current + self.preferred_response = response if response.present? + save! + end + + def verify! + self.preferred_verified_at = DateTime.current + save! + end + + def verified_at + DateTime.parse(preferred_verified_at).strftime('%B %d, %Y %I:%M %p') if preferred_verified_at.present? end def event_start_date diff --git a/app/models/spree_cm_commissioner/google_wallet.rb b/app/models/spree_cm_commissioner/google_wallet.rb index e17e32cb9..3840e8f7f 100644 --- a/app/models/spree_cm_commissioner/google_wallet.rb +++ b/app/models/spree_cm_commissioner/google_wallet.rb @@ -16,8 +16,6 @@ class GoogleWallet < SpreeCmCommissioner::Base attachable.variant :large, resize_to_limit: [960, 480] end - validates :hero_image, presence: true, on: :update - def class_creator raise NotImplementedError, 'class_creator must be implemented in subclasses' end diff --git a/app/services/spree_cm_commissioner/google_wallets/base_event_ticket_class.rb b/app/services/spree_cm_commissioner/google_wallets/base_event_ticket_class.rb index 0a847a080..1556162f4 100644 --- a/app/services/spree_cm_commissioner/google_wallets/base_event_ticket_class.rb +++ b/app/services/spree_cm_commissioner/google_wallets/base_event_ticket_class.rb @@ -52,7 +52,7 @@ def logo end def hero_image - rails_blob_url(@google_wallet_class.hero_image) + (product_image_url.presence || rails_blob_url(@google_wallet_class.hero_image)) end def start_date @@ -72,6 +72,10 @@ def vendor_logo_url @google_wallet_class.product.vendor.logo&.original_url end + def product_image_url + @google_wallet_class.product.images.first&.original_url + end + def build_request_body { id: class_id, diff --git a/app/views/spree/admin/google_wallets/_form.html.erb b/app/views/spree/admin/google_wallets/_form.html.erb index 55be6b7c3..faf110dd3 100644 --- a/app/views/spree/admin/google_wallets/_form.html.erb +++ b/app/views/spree/admin/google_wallets/_form.html.erb @@ -32,7 +32,7 @@ <%= render 'shared/asset_field', field: :logo, label: 'Logo', - asset: @object.logo, + asset: @object.logo.blank? ? @product.vendor.logo : @object.logo , remove_url: remove_logo_admin_product_google_wallet_url(@product.slug, @object), form: f, classes: ['col-md-5 ml-3'] @@ -41,12 +41,11 @@ <%= render 'shared/asset_field', field: :hero_image, label: 'Hero Image', - asset: @object.hero_image, - remove_url: remove_hero_image_admin_product_google_wallet_url( @product.slug, @object), + asset: @object.hero_image.blank? ? @product.images.first : @object.hero_image, + remove_url: remove_hero_image_admin_product_google_wallet_url(@product.slug, @object), form:f, classes: ['col-md-5 ml-3'] - - %> + %> <% if @object.respond_to?(:preferences) %>
<%= Spree.t(:provider_settings_warning) %>
diff --git a/app/views/spree/admin/google_wallets/edit.html.erb b/app/views/spree/admin/google_wallets/edit.html.erb index 842c22601..67264c188 100644 --- a/app/views/spree/admin/google_wallets/edit.html.erb +++ b/app/views/spree/admin/google_wallets/edit.html.erb @@ -1,22 +1,36 @@ <%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :google_wallet } %> -
- <%= button_to Spree.t(:create), create_google_wallet_class_admin_product_google_wallet_path(@product, @object.id), method: :post, class: 'btn bg-success text-white my-1' %> - <%= button_to Spree.t(:update), update_google_wallet_class_admin_product_google_wallet_path(@product, @object), method: :patch, class: 'btn bg-success text-white my-1' %> - - <%= raw I18n.t('google_wallet.note') %> - -
- -
- <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+
+ <%= Spree.t(:verified) %> + <%= active_badge(@object.verified?) %> +
-
- <%= form_with model: @object, url: { action: 'update' } do |f| %> - <%= render partial: 'form', locals: { f: f } %> -
- <%= render partial: 'spree/admin/shared/edit_resource_links' %> + <% if @object.verified_at.present? %> +
+

<%= Spree.t(:verified_at) %>: <%= @object.verified_at %>

+ <% end %> + + <%= button_to Spree.t('verify_with_google_wallet'), + verify_with_google_admin_product_google_wallet_path(@product, @object), + method: :post, + class: 'btn btn-outline-primary my-1', + disabled: @object.verified? %> - <% end %> + + <%= raw I18n.t('google_wallet.note') %> + +
+ + +
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %> +
+ <%= form_with model: @object, url: { action: 'update' } do |f| %> + <%= render partial: 'form', locals: { f: f } %> +
+ <%= render partial: 'spree/admin/shared/edit_resource_links' %> +
+ <% end %>
diff --git a/config/routes.rb b/config/routes.rb index 4729818fe..589a9b3d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -111,8 +111,7 @@ resources :google_wallets do member do - post :create_google_wallet_class - patch :update_google_wallet_class + post :verify_with_google delete :remove_logo delete :remove_hero_image end