From d8273ebd68ef45048015452774c7fd446338207f Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 10 May 2022 20:48:51 +0100 Subject: [PATCH] Not part of the usual app flow so contain to rake task --- app/models/user.rb | 11 ----------- lib/tasks/onboarding_emails.rake | 10 +++++++++- spec/lib/tasks/onboarding_emails_spec.rb | 1 - 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index d48364017..27a884fa4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -67,7 +67,6 @@ class User < ApplicationRecord MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".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 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) 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 return {} unless data_coordinator? || support? return ROLES if support? diff --git a/lib/tasks/onboarding_emails.rake b/lib/tasks/onboarding_emails.rake index ec8538436..eb77a70e8 100644 --- a/lib/tasks/onboarding_emails.rake +++ b/lib/tasks/onboarding_emails.rake @@ -11,6 +11,14 @@ namespace :onboarding_emails do organisation = Organisation.find(organisation_id) 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 diff --git a/spec/lib/tasks/onboarding_emails_spec.rb b/spec/lib/tasks/onboarding_emails_spec.rb index 4c563f9b1..b083e5814 100644 --- a/spec/lib/tasks/onboarding_emails_spec.rb +++ b/spec/lib/tasks/onboarding_emails_spec.rb @@ -12,7 +12,6 @@ describe "rake onboarding_emails:send", type: task do before do Rake.application.rake_require("tasks/onboarding_emails") - Rake::Task.define_task(:environment) task.reenable allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)