mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #8895 from Matt-Yorkley/cloned-subscription-syncing
Sync subscriptions on newly-cloned order cycles
This commit is contained in:
@@ -29,6 +29,7 @@ class OrderCycle < ApplicationRecord
|
||||
attr_accessor :incoming_exchanges, :outgoing_exchanges
|
||||
|
||||
before_update :reset_processed_at, if: :will_save_change_to_orders_close_at?
|
||||
after_save :sync_subscriptions, if: :opening?
|
||||
|
||||
validates :name, :coordinator_id, presence: true
|
||||
validate :orders_close_at_after_orders_open_at?
|
||||
@@ -159,6 +160,7 @@ class OrderCycle < ApplicationRecord
|
||||
oc.schedule_ids = schedule_ids
|
||||
oc.save!
|
||||
exchanges.each { |e| e.clone!(oc) }
|
||||
sync_subscriptions
|
||||
oc.reload
|
||||
end
|
||||
|
||||
@@ -273,6 +275,22 @@ class OrderCycle < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def opening?
|
||||
(open? || upcoming?) && saved_change_to_orders_close_at? && was_closed?
|
||||
end
|
||||
|
||||
def was_closed?
|
||||
orders_close_at_previously_was.blank? || Time.zone.now > orders_close_at_previously_was
|
||||
end
|
||||
|
||||
def sync_subscriptions
|
||||
return unless schedule_ids.any?
|
||||
|
||||
OrderManagement::Subscriptions::ProxyOrderSyncer.new(
|
||||
Subscription.where(schedule_id: schedule_ids)
|
||||
).sync!
|
||||
end
|
||||
|
||||
def orders_close_at_after_orders_open_at?
|
||||
return if orders_open_at.blank? || orders_close_at.blank?
|
||||
return if orders_close_at > orders_open_at
|
||||
|
||||
@@ -550,6 +550,42 @@ describe OrderCycle do
|
||||
end
|
||||
end
|
||||
|
||||
describe "syncing subscriptions" do
|
||||
let!(:oc) {
|
||||
create(:simple_order_cycle, orders_open_at: 1.week.ago, orders_close_at: 1.day.ago)
|
||||
}
|
||||
let(:schedule) { create(:schedule, order_cycles: [oc]) }
|
||||
let!(:subscription) { create(:subscription, schedule: schedule, with_items: true) }
|
||||
|
||||
it "syncs subscriptions when transitioning from closed to open" do
|
||||
expect(OrderManagement::Subscriptions::ProxyOrderSyncer).to receive(:new).and_call_original
|
||||
|
||||
expect{
|
||||
oc.update(orders_close_at: 1.week.from_now)
|
||||
}.to change{ ProxyOrder.count }
|
||||
end
|
||||
|
||||
it "syncs subscriptions when transitioning from closed to upcoming" do
|
||||
expect(OrderManagement::Subscriptions::ProxyOrderSyncer).to receive(:new).and_call_original
|
||||
|
||||
expect {
|
||||
oc.update(orders_open_at: 1.day.from_now, orders_close_at: 1.week.from_now)
|
||||
}.to change{ ProxyOrder.count }
|
||||
end
|
||||
|
||||
context "when the current dates are nil" do
|
||||
before { oc.update(orders_open_at: nil, orders_close_at: nil) }
|
||||
|
||||
it "syncs subscriptions when transitioning from closed to open" do
|
||||
expect(OrderManagement::Subscriptions::ProxyOrderSyncer).to receive(:new).and_call_original
|
||||
|
||||
expect{
|
||||
oc.update(orders_open_at: 1.day.ago, orders_close_at: 1.week.from_now)
|
||||
}.to change{ ProxyOrder.count }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "processed_at " do
|
||||
let!(:oc) {
|
||||
create(:simple_order_cycle, orders_open_at: 1.week.ago, orders_close_at: 1.day.ago, processed_at: 1.hour.ago)
|
||||
|
||||
Reference in New Issue
Block a user