diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 40c0cbe..afa3b5e 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -77,4 +77,18 @@ subject: _('DMP Visibility Changed: %{plan_title}') %{ :plan_title => @plan.title }) end end + # @param commenter - User who wrote the comment + # @param plan - Plan for which the comment is associated to + def new_comment(commenter, plan) + if commenter.is_a?(User) && plan.is_a?(Plan) + if plan.owner.present? + @commenter = commenter + @plan = plan + FastGettext.with_locale FastGettext.default_locale do + mail(to: plan.owner.email, subject: + _('%{tool_name}: A new comment was added to %{plan_title}') %{ :tool_name => Rails.configuration.branding[:application][:name], :plan_title => plan.title }) + end + end + end + end end diff --git a/app/models/note.rb b/app/models/note.rb index 4341c2a..e20cc04 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -11,4 +11,10 @@ :answer, :user, :as => [:default, :admin] validates :text, :answer, :user, presence: {message: _("can't be blank")} + + # Active Record Callbacks + after_create do + # Sends an email to the plan owner regarding a new comment created by new note user + UserMailer.new_comment(self.user, self.answer.plan).deliver_now() + end end diff --git a/app/views/user_mailer/new_comment.html.erb b/app/views/user_mailer/new_comment.html.erb new file mode 100644 index 0000000..f10743a --- /dev/null +++ b/app/views/user_mailer/new_comment.html.erb @@ -0,0 +1,27 @@ +<% + tool_name = Rails.configuration.branding[:application][:name] + commenter_name = @commenter.name + plan_title = @plan.title + user_name = @plan.owner.name + helpdesk_email = Rails.configuration.branding[:organisation][:helpdesk_email] + contact_us_url = Rails.configuration.branding[:organisation][:contact_us_url] + email_subject = _('Query or feedback related to %{tool_name}') %{ :tool_name => tool_name } +%> +<% FastGettext.with_locale FastGettext.default_locale do %> +

+ <%= _('Hello %{user_name}') %{ :user_name => user_name } %> +

+

+ <%= _('%{commenter_name} has commented on the plan %{plan_title}. To view the comments, '\ + 'please visit the My Dashboard page in %{tool_name} and open your plan.') %{ :plan_title => plan_title, + :commenter_name => commenter_name, :tool_name => tool_name } %> +

+

+ <%= _('All the best') %> +
+ <%= _('The %{tool_name} team') %{:tool_name => tool_name} %> +

+

+ <%= _('You may change your notification preferences on your profile page.') %> <%= _('Please do not reply to this email.') %> <%= raw(_('If you have any questions or need help, please contact us at %{helpdesk_email} or visit %{contact_us_url}') %{ :helpdesk_email => mail_to(helpdesk_email, helpdesk_email, subject: email_subject), :contact_us_url => link_to(contact_us_url, contact_us_url) }) %> +

+<% end %> \ No newline at end of file diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index 39e41aa..8755cf8 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -18,4 +18,9 @@ user = User.find_by(email: 'super_admin@example.com') UserMailer.plan_visibility(user, user.plans.first) end + def new_comment + commenter = User.find_by(email: 'super_admin@example.com') + plan = Plan.joins(:roles).where(Role.creator_condition).first + UserMailer.new_comment(commenter, plan) + end end \ No newline at end of file