From 8f825a709c82718c5192535ed6ea1f9eeecafc94 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 12 Jan 2022 10:34:47 +0000 Subject: [PATCH 1/4] Add mails_sent column to order cycles --- db/migrate/20220112102539_add_mails_sent_to_order_cycles.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220112102539_add_mails_sent_to_order_cycles.rb diff --git a/db/migrate/20220112102539_add_mails_sent_to_order_cycles.rb b/db/migrate/20220112102539_add_mails_sent_to_order_cycles.rb new file mode 100644 index 0000000000..c4175407b6 --- /dev/null +++ b/db/migrate/20220112102539_add_mails_sent_to_order_cycles.rb @@ -0,0 +1,5 @@ +class AddMailsSentToOrderCycles < ActiveRecord::Migration[6.1] + def change + add_column :order_cycles, :mails_sent, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 9ce9cd09fd..5a1d641eee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_12_17_094141) do +ActiveRecord::Schema.define(version: 2022_01_12_102539) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -295,6 +295,7 @@ ActiveRecord::Schema.define(version: 2021_12_17_094141) do t.datetime "updated_at", null: false t.datetime "processed_at" t.boolean "automatic_notifications", default: false + t.boolean "mails_sent", default: false end create_table "producer_properties", force: :cascade do |t| From 54e1a8807a507c0a8b2d6d2c984c071ab7ceddc5 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 12 Jan 2022 10:36:17 +0000 Subject: [PATCH 2/4] Use mails_sent flag for tracking mails --- app/jobs/order_cycle_notification_job.rb | 1 + app/models/order_cycle.rb | 4 +- app/views/admin/order_cycles/edit.html.haml | 6 +- .../jobs/order_cycle_notification_job_spec.rb | 8 ++- spec/system/admin/order_cycles/simple_spec.rb | 63 ++++++++++++------- 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/app/jobs/order_cycle_notification_job.rb b/app/jobs/order_cycle_notification_job.rb index 2e0dfb9e6c..2dfb731c93 100644 --- a/app/jobs/order_cycle_notification_job.rb +++ b/app/jobs/order_cycle_notification_job.rb @@ -7,5 +7,6 @@ class OrderCycleNotificationJob < ActiveJob::Base order_cycle.suppliers.each do |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver_now end + order_cycle.update_columns mails_sent: true end end diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 83a9b769fe..6de50ddc7f 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -281,7 +281,9 @@ class OrderCycle < ApplicationRecord def reset_processed_at return unless orders_close_at.present? && orders_close_at_was.present? + return unless orders_close_at > orders_close_at_was - self.processed_at = nil if orders_close_at > orders_close_at_was + self.processed_at = nil + self.mails_sent = false end end diff --git a/app/views/admin/order_cycles/edit.html.haml b/app/views/admin/order_cycles/edit.html.haml index 35d0704ae4..fdad0e3599 100644 --- a/app/views/admin/order_cycles/edit.html.haml +++ b/app/views/admin/order_cycles/edit.html.haml @@ -2,12 +2,12 @@ - content_for :page_actions do - if can? :notify_producers, @order_cycle %li - - processed = @order_cycle.processed_at.present? + - mails_sent = @order_cycle.mails_sent? - url = main_app.notify_producers_admin_order_cycle_path - confirm_msg = "#{t('.notify_producers_tip')} #{t(:are_you_sure)}" %a.button.icon-email.with-tip{ href: url, data: { method: 'post', confirm: confirm_msg }, 'data-powertip': t('.notify_producers_tip') } - = processed ? t('.re_notify_producers') : t(:notify_producers) - - if processed + = mails_sent ? t('.re_notify_producers') : t(:notify_producers) + - if mails_sent .badge.icon-ok.success - content_for :page_title do diff --git a/spec/jobs/order_cycle_notification_job_spec.rb b/spec/jobs/order_cycle_notification_job_spec.rb index 670658560c..233beac26b 100644 --- a/spec/jobs/order_cycle_notification_job_spec.rb +++ b/spec/jobs/order_cycle_notification_job_spec.rb @@ -10,8 +10,14 @@ describe OrderCycleNotificationJob do allow(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail) end - it 'sends a mail to each supplier' do + it "sends a mail to each supplier" do OrderCycleNotificationJob.perform_now order_cycle.id expect(ProducerMailer).to have_received(:order_cycle_report).twice end + + it "records that mails have been sent for the order cycle" do + expect do + OrderCycleNotificationJob.perform_now(order_cycle.id) + end.to change{ order_cycle.reload.mails_sent? }.from(false).to(true) + end end diff --git a/spec/system/admin/order_cycles/simple_spec.rb b/spec/system/admin/order_cycles/simple_spec.rb index 48f90a189b..c3c84f18e5 100644 --- a/spec/system/admin/order_cycles/simple_spec.rb +++ b/spec/system/admin/order_cycles/simple_spec.rb @@ -272,36 +272,51 @@ describe ' expect(exchange.tag_list).to eq(["wholesale"]) end - it "editing an order cycle" do - oc = create(:simple_order_cycle, - suppliers: [supplier_managed, supplier_permitted, supplier_unmanaged], coordinator: distributor_managed, distributors: [distributor_managed, distributor_permitted, distributor_unmanaged], name: 'Order Cycle 1' ) - distributor_managed.update_attribute(:enable_subscriptions, true) + context "editing an order cycle" do + let(:oc) do + create(:simple_order_cycle, suppliers: [supplier_managed, supplier_permitted, supplier_unmanaged], + coordinator: distributor_managed, + distributors: [distributor_managed, distributor_permitted, distributor_unmanaged], + name: 'Order Cycle 1' ) + end - visit edit_admin_order_cycle_path(oc) + before { distributor_managed.update_attribute(:enable_subscriptions, true) } - expect(page).to have_field 'order_cycle_name', with: oc.name - select2_select schedule.name, from: 'schedule_ids' - expect(page).not_to have_select2 'schedule_ids', - with_options: [schedule_of_other_managed_distributor.name] + it "shows if notifications have been sent" do + oc.update_columns mails_sent: true - click_button 'Save and Next' + visit edit_admin_order_cycle_path(oc) - # When I remove all incoming exchanges - page.find("tr.supplier-#{supplier_managed.id} a.remove-exchange").click - page.find("tr.supplier-#{supplier_permitted.id} a.remove-exchange").click - click_button 'Save and Next' + expect(page).to have_content I18n.t("admin.order_cycles.edit.re_notify_producers").upcase + end - # And I remove all outgoing exchanges - page.find("tr.distributor-#{distributor_managed.id} a.remove-exchange").click - page.find("tr.distributor-#{distributor_permitted.id} a.remove-exchange").click - click_button 'Save and Back to List' - expect(page).to have_input "oc#{oc.id}[name]", value: oc.name + it "allows removing exchanges" do + visit edit_admin_order_cycle_path(oc) - oc.reload - expect(oc.suppliers).to eq([supplier_unmanaged]) - expect(oc.coordinator).to eq(distributor_managed) - expect(oc.distributors).to eq([distributor_unmanaged]) - expect(oc.schedules).to eq([schedule]) + expect(page).to have_field 'order_cycle_name', with: oc.name + select2_select schedule.name, from: 'schedule_ids' + expect(page).not_to have_select2 'schedule_ids', + with_options: [schedule_of_other_managed_distributor.name] + + click_button 'Save and Next' + + # When I remove all incoming exchanges + page.find("tr.supplier-#{supplier_managed.id} a.remove-exchange").click + page.find("tr.supplier-#{supplier_permitted.id} a.remove-exchange").click + click_button 'Save and Next' + + # And I remove all outgoing exchanges + page.find("tr.distributor-#{distributor_managed.id} a.remove-exchange").click + page.find("tr.distributor-#{distributor_permitted.id} a.remove-exchange").click + click_button 'Save and Back to List' + expect(page).to have_input "oc#{oc.id}[name]", value: oc.name + + oc.reload + expect(oc.suppliers).to eq([supplier_unmanaged]) + expect(oc.coordinator).to eq(distributor_managed) + expect(oc.distributors).to eq([distributor_unmanaged]) + expect(oc.schedules).to eq([schedule]) + end end it "cloning an order cycle" do From 9be37b0aaac784caa0022020c7ba1ce4556061b2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 12 Jan 2022 10:55:06 +0000 Subject: [PATCH 3/4] Reset flags when cloning an OC --- app/models/order_cycle.rb | 2 +- spec/models/order_cycle_spec.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 6de50ddc7f..2d2c14dbe5 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -151,7 +151,7 @@ class OrderCycle < ApplicationRecord def clone! oc = dup oc.name = I18n.t("models.order_cycle.cloned_order_cycle_name", order_cycle: oc.name) - oc.orders_open_at = oc.orders_close_at = nil + oc.orders_open_at = oc.orders_close_at = oc.mails_sent = oc.processed_at = nil oc.coordinator_fee_ids = coordinator_fee_ids # rubocop:disable Layout/LineLength oc.preferred_product_selection_from_coordinator_inventory_only = preferred_product_selection_from_coordinator_inventory_only diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 19a94492ed..0a36ca7459 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -373,7 +373,7 @@ describe OrderCycle do oc = create(:simple_order_cycle, coordinator_fees: [create(:enterprise_fee, enterprise: coordinator)], preferred_product_selection_from_coordinator_inventory_only: true, - automatic_notifications: true) + automatic_notifications: true, processed_at: Time.zone.now, mails_sent: true) ex1 = create(:exchange, order_cycle: oc) ex2 = create(:exchange, order_cycle: oc) oc.clone! @@ -385,6 +385,8 @@ describe OrderCycle do expect(occ.coordinator).not_to be_nil expect(occ.preferred_product_selection_from_coordinator_inventory_only).to be true expect(occ.automatic_notifications).to eq(oc.automatic_notifications) + expect(occ.processed_at).to eq(nil) + expect(occ.mails_sent).to eq(nil) expect(occ.coordinator).to eq(oc.coordinator) expect(occ.coordinator_fee_ids).not_to be_empty From 534354a1770199651745525748b141cbbead9bd0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 14 Jan 2022 11:23:41 +0000 Subject: [PATCH 4/4] Add data migration --- .../20220114110920_update_order_cycle_mails.rb | 12 ++++++++++++ db/schema.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220114110920_update_order_cycle_mails.rb diff --git a/db/migrate/20220114110920_update_order_cycle_mails.rb b/db/migrate/20220114110920_update_order_cycle_mails.rb new file mode 100644 index 0000000000..aa4fef0d6f --- /dev/null +++ b/db/migrate/20220114110920_update_order_cycle_mails.rb @@ -0,0 +1,12 @@ +class UpdateOrderCycleMails < ActiveRecord::Migration[6.1] + class MigrationOrderCycle < ActiveRecord::Base + self.table_name = "order_cycles" + end + + def up + MigrationOrderCycle. + where(automatic_notifications: true). + where.not(processed_at: nil). + update_all(mails_sent: true) + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a1d641eee..d3ec898e90 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_01_12_102539) do +ActiveRecord::Schema.define(version: 2022_01_14_110920) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"