Skip to content

Commit

Permalink
Close #1979 create new api for organizer profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreyleak-Deth committed Oct 9, 2024
1 parent 4b53968 commit 8157ab7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 2 deletions.
31 changes: 31 additions & 0 deletions app/controllers/spree/api/v2/storefront/events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Spree
module Api
module V2
module Storefront
class EventsController < Spree::Api::V2::ResourceController
before_action :require_spree_current_user

def collection
organizer_query = SpreeCmCommissioner::OrganizerProfileEventQuery.new(
user_id: spree_current_user.id,
vendor_id: vendor.id,
section: params[:section] || 'upcoming'
)

organizer_query.events.page(params[:page]).per(params[:per_page])
end

def collection_serializer
Spree::V2::Storefront::TaxonSerializer
end

private

def vendor
@vendor ||= Spree::Vendor.find_by(id: params[:vendor_id])
end
end
end
end
end
end
9 changes: 9 additions & 0 deletions app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- insert_before "erb[loud]:contains('field_container :permalink')" -->

<div>
<%= 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 %>
</div>
25 changes: 25 additions & 0 deletions app/queries/spree_cm_commissioner/organizer_profile_event_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def self.prepended(base)
base.has_one :web_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer
base.has_one :home_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer

base.attributes :custom_redirect_url, :kind, :subtitle, :from_date, :to_date, :background_color, :foreground_color, :show_badge_status
base.attributes :custom_redirect_url, :kind, :subtitle, :from_date, :to_date,
:background_color, :foreground_color, :show_badge_status, :vendor_id
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/initializers/spree_permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module PermittedAttributes
preferred_background_color
preferred_foreground_color
show_badge_status
vendor_id
]

@@store_attributes += [
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
resources :vendors do
resources :nearby_places, only: %i[index]
resources :vendor_photos
resources :events
end
resource :homepage_data, only: [:show]
resources :homepage_sections, only: [:index]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240926044328_add_vendor_to_spree_taxon.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
:to_date,
:background_color,
:foreground_color,
:show_badge_status
:show_badge_status,
:vendor_id
)
end

Expand Down

0 comments on commit 8157ab7

Please sign in to comment.