Aggiunge pulsante per fermare un invio email in corso.
Permette di annullare i destinatari ancora in coda mantenendo le email già inviate. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -72,6 +72,10 @@ class Mailing < ApplicationRecord
|
||||
status == "sent"
|
||||
end
|
||||
|
||||
def cancelled?
|
||||
status == "cancelled"
|
||||
end
|
||||
|
||||
def editable?
|
||||
draft?
|
||||
end
|
||||
@@ -187,6 +191,17 @@ class Mailing < ApplicationRecord
|
||||
update!(status: "sent", completed_at: Time.current, next_send_at: nil)
|
||||
end
|
||||
|
||||
def cancel_send!
|
||||
raise "Solo un invio in corso può essere fermato" unless sending?
|
||||
|
||||
transaction do
|
||||
mailing_recipients.where(status: %w[pending queued]).find_each do |recipient|
|
||||
recipient.update!(status: "skipped", skip_reason: "invio annullato")
|
||||
end
|
||||
update!(status: "cancelled", next_send_at: nil, completed_at: Time.current)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_window_order
|
||||
|
||||
@@ -47,10 +47,13 @@ class MailingRecipient < ApplicationRecord
|
||||
def deliver!
|
||||
with_lock do
|
||||
return if status.in?(%w[sent skipped])
|
||||
return abort_delivery!("invio annullato") unless mailing.sending?
|
||||
|
||||
update!(status: "queued")
|
||||
end
|
||||
|
||||
return unless mailing.sending?
|
||||
|
||||
html = rendered_html
|
||||
subject_line = rendered_subject_line
|
||||
CampaignMailer.outreach(self, html: html, subject: subject_line).deliver_now
|
||||
@@ -63,6 +66,10 @@ class MailingRecipient < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def abort_delivery!(reason)
|
||||
update!(status: "skipped", skip_reason: reason)
|
||||
end
|
||||
|
||||
def record_success!(subject_line)
|
||||
transaction do
|
||||
update!(status: "sent", sent_at: Time.current, rendered_subject: subject_line, error_message: nil)
|
||||
|
||||
Reference in New Issue
Block a user