From a2f4df191a7e650a1f0838b50e1a5ab5f136ff4a Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Tue, 6 Aug 2024 15:30:21 +0100 Subject: [PATCH 1/2] Improve effiency of OrderCycle.earliest_closing_times --- app/models/order_cycle.rb | 5 ++--- app/serializers/api/uncached_enterprise_serializer.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 060f75e4f0..3fa6448944 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -154,9 +154,8 @@ class OrderCycle < ApplicationRecord 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 diff --git a/app/serializers/api/uncached_enterprise_serializer.rb b/app/serializers/api/uncached_enterprise_serializer.rb index d92db46d28..c1d620facd 100644 --- a/app/serializers/api/uncached_enterprise_serializer.rb +++ b/app/serializers/api/uncached_enterprise_serializer.rb @@ -7,7 +7,7 @@ module Api 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 From 5ca7f40a4e67d6d68e9d1a1fc41b5780ea20c045 Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Tue, 6 Aug 2024 16:09:08 +0100 Subject: [PATCH 2/2] Add unit test --- .../uncached_enterprise_serializer_spec.rb | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 spec/serializers/api/uncached_enterprise_serializer_spec.rb diff --git a/spec/serializers/api/uncached_enterprise_serializer_spec.rb b/spec/serializers/api/uncached_enterprise_serializer_spec.rb new file mode 100644 index 0000000000..b86ed91955 --- /dev/null +++ b/spec/serializers/api/uncached_enterprise_serializer_spec.rb @@ -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