Add a form to email users from internal (#6668)

Co-authored-by: MichaelaHunter <michaela1234@gmail.com>

Co-authored-by: MichaelaHunter <michaela1234@gmail.com>
This commit is contained in:
Jacob Herrington 2020-03-17 10:19:42 -05:00 committed by GitHub
parent b8b321c13d
commit 243e9b5524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 0 deletions

View file

@ -101,6 +101,14 @@ class Internal::UsersController < Internal::ApplicationController
redirect_to "/internal/users/#{@user.id}/edit"
end
def send_email
if NotifyMailer.user_contact_email(params).deliver
redirect_back(fallback_location: "/users")
else
flash[:danger] = "Email failed to send!"
end
end
private
def manage_credits

View file

@ -66,6 +66,13 @@ class NotifyMailer < ApplicationMailer
mail(to: params[:email_to], subject: params[:email_subject])
end
def user_contact_email(params)
@user = User.find(params[:user_id])
@email_body = params[:email_body]
track utm_campaign: "user_contact"
mail(to: @user.email, subject: params[:email_subject])
end
def new_message_email(direct_message)
@message = direct_message
@user = @message.direct_receiver

View file

@ -49,6 +49,21 @@
<%= render "notes" %>
<%= render "user_organizations" %>
<div class="row mb-3">
<div class="col-12">
<h2>Email <%= @user.name %></h2>
<%= form_with url: send_email_internal_user_path(@user), local: true do |f| %>
<%= f.hidden_field(:user_id, value: @user.id) %>
<h4>Subject:</h4>
<%= f.text_field :email_subject, class: "form-control my-1" %>
<h4>Body:</h4>
<%= f.text_area :email_body, class: "form-control my-1", rows: "10" %>
<%= f.submit "Send Email", class: "btn btn-primary float-right" %>
<% end %>
</div>
</div>
<div class="row mb-3">
<div class="col-12">
<h2>Recent Emails</h2>

View file

@ -0,0 +1,3 @@
<p>
<%= simple_format @email_body %>
</p>

View file

@ -0,0 +1 @@
<%= @email_body %>

View file

@ -78,6 +78,7 @@ Rails.application.routes.draw do
post "merge"
delete "remove_identity"
post "recover_identity"
post "send_email"
end
end
resources :organization_memberships, only: %i[update destroy create]

View file

@ -228,6 +228,38 @@ RSpec.describe NotifyMailer, type: :mailer do
end
end
describe "#user_contact_email" do
let(:email_params) do
{
user_id: user.id,
email_subject: "Buddy",
email_body: "Laugh with me, buddy"
}
end
let(:email) { described_class.user_contact_email(email_params) }
it "renders proper subject" do
expect(email.subject).to eq("Buddy")
end
it "renders proper sender" do
expect(email.from).to eq([SiteConfig.default_site_email])
expect(email["from"].value).to eq("DEV Community <#{SiteConfig.default_site_email}>")
end
it "renders proper receiver" do
expect(email.to).to eq([user.email])
end
it "includes the tracking pixel" do
expect(email.html_part.body).to include("open.gif")
end
it "includes UTM params" do
expect(email.html_part.body).to include(CGI.escape("utm_campaign=user_contact"))
end
end
describe "#new_message_email" do
let(:direct_channel) { ChatChannel.create_with_users([user, user2], "direct") }
let(:direct_message) { create(:message, user: user, chat_channel: direct_channel) }