mirror of
https://github.com/internetee/registry.git
synced 2025-06-11 23:24:48 +02:00
Add email verification rake task
This commit is contained in:
parent
ee7d69c1ce
commit
a13725faa7
3 changed files with 35 additions and 7 deletions
|
@ -1,8 +1,7 @@
|
||||||
class SendEInvoiceJob < Que::Job
|
class VerifyEmailsJob < Que::Job
|
||||||
|
|
||||||
def run(verification_id)
|
def run(verification_id)
|
||||||
email_address_verification = run_condition(EmailAddressVerification
|
email_address_verification = run_condition(EmailAddressVerification.find(verification_id))
|
||||||
.find(run_condition(verification_id)))
|
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
email_address_verification.verify
|
email_address_verification.verify
|
||||||
|
@ -17,12 +16,14 @@ class SendEInvoiceJob < Que::Job
|
||||||
private
|
private
|
||||||
|
|
||||||
def run_condition(email_address_verification)
|
def run_condition(email_address_verification)
|
||||||
|
destroy unless email_address_verification
|
||||||
destroy if email_address_verification.recently_verified?
|
destroy if email_address_verification.recently_verified?
|
||||||
|
|
||||||
email_address_verification
|
email_address_verification
|
||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
Rails.logger
|
Rails.logger = Logger.new(STDOUT)
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_success(verification)
|
def log_success(verification)
|
||||||
|
|
|
@ -1,14 +1,30 @@
|
||||||
class EmailAddressVerification < ApplicationRecord
|
class EmailAddressVerification < ApplicationRecord
|
||||||
|
|
||||||
RECENTLY_VERIFIED_PERIOD = 1.month
|
RECENTLY_VERIFIED_PERIOD = 1.month
|
||||||
|
|
||||||
|
scope :not_verified_recently, -> {
|
||||||
|
where('verified_at IS NULL or verified_at < ?', verification_period)
|
||||||
|
}
|
||||||
|
|
||||||
|
scope :verified_recently, -> {
|
||||||
|
where('verified_at IS NOT NULL and verified_at >= ?', verification_period)
|
||||||
|
}
|
||||||
|
|
||||||
def recently_verified?
|
def recently_verified?
|
||||||
verified_at.present? &&
|
verified_at.present? &&
|
||||||
verified_at > Time.zone.now - RECENTLY_VERIFIED_PERIOD
|
verified_at > verification_period
|
||||||
|
end
|
||||||
|
|
||||||
|
def verification_period
|
||||||
|
self.class.verification_period
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.verification_period
|
||||||
|
Time.zone.now - RECENTLY_VERIFIED_PERIOD
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify
|
def verify
|
||||||
validation_request = Truemail.validate(email)
|
media = success ? :mx : :smtp
|
||||||
|
validation_request = Truemail.validate(email, with: media)
|
||||||
|
|
||||||
if validation_request.result.success
|
if validation_request.result.success
|
||||||
update(verified_at: Time.zone.now,
|
update(verified_at: Time.zone.now,
|
||||||
|
|
11
lib/tasks/verify_email.rake
Normal file
11
lib/tasks/verify_email.rake
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
namespace :verify_email do
|
||||||
|
desc 'Stars verifying email jobs'
|
||||||
|
task all_domains: :environment do
|
||||||
|
verifications_by_domain = EmailAddressVerification.not_verified_recently.group_by(&:domain)
|
||||||
|
|
||||||
|
verifications_by_domain.each do |_domain, verifications|
|
||||||
|
ver = verifications[0] # Only first email to not to clog the SMTP servers
|
||||||
|
VerifyEmailsJob.enqueue(ver.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue