From e74cf2bc7f01d5d57f91c50d59d54fd051f3b74a Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 11 Nov 2021 12:31:53 +0200 Subject: [PATCH 1/3] added mail contronller and job for notificate registrars and admins about accreditation expire date --- .../accreditation_results_controller.rb | 28 +++++++++++++++--- ...accreditation_admins_and_registrars_job.rb | 29 +++++++++++++++++++ app/mailers/accreditation_center_mailer.rb | 21 ++++++++++++++ .../test_results_are_expired.html.erb | 1 + ...results_will_expired_in_one_month.html.erb | 1 + ...est_was_successfully_passed_admin.html.erb | 1 + ...was_successfully_passed_registrar.html.erb | 1 + 7 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 app/jobs/notify_accreditation_admins_and_registrars_job.rb create mode 100644 app/mailers/accreditation_center_mailer.rb create mode 100644 app/views/mailers/accreditation_center_mailer/test_results_are_expired.html.erb create mode 100644 app/views/mailers/accreditation_center_mailer/test_results_will_expired_in_one_month.html.erb create mode 100644 app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_admin.html.erb create mode 100644 app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_registrar.html.erb diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index b33ed58ee1..2efe345bfa 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -5,6 +5,7 @@ class AccreditationResultsController < ActionController::API before_action :authenticate_shared_key TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze + EXPIRE_DEADLINE = 15.minutes.freeze api :POST, 'repp/v1/registrar/accreditation/push_results' desc 'added datetime results' @@ -26,12 +27,31 @@ def record_accreditation_result(username, result) raise ActiveRecord::RecordNotFound if user.nil? user.accreditation_date = DateTime.current + user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE - return unless user.save + if user.save + notify_registrar(user) + notify_admins + render_success(data: { user: user, + result: result, + message: 'Accreditation info successfully added' }) + else + render_failed + end + end + + def notify_registrar(user) + AccreditationCenterMailer.test_was_successfully_passed_registrar(user.registrar.email).deliver_now + end + + def notify_admins + admin_users_emails = User.all.reject { |u| u.roles.nil? }.select { |u| u.roles.include? "admin" }.pluck(:email) + + return if admin_users_emails.empty? - render_success(data: { user: user, - result: result, - message: 'Accreditation info successfully added' }) + admin_users_emails.each do |email| + AccreditationCenterMailer.test_was_successfully_passed_admin(email).deliver_now + end end def authenticate_shared_key diff --git a/app/jobs/notify_accreditation_admins_and_registrars_job.rb b/app/jobs/notify_accreditation_admins_and_registrars_job.rb new file mode 100644 index 0000000000..ce1aa62b36 --- /dev/null +++ b/app/jobs/notify_accreditation_admins_and_registrars_job.rb @@ -0,0 +1,29 @@ +class NotifyAccreditationAdminsAndRegistrarsJob < ApplicationJob + MONTH_BEFORE = 5.minute.freeze + + def perform + prepare_data_month_before.each do |user| + next if user.registrar.email.nil? + + AccreditationCenterMailer.test_results_will_expired_in_one_month(user.registrar.email).deliver_now + end + + prepare_data_expired_data.each do |user| + next if user.registrar.email.nil? + + AccreditationCenterMailer.test_results_are_expired(user.registrar.email).deliver_now + end + end + + private + + def prepare_data_month_before + ApiUser.where("accreditation_expire_date > ? AND accreditation_expire_date < ?", + Time.now.beginning_of_day + MONTH_BEFORE, + Time.now.end_of_day + MONTH_BEFORE).includes(:registrar) + end + + def prepare_data_expired_data + ApiUser.where("accreditation_expire_date < ?", Time.now.beginning_of_day).includes(:registrar) + end +end diff --git a/app/mailers/accreditation_center_mailer.rb b/app/mailers/accreditation_center_mailer.rb new file mode 100644 index 0000000000..9bfea14e03 --- /dev/null +++ b/app/mailers/accreditation_center_mailer.rb @@ -0,0 +1,21 @@ +class AccreditationCenterMailer < ApplicationMailer + def test_was_successfully_passed_admin(email) + subject = 'Test passed admin' + mail(to: email, subject: subject) + end + + def test_was_successfully_passed_registrar(email) + subject = 'Test passed registrar' + mail(to: email, subject: subject) + end + + def test_results_will_expired_in_one_month(email) + subject = 'Test will expired in one month' + mail(to: email, subject: subject) + end + + def test_results_are_expired(email) + subject = 'Test are expired' + mail(to: email, subject: subject) + end +end diff --git a/app/views/mailers/accreditation_center_mailer/test_results_are_expired.html.erb b/app/views/mailers/accreditation_center_mailer/test_results_are_expired.html.erb new file mode 100644 index 0000000000..9b67376d4f --- /dev/null +++ b/app/views/mailers/accreditation_center_mailer/test_results_are_expired.html.erb @@ -0,0 +1 @@ +

Test result are expired

diff --git a/app/views/mailers/accreditation_center_mailer/test_results_will_expired_in_one_month.html.erb b/app/views/mailers/accreditation_center_mailer/test_results_will_expired_in_one_month.html.erb new file mode 100644 index 0000000000..b9d778f06b --- /dev/null +++ b/app/views/mailers/accreditation_center_mailer/test_results_will_expired_in_one_month.html.erb @@ -0,0 +1 @@ +

Test result will expired in one month

diff --git a/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_admin.html.erb b/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_admin.html.erb new file mode 100644 index 0000000000..31b5c2f451 --- /dev/null +++ b/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_admin.html.erb @@ -0,0 +1 @@ +

Test was successfully passed (ADMIN)

diff --git a/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_registrar.html.erb b/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_registrar.html.erb new file mode 100644 index 0000000000..e562982ba0 --- /dev/null +++ b/app/views/mailers/accreditation_center_mailer/test_was_successfully_passed_registrar.html.erb @@ -0,0 +1 @@ +

Test was successfully passed (REGISTRAR)

From d6824bf46c8fdbeaa362ced66e831c6215c5c34d Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 11 Nov 2021 13:10:27 +0200 Subject: [PATCH 2/3] added tests --- .../accreditation_results_controller.rb | 3 +- ...accreditation_admins_and_registrars_job.rb | 15 +++++-- .../registrar/accreditation_results_test.rb | 3 ++ ...ditation_admins_and_registrars_job_test.rb | 38 ++++++++++++++++++ .../accrediation_center_mailer_test.rb | 40 +++++++++++++++++++ 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 test/jobs/notify_accreditation_admins_and_registrars_job_test.rb create mode 100644 test/mailers/accrediation_center_mailer_test.rb diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index 2efe345bfa..e860a86795 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -45,7 +45,8 @@ def notify_registrar(user) end def notify_admins - admin_users_emails = User.all.reject { |u| u.roles.nil? }.select { |u| u.roles.include? "admin" }.pluck(:email) + admin_users_emails = User.all.reject { |u| u.roles.nil? }. + select { |u| u.roles.include? 'admin' }.pluck(:email) return if admin_users_emails.empty? diff --git a/app/jobs/notify_accreditation_admins_and_registrars_job.rb b/app/jobs/notify_accreditation_admins_and_registrars_job.rb index ce1aa62b36..50252ce9cd 100644 --- a/app/jobs/notify_accreditation_admins_and_registrars_job.rb +++ b/app/jobs/notify_accreditation_admins_and_registrars_job.rb @@ -1,13 +1,20 @@ class NotifyAccreditationAdminsAndRegistrarsJob < ApplicationJob - MONTH_BEFORE = 5.minute.freeze + MONTH_BEFORE = 5.minutes.freeze def perform + notify_month_before + notify_date_is_expired + end + + def notify_month_before prepare_data_month_before.each do |user| next if user.registrar.email.nil? AccreditationCenterMailer.test_results_will_expired_in_one_month(user.registrar.email).deliver_now end + end + def notify_date_is_expired prepare_data_expired_data.each do |user| next if user.registrar.email.nil? @@ -19,11 +26,11 @@ def perform def prepare_data_month_before ApiUser.where("accreditation_expire_date > ? AND accreditation_expire_date < ?", - Time.now.beginning_of_day + MONTH_BEFORE, - Time.now.end_of_day + MONTH_BEFORE).includes(:registrar) + Time.zone.now.beginning_of_day + MONTH_BEFORE, + Time.zone.now.end_of_day + MONTH_BEFORE).includes(:registrar) end def prepare_data_expired_data - ApiUser.where("accreditation_expire_date < ?", Time.now.beginning_of_day).includes(:registrar) + ApiUser.where("accreditation_expire_date < ?", Time.zone.now.beginning_of_day).includes(:registrar) end end diff --git a/test/integration/repp/v1/registrar/accreditation_results_test.rb b/test/integration/repp/v1/registrar/accreditation_results_test.rb index ac9d4fa4ac..92c5df37ca 100644 --- a/test/integration/repp/v1/registrar/accreditation_results_test.rb +++ b/test/integration/repp/v1/registrar/accreditation_results_test.rb @@ -18,6 +18,7 @@ def test_should_return_valid_response json = JSON.parse(response.body, symbolize_names: true) assert_response :ok + assert_emails 2 assert_equal json[:data][:user][:username], @user.username assert_equal json[:data][:result], "true" assert_equal json[:data][:message], "Accreditation info successfully added" @@ -31,6 +32,7 @@ def test_should_return_valid_response_invalid_authorization assert_response :unauthorized + assert_emails 0 assert_equal json[:code], 2202 assert_equal json[:message], 'Invalid authorization information' end @@ -43,6 +45,7 @@ def test_should_return_valid_response_record_exception assert_response :ok + assert_emails 0 assert_equal json[:code], 2303 assert_equal json[:message], "Object 'chungachanga' does not exist" end diff --git a/test/jobs/notify_accreditation_admins_and_registrars_job_test.rb b/test/jobs/notify_accreditation_admins_and_registrars_job_test.rb new file mode 100644 index 0000000000..82cf2b87d4 --- /dev/null +++ b/test/jobs/notify_accreditation_admins_and_registrars_job_test.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class NotifyAccreditationAdminsAndRegistrarsJobTest < ActiveJob::TestCase + include ActionMailer::TestHelper + + setup do + ActionMailer::Base.deliveries.clear + end + + def test_inform_registrars_if_accredation_date_is_expires + api_user = users(:api_bestnames) + api_user.accreditation_date = Time.now - 2.year - 1.day + api_user.accreditation_expire_date = Time.now - 1.day + api_user.save + api_user.reload + + perform_enqueued_jobs do + NotifyAccreditationAdminsAndRegistrarsJob.perform_now + end + + assert_emails 1 + end + + # def test_inform_registrars_if_deadline_date_in_one_month + # api_user = users(:api_bestnames) + # api_user.accreditation_date = Time.now - 2.year - 1.day + # api_user.accreditation_expire_date = Time.now + 1.month - 1.day + # api_user.save + # api_user.reload + # + # perform_enqueued_jobs do + # NotifyAccreditationAdminsAndRegistrarsJob.perform_now + # end + # + # assert_emails 1 + # end + +end diff --git a/test/mailers/accrediation_center_mailer_test.rb b/test/mailers/accrediation_center_mailer_test.rb new file mode 100644 index 0000000000..5aa2568955 --- /dev/null +++ b/test/mailers/accrediation_center_mailer_test.rb @@ -0,0 +1,40 @@ +require 'test_helper' + +class AccreditationCenterMailerTest < ActionMailer::TestCase + setup do + @admin = users(:admin) + @registrar = registrars(:bestnames) + end + + def test_send_mails_for_admins + email = AccreditationCenterMailer.test_was_successfully_passed_admin(@admin.email) + .deliver_now + + assert_emails 1 + assert_equal [@admin.email], email.to + end + + def test_send_mails_for_registrar + email = AccreditationCenterMailer.test_was_successfully_passed_registrar(@registrar.email) + .deliver_now + + assert_emails 1 + assert_equal [@registrar.email], email.to + end + + def test_send_mails_month_before + email = AccreditationCenterMailer.test_results_will_expired_in_one_month(@registrar.email) + .deliver_now + + assert_emails 1 + assert_equal [@registrar.email], email.to + end + + def test_send_mails_if_accredation_date_is_expired + email = AccreditationCenterMailer.test_results_are_expired(@registrar.email) + .deliver_now + + assert_emails 1 + assert_equal [@registrar.email], email.to + end +end From 74efc601392048e4516a8cbdccfd71546229583d Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 11 Nov 2021 13:14:09 +0200 Subject: [PATCH 3/3] refactored --- .../repp/v1/registrar/accreditation_results_controller.rb | 4 ++-- app/jobs/notify_accreditation_admins_and_registrars_job.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index e860a86795..b58fd3895d 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -45,8 +45,8 @@ def notify_registrar(user) end def notify_admins - admin_users_emails = User.all.reject { |u| u.roles.nil? }. - select { |u| u.roles.include? 'admin' }.pluck(:email) + admin_users_emails = User.all.reject { |u| u.roles.nil? } + .select { |u| u.roles.include? 'admin' }.pluck(:email) return if admin_users_emails.empty? diff --git a/app/jobs/notify_accreditation_admins_and_registrars_job.rb b/app/jobs/notify_accreditation_admins_and_registrars_job.rb index 50252ce9cd..72ea5dfe59 100644 --- a/app/jobs/notify_accreditation_admins_and_registrars_job.rb +++ b/app/jobs/notify_accreditation_admins_and_registrars_job.rb @@ -25,12 +25,12 @@ def notify_date_is_expired private def prepare_data_month_before - ApiUser.where("accreditation_expire_date > ? AND accreditation_expire_date < ?", + ApiUser.where('accreditation_expire_date > ? AND accreditation_expire_date < ?', Time.zone.now.beginning_of_day + MONTH_BEFORE, Time.zone.now.end_of_day + MONTH_BEFORE).includes(:registrar) end def prepare_data_expired_data - ApiUser.where("accreditation_expire_date < ?", Time.zone.now.beginning_of_day).includes(:registrar) + ApiUser.where('accreditation_expire_date < ?', Time.zone.now.beginning_of_day).includes(:registrar) end end