-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #1979: Create API for upcoming and previous events on the organ…
…izer profile screen (#1980)
- Loading branch information
1 parent
15cfed1
commit 366b66d
Showing
10 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/controllers/spree/api/v2/storefront/events_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Storefront | ||
class EventsController < Spree::Api::V2::ResourceController | ||
def collection | ||
events_query.events | ||
.includes(:vendor) | ||
.page(params.fetch(:page, 1)) | ||
.per(params.fetch(:per_page, 20)) | ||
end | ||
|
||
def collection_serializer | ||
Spree::V2::Storefront::TaxonSerializer | ||
end | ||
|
||
private | ||
|
||
def events_query | ||
SpreeCmCommissioner::OrganizerProfileEventQuery.new( | ||
vendor_id: params[:vendor_id], | ||
section: params.fetch(:section, 'upcoming'), | ||
start_from_date: params[:start_from_date] || nil | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/overrides/spree/admin/taxons/_form/vendor.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- insert_before "erb[loud]:contains('field_container :permalink')" --> | ||
|
||
<% if @taxon.depth == 1 %> | ||
<div> | ||
<%= f.field_container :vendor_id do %> | ||
<%= label_tag :vendor_id, Spree.t(:vendors) %> | ||
<%= f.select :vendor_id, Spree::Vendor.order(:name).pluck(:name, :id), | ||
{ include_blank: true }, { class: 'select2-clear js-filterable' } %> | ||
<%= f.error_message_on :vendor_id, class: 'error-message' %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
|
27 changes: 27 additions & 0 deletions
27
app/queries/spree_cm_commissioner/organizer_profile_event_query.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module SpreeCmCommissioner | ||
class OrganizerProfileEventQuery | ||
attr_reader :vendor_id, :section, :start_from_date | ||
|
||
# user_id: user ID, vendor_id: vendor ID, section: 'upcoming', 'previous' or 'all' | ||
def initialize(vendor_id:, section:, start_from_date: nil) | ||
@vendor_id = vendor_id | ||
@section = section | ||
@start_from_date = start_from_date || Time.zone.now | ||
end | ||
|
||
def events | ||
taxons = Spree::Taxon.includes(:vendor).where(vendor_id: vendor_id) | ||
|
||
case section | ||
when 'upcoming' | ||
taxons.where('to_date >= ?', start_from_date) | ||
.order(from_date: :asc) | ||
when 'previous' | ||
taxons.where('to_date < ?', start_from_date) | ||
.order(to_date: :desc) | ||
else | ||
taxons.order(from_date: :asc) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
43 changes: 43 additions & 0 deletions
43
spec/queries/spree_cm_commissioner/organizer_profile_event_query_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SpreeCmCommissioner::OrganizerProfileEventQuery do | ||
let(:vendor) { create(:vendor) } | ||
let(:start_from_date) { Time.zone.now } | ||
let!(:upcoming_taxon) { create(:taxon, vendor_id: vendor.id, from_date: 1.day.from_now, to_date: 5.days.from_now) } | ||
let!(:previous_taxon) { create(:taxon, vendor_id: vendor.id, from_date: 10.days.ago, to_date: 1.day.ago) } | ||
|
||
describe '#events' do | ||
context 'when section is upcoming' do | ||
it 'returns only upcoming events for the vendor' do | ||
query = described_class.new(vendor_id: vendor.id, section: 'upcoming', start_from_date: start_from_date) | ||
expect(query.events).to contain_exactly(upcoming_taxon) | ||
end | ||
end | ||
|
||
context 'when section is previous' do | ||
it 'returns only previous events for the vendor' do | ||
query = described_class.new(vendor_id: vendor.id, section: 'previous', start_from_date: start_from_date) | ||
|
||
expect(query.events).to contain_exactly(previous_taxon) | ||
end | ||
end | ||
|
||
context 'when start_from_date is not provided' do | ||
it 'defaults to the current time' do | ||
travel_to(Time.zone.now) do | ||
query = described_class.new(vendor_id: vendor.id, section: 'upcoming') | ||
|
||
expect(query.start_from_date.to_i).to be_within(1).of(Time.zone.now.to_i) | ||
end | ||
end | ||
end | ||
|
||
context 'when section is all' do | ||
it 'returns all events for the vendor, both upcoming and previous' do | ||
query = described_class.new(vendor_id: vendor.id, section: 'all', start_from_date: start_from_date) | ||
|
||
expect(query.events).to match_array([upcoming_taxon, previous_taxon]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters