Browse Source

Not part of the usual app flow so contain to rake task

pull/566/head
baarkerlounger 3 years ago
parent
commit
d8273ebd68
  1. 11
      app/models/user.rb
  2. 10
      lib/tasks/onboarding_emails.rake
  3. 1
      spec/lib/tasks/onboarding_emails_spec.rb

11
app/models/user.rb

@ -67,7 +67,6 @@ class User < ApplicationRecord
MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze
RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".freeze RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".freeze
SET_PASSWORD_TEMPLATE_ID = "257460a6-6616-4640-a3f9-17c3d73d9e91".freeze SET_PASSWORD_TEMPLATE_ID = "257460a6-6616-4640-a3f9-17c3d73d9e91".freeze
BETA_ONBOARDING_TEMPLATE_ID = "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd".freeze
def reset_password_notify_template def reset_password_notify_template
last_sign_in_at ? RESET_PASSWORD_TEMPLATE_ID : SET_PASSWORD_TEMPLATE_ID last_sign_in_at ? RESET_PASSWORD_TEMPLATE_ID : SET_PASSWORD_TEMPLATE_ID
@ -83,16 +82,6 @@ class User < ApplicationRecord
DeviseNotifyMailer.new.send_email(email, template_id, personalisation) DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end end
def send_beta_onboarding_email(host)
return unless URI::MailTo::EMAIL_REGEXP.match?(email) && host
template_id = BETA_ONBOARDING_TEMPLATE_ID
url = edit_user_password_url({ host: })
token = Devise.token_generator.generate(User, :reset_password_token)
personalisation = { name: name || email, link: "#{url}?reset_password_token=#{token}" }
DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end
def assignable_roles def assignable_roles
return {} unless data_coordinator? || support? return {} unless data_coordinator? || support?
return ROLES if support? return ROLES if support?

10
lib/tasks/onboarding_emails.rake

@ -11,6 +11,14 @@ namespace :onboarding_emails do
organisation = Organisation.find(organisation_id) organisation = Organisation.find(organisation_id)
raise "Organisation #{organisation_id} does not exist" unless organisation raise "Organisation #{organisation_id} does not exist" unless organisation
organisation.users.each { |user| user.send_beta_onboarding_email(host) } organisation.users.each do |user|
return unless URI::MailTo::EMAIL_REGEXP.match?(user.email)
onboarding_template_id = "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd".freeze
token = user.send(:set_reset_password_token)
url = "#{host}/account/password/edit?reset_password_token=#{token}"
personalisation = { name: user.name || user.email, link: url }
DeviseNotifyMailer.new.send_email(user.email, onboarding_template_id, personalisation)
end
end end
end end

1
spec/lib/tasks/onboarding_emails_spec.rb

@ -12,7 +12,6 @@ describe "rake onboarding_emails:send", type: task do
before do before do
Rake.application.rake_require("tasks/onboarding_emails") Rake.application.rake_require("tasks/onboarding_emails")
Rake::Task.define_task(:environment)
task.reenable task.reenable
allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client) allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)

Loading…
Cancel
Save