amelioration(dolist_api): forward le message_id afin de faciliter les investigations

This commit is contained in:
Martin 2023-02-03 15:40:16 +01:00 committed by mfo
parent 709a9e82a2
commit 2de9026c13
5 changed files with 27 additions and 3 deletions

View file

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

View file

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

View file

@ -0,0 +1,5 @@
class AddMessageIdToEmailEvent < ActiveRecord::Migration[6.1]
def change
add_column :email_events, :message_id, :string
end
end

View file

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

View file

@ -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") }