From 8daa01e2288c8db2a2c7029d628fbc69143773e8 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 2 May 2025 16:58:00 +1000 Subject: [PATCH] Process OC emails individually to avoid duplicates Errors or delays can cause multiple duplicate emails to the same suppliers. --- app/jobs/order_cycle_notification_job.rb | 2 +- spec/jobs/order_cycle_notification_job_spec.rb | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/jobs/order_cycle_notification_job.rb b/app/jobs/order_cycle_notification_job.rb index c713e12472..42862d832d 100644 --- a/app/jobs/order_cycle_notification_job.rb +++ b/app/jobs/order_cycle_notification_job.rb @@ -5,7 +5,7 @@ class OrderCycleNotificationJob < ApplicationJob def perform(order_cycle_id) order_cycle = OrderCycle.find(order_cycle_id) order_cycle.suppliers.each do |supplier| - ProducerMailer.order_cycle_report(supplier, order_cycle).deliver_now + ProducerMailer.order_cycle_report(supplier, order_cycle).deliver_later end order_cycle.update_columns mails_sent: true end diff --git a/spec/jobs/order_cycle_notification_job_spec.rb b/spec/jobs/order_cycle_notification_job_spec.rb index f8496fc86c..872b63d6ad 100644 --- a/spec/jobs/order_cycle_notification_job_spec.rb +++ b/spec/jobs/order_cycle_notification_job_spec.rb @@ -4,20 +4,18 @@ require 'spec_helper' RSpec.describe OrderCycleNotificationJob do let(:order_cycle) { create(:order_cycle) } - let(:mail) { double(:mail, deliver_now: true) } - - before do - allow(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail) - end it "sends a mail to each supplier" do - OrderCycleNotificationJob.perform_now order_cycle.id - expect(ProducerMailer).to have_received(:order_cycle_report).twice + expect { + OrderCycleNotificationJob.perform_now(order_cycle.id) + }.to enqueue_mail(ProducerMailer, :order_cycle_report).twice end it "records that mails have been sent for the order cycle" do - expect do + expect { OrderCycleNotificationJob.perform_now(order_cycle.id) - end.to change{ order_cycle.reload.mails_sent? }.from(false).to(true) + }.to change { + order_cycle.reload.mails_sent? + }.from(false).to(true) end end