Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added mail contronller and job for notificate registrars and admins about accreditation expire date #2212

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -26,12 +27,32 @@ 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
Expand Down
36 changes: 36 additions & 0 deletions app/jobs/notify_accreditation_admins_and_registrars_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class NotifyAccreditationAdminsAndRegistrarsJob < ApplicationJob
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?

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.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)
end
end
21 changes: 21 additions & 0 deletions app/mailers/accreditation_center_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Test result are expired</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Test result will expired in one month</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Test was successfully passed (ADMIN)</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Test was successfully passed (REGISTRAR)</h1>
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions test/jobs/notify_accreditation_admins_and_registrars_job_test.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions test/mailers/accrediation_center_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -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