Use mails_sent flag for tracking mails

This commit is contained in:
Matt-Yorkley
2022-01-12 10:36:17 +00:00
parent 8f825a709c
commit 54e1a8807a
5 changed files with 53 additions and 29 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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