diff --git a/app/models/email_event.rb b/app/models/email_event.rb index aa13275a6..49d2f0dad 100644 --- a/app/models/email_event.rb +++ b/app/models/email_event.rb @@ -10,6 +10,7 @@ # to :string not null # created_at :datetime not null # updated_at :datetime not null +# message_id :string # class EmailEvent < ApplicationRecord enum status: { @@ -30,6 +31,7 @@ class EmailEvent < ApplicationRecord subject: message.subject || "", processed_at: message.date, method: ActionMailer::Base.delivery_methods.key(message.delivery_method.class), + message_id: message.message_id, status: ) rescue StandardError => error diff --git a/config/initializers/dolist.rb b/config/initializers/dolist.rb index 1f59a888e..770f1ae1c 100644 --- a/config/initializers/dolist.rb +++ b/config/initializers/dolist.rb @@ -16,8 +16,9 @@ ActiveSupport.on_load(:action_mailer) do def deliver!(mail) response = Dolist::API.new.send_email(mail) - if !respons&.dig("Result") - Rails.logger.info "Email not sent. Error message: #{mail}" + + if response&.dig("Result") + mail.message_id = response.dig("Result") else Rails.logger.info "Email sent. #{mail}" end diff --git a/db/migrate/20230203134127_add_message_id_to_email_event.rb b/db/migrate/20230203134127_add_message_id_to_email_event.rb new file mode 100644 index 000000000..cf408ebd8 --- /dev/null +++ b/db/migrate/20230203134127_add_message_id_to_email_event.rb @@ -0,0 +1,5 @@ +class AddMessageIdToEmailEvent < ActiveRecord::Migration[6.1] + def change + add_column :email_events, :message_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 0ecac4241..d465805de 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: 2023_01_31_172119) do +ActiveRecord::Schema.define(version: 2023_02_03_134127) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -407,6 +407,7 @@ ActiveRecord::Schema.define(version: 2023_01_31_172119) do create_table "email_events", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false + t.string "message_id" t.string "method", null: false t.datetime "processed_at" t.string "status", null: false diff --git a/spec/mailers/application_mailer_spec.rb b/spec/mailers/application_mailer_spec.rb index 4146d56ad..2337b0e2c 100644 --- a/spec/mailers/application_mailer_spec.rb +++ b/spec/mailers/application_mailer_spec.rb @@ -26,6 +26,21 @@ RSpec.describe ApplicationMailer, type: :mailer do end end + describe 'dealing with Dolist API success' do + let(:dossier) { create(:dossier, procedure: create(:simple_procedure)) } + let(:message_id) { "29d9b692-0374-4084-8434-d9cddbced205" } + before do + ActionMailer::Base.delivery_method = :dolist_api + api_success_response = { "Result" => message_id } + allow_any_instance_of(Dolist::API).to receive(:send_email).and_return(api_success_response) + end + subject { DossierMailer.with(dossier:).notify_new_draft.deliver_now } + + it 'forward message ID to observer and EmailEvent.create_from_message!' do + expect { subject }.to change { EmailEvent.dolist_api.dispatched.where(message_id:).count }.to eq(1) + end + end + describe 'EmailDeliveryObserver is invoked' do let(:user1) { create(:user) } let(:user2) { create(:user, email: "your@email.com") }