Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve effiency of OrderCycle.earliest_closing_times #12745

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/models/order_cycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ def self.earliest_closing_times
joins(:order_cycle).
merge(OrderCycle.active).
group('exchanges.receiver_id').
select("exchanges.receiver_id AS receiver_id,
MIN(order_cycles.orders_close_at) AS earliest_close_at").
map { |ex| [ex.receiver_id, ex.earliest_close_at.to_time] }
pluck(Arel.sql("exchanges.receiver_id AS receiver_id"),
Arel.sql("MIN(order_cycles.orders_close_at) AS earliest_close_at"))
]
end

Expand Down
2 changes: 1 addition & 1 deletion app/serializers/api/uncached_enterprise_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class UncachedEnterpriseSerializer < ActiveModel::Serializer
attributes :orders_close_at, :active

def orders_close_at
options[:data].earliest_closing_times[object.id]
options[:data].earliest_closing_times[object.id]&.to_time
end

def active
Expand Down
36 changes: 36 additions & 0 deletions spec/serializers/api/uncached_enterprise_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Api::UncachedEnterpriseSerializer do
let(:serializer) {
described_class.new enterprise, { data: OpenFoodNetwork::EnterpriseInjectionData.new }
}
let(:enterprise) { create :enterprise }

before do
allow_any_instance_of(OpenFoodNetwork::EnterpriseInjectionData).to(
receive(:earliest_closing_times).
and_return(data)
)
end

describe '#orders_close_at' do
context "for an enterprise with an active order cycle" do
let(:order_cycle) { create :open_order_cycle, coordinator: enterprise }
let(:data) { { enterprise.id => order_cycle.orders_close_at } }

it "returns a closing time for an enterprise" do
expect(serializer.orders_close_at).to eq order_cycle.orders_close_at
end
end

context "for an enterprise without an active order cycle" do
let(:data) { {} }

it "returns nil for an enterprise without a closing time" do
expect(serializer.orders_close_at).to be_nil
end
end
end
end
Loading