From 4be624af15ff105273eb5cb6a6eca22716a91ad3 Mon Sep 17 00:00:00 2001 From: sreyleak Date: Mon, 7 Oct 2024 09:50:50 +0700 Subject: [PATCH] Close #1979 create new api for organizer profile --- .../api/v2/storefront/events_controller.rb | 32 +++++++++++++++++++ .../admin/taxons/_form/vendor.html.erb.deface | 9 ++++++ .../organizer_profile_event_query.rb | 31 ++++++++++++++++++ .../spree/v2/storefront/event_serializer.rb | 9 ++++++ .../storefront/taxon_serializer_decorator.rb | 1 + .../storefront/vendor_serializer_decorator.rb | 1 + .../spree_permitted_attributes.rb | 1 + config/routes.rb | 1 + ...0240926044328_add_vendor_to_spree_taxon.rb | 5 +++ .../v2/storefront/taxon_serializer_spec.rb | 2 +- 10 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 app/controllers/spree/api/v2/storefront/events_controller.rb create mode 100644 app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface create mode 100644 app/queries/spree_cm_commissioner/organizer_profile_event_query.rb create mode 100644 app/serializers/spree/v2/storefront/event_serializer.rb create mode 100644 db/migrate/20240926044328_add_vendor_to_spree_taxon.rb diff --git a/app/controllers/spree/api/v2/storefront/events_controller.rb b/app/controllers/spree/api/v2/storefront/events_controller.rb new file mode 100644 index 000000000..af72e49b0 --- /dev/null +++ b/app/controllers/spree/api/v2/storefront/events_controller.rb @@ -0,0 +1,32 @@ +module Spree + module Api + module V2 + module Storefront + class EventsController < Spree::Api::V2::ResourceController + before_action :require_spree_current_user + before_action :set_vendor + + def collection + organizer_query = SpreeCmCommissioner::OrganizerProfileEventQuery.new( + user_id: spree_current_user.id, + vendor_id: @vendor.id, + section: params[:section] || 'upcoming' + ) + + @events = organizer_query.events.page(params[:page]).per(params[:per_page]) + end + + def collection_serializer + Spree::V2::Storefront::EventSerializer + end + + private + + def set_vendor + @vendor = Spree::Vendor.find_by(id: params[:vendor_id]) + end + end + end + end + end +end diff --git a/app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface b/app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface new file mode 100644 index 000000000..a97341727 --- /dev/null +++ b/app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface @@ -0,0 +1,9 @@ + + +
+ <%= f.field_container :vendor_id do %> + <%= label_tag :vendor_id, Spree.t(:vendors) %> + <%= f.select :vendor_id, Spree::Vendor.pluck(:name, :id), { include_blank: true }, { class: 'select2-clear js-filterable' } %> + <%= f.error_message_on :vendor_id, class: 'error-message' %> + <% end %> +
diff --git a/app/queries/spree_cm_commissioner/organizer_profile_event_query.rb b/app/queries/spree_cm_commissioner/organizer_profile_event_query.rb new file mode 100644 index 000000000..e497d1230 --- /dev/null +++ b/app/queries/spree_cm_commissioner/organizer_profile_event_query.rb @@ -0,0 +1,31 @@ +module SpreeCmCommissioner + class OrganizerProfileEventQuery + attr_reader :user_id, :vendor_id, :section, :start_from_date + + # user_id: user ID, vendor_id: vendor ID, section: 'upcoming' or 'previous' + def initialize(user_id:, vendor_id:, section:, start_from_date: nil) + @user_id = user_id + @vendor_id = vendor_id + @section = section + @start_from_date = start_from_date || Time.zone.now + end + + def events + taxons = Spree::Taxon.where(vendor_id: vendor_id) + + if section == 'upcoming' + taxons.where('to_date >= ?', start_from_date) + .order(from_date: :asc) + else + taxons.where('to_date < ?', start_from_date) + .order(to_date: :desc) + end + end + + private + + def user_can_access_events? + true + end + end +end diff --git a/app/serializers/spree/v2/storefront/event_serializer.rb b/app/serializers/spree/v2/storefront/event_serializer.rb new file mode 100644 index 000000000..5070a3f9d --- /dev/null +++ b/app/serializers/spree/v2/storefront/event_serializer.rb @@ -0,0 +1,9 @@ +module Spree + module V2 + module Storefront + class EventSerializer < BaseSerializer + attributes :id, :name, :description, :from_date, :to_date, :kind, :permalink, :vendor_id + end + end + end +end diff --git a/app/serializers/spree/v2/storefront/taxon_serializer_decorator.rb b/app/serializers/spree/v2/storefront/taxon_serializer_decorator.rb index ecbc72c45..901ad21d7 100644 --- a/app/serializers/spree/v2/storefront/taxon_serializer_decorator.rb +++ b/app/serializers/spree/v2/storefront/taxon_serializer_decorator.rb @@ -5,6 +5,7 @@ module TaxonSerializerDecorator def self.prepended(base) base.has_many :vendors, serializer: ::Spree::V2::Storefront::VendorSerializer base.has_many :visible_products, serializer: ::Spree::V2::Storefront::ProductSerializer + base.has_many :vendors, serializer: ::Spree::V2::Storefront::VendorSerializerDecorator base.has_one :category_icon, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer base.has_one :app_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer diff --git a/app/serializers/spree/v2/storefront/vendor_serializer_decorator.rb b/app/serializers/spree/v2/storefront/vendor_serializer_decorator.rb index 0540b4b53..fab192a5a 100644 --- a/app/serializers/spree/v2/storefront/vendor_serializer_decorator.rb +++ b/app/serializers/spree/v2/storefront/vendor_serializer_decorator.rb @@ -16,6 +16,7 @@ def self.prepended(base) base.has_many :vendor_kind_option_values, serializer: :option_value base.has_many :auto_apply_promotions, serializer: ::SpreeCmCommissioner::V2::Storefront::PromotionSerializer base.has_many :active_promotions, serializer: ::SpreeCmCommissioner::V2::Storefront::PromotionSerializer + base.has_many :taxon base.has_one :default_state, serializer: :state base.has_one :logo, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer diff --git a/config/initializers/spree_permitted_attributes.rb b/config/initializers/spree_permitted_attributes.rb index 31ff7e55c..7938aeec5 100644 --- a/config/initializers/spree_permitted_attributes.rb +++ b/config/initializers/spree_permitted_attributes.rb @@ -26,6 +26,7 @@ module PermittedAttributes preferred_background_color preferred_foreground_color show_event_status + vendor_id ] @@store_attributes += [ diff --git a/config/routes.rb b/config/routes.rb index 907c9c2a3..55e06fa33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -426,6 +426,7 @@ resources :vendors do resources :nearby_places, only: %i[index] + resources :events end resource :homepage_data, only: [:show] resources :homepage_sections, only: [:index] diff --git a/db/migrate/20240926044328_add_vendor_to_spree_taxon.rb b/db/migrate/20240926044328_add_vendor_to_spree_taxon.rb new file mode 100644 index 000000000..18ebbe11b --- /dev/null +++ b/db/migrate/20240926044328_add_vendor_to_spree_taxon.rb @@ -0,0 +1,5 @@ +class AddVendorToSpreeTaxon < ActiveRecord::Migration[7.0] + def change + add_reference :spree_taxons, :vendor, index: true, foreign_key: { to_table: :spree_vendors }, if_not_exists: true + end +end diff --git a/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb b/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb index eef219535..f546519c3 100644 --- a/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb +++ b/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb @@ -47,7 +47,7 @@ :to_date, :background_color, :foreground_color, - :show_event_status + :show_event_status, ) end