Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jw email part 4 dashboard #655

Merged
merged 16 commits into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/controllers/dashboard/associated_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def update
updater = Dashboard::AssociatedUserUpdater.new(id: params[:id], project_role: params[:project_role])

if updater.successful?
#We care about this because the new rights will determine what is rendered
# We care about this because the new rights will determine what is rendered
if @current_user_updated = params[:project_role][:identity_id].to_i == @user.id
@protocol_type = @protocol.type
protocol_role = updater.protocol_role
Expand All @@ -110,10 +110,13 @@ def update
end

def destroy
modified_user = @protocol_role.identity
@protocol = @protocol_role.protocol
epic_access = @protocol_role.epic_access
protocol_role_clone = @protocol_role.clone

@protocol.email_about_change_in_authorized_user(modified_user, "destroy")

@protocol_role.destroy

if @current_user_destroyed = protocol_role_clone.identity_id == @user.id
Expand Down
6 changes: 4 additions & 2 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
class UserMailer < ActionMailer::Base
default :from => NO_REPLY_FROM

def authorized_user_changed(user, protocol)
def authorized_user_changed(user, protocol, modified_user, action)
@action = action
@modified_user = modified_user
@send_to = user
@protocol = protocol

@protocol_link = DASHBOARD_LINK + "/protocols/#{@protocol.id}"
send_message("#{I18n.t('application_title')} Authorized Users")
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ def activate
update_attribute(:study_type_question_group_id, StudyTypeQuestionGroup.active.pluck(:id).first)
end

def email_about_change_in_authorized_user(modified_user, action)
# Alert authorized users of deleted authorized user
# Send emails if SEND_AUTHORIZED_USER_EMAILS is set to true and if there are any non-draft SSRs
# For example: if a SR has SSRs all with a status of 'draft', don't send emails
if SEND_AUTHORIZED_USER_EMAILS && sub_service_requests.where.not(status: 'draft').any?
emailed_associated_users.each do |project_role|
UserMailer.authorized_user_changed(project_role.identity, self, modified_user, action).deliver unless project_role.identity.email.blank?
end
end
end

def validate_funding_source
if self.funding_status == "funded" && self.funding_source.blank?
errors.add(:funding_source, "You must select a funding source")
Expand Down
2 changes: 2 additions & 0 deletions app/views/user_mailer/_acknowledgments.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- @protocol.service_requests.first.service_list.map{|k, v| v[:acks]}.flatten.uniq.each do |ack|
%p{:style => 'font-size: small;'}=ack
10 changes: 10 additions & 0 deletions app/views/user_mailer/_intro.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%p= "Dear #{@send_to.try(:full_name)},"
%p{ :style => 'display: inline;' }
- if @action == "destroy"
= t(:mailer)[:delete_intro]
- if @action == "add"
= t(:mailer)[:add_intro]
- if @action == "update"
= t(:mailer)[:update]
%p{ :style => 'display: inline;' }
%a{:style => "color: blue; font-weight: bold;", :href => @protocol_link}=t(:mailer)[:sparc_dashboard_period]
30 changes: 30 additions & 0 deletions app/views/user_mailer/_protocol_info_table.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%table{:style => 'border: 1px solid #000; width: 100%; border-collapse: collapse;'}
%thead
%tr{:style => 'border: 1px solid black;'}
%td{:colspan => 2}
- if @protocol.type.capitalize == "Project"
%strong= t(:notifier)[:p_info]
- else
%strong= t(:notifier)[:study_info]
%tbody
%tr{:style => 'border: 1px solid black;'}
- if @protocol.type.capitalize == "Project"
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:p_id]
- else
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:study_id]
%td{:style => 'border: 1px solid black;'}= @protocol.id
%tr{:style => 'border: 1px solid black;'}
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:title1]
%td{:style => 'border: 1px solid black;'}= @protocol.try(:short_title)
%tr{:style => 'border: 1px solid black;'}
- if @protocol.type.capitalize == "Project"
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:p_title]
- else
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:study_title]
%td{:style => 'border: 1px solid black;'}= @protocol.try(:title)
%tr{:style => 'border: 1px solid black;'}
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:s_name]
%td{:style => 'border: 1px solid black;'}= @protocol.try(:sponsor_name)
%tr{:style => 'border: 1px solid black;'}
%th{:style => 'border: 1px solid black; width:200px; text-align:left;'}= t(:notifier)[:source]
%td{:style => 'border: 1px solid black;'}= (@protocol.funding_source or @protocol.potential_funding_source or "").capitalize
20 changes: 20 additions & 0 deletions app/views/user_mailer/_user_info_table.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%table{:style => 'border: 1px solid #000; width: 100%; border-collapse: collapse;'}
%thead
%tr{:style => 'border: 1px solid black;'}
%td{:colspan => 5}
%strong= t(:notifier)[:u_info]
%tr
%th{:style => 'border: 1px solid black;'}= t(:mailer)[:user_modification]
%th{:style => 'border: 1px solid black;'}= t(:notifier)[:contact_info]
%th{:style => 'border: 1px solid black;'}= t(:notifier)[:role]
%th{:style => 'border: 1px solid black;'}= t(:mailer)[:proxy]
- if !@protocol.selected_for_epic.nil?
%th{:style => 'border: 1px solid black;'}= t(:notifier)[:epic_access]
%tbody
%tr{:style => 'border: 1px solid black;'}
%td{:style => 'border: 1px solid black; text-align: center;'}= @modified_user.full_name
%td{:style => 'border: 1px solid black; text-align: center;'}= @modified_user.email
%td{:style => 'border: 1px solid black; text-align: center;'}= @modified_user.project_roles.first.role.upcase
%td{:style => 'border: 1px solid black; text-align: center;'}= @modified_user.project_roles.first.display_rights
- if !@protocol.selected_for_epic.nil?
%td{:style => 'border: 1px solid black; text-align: center;'}= @modified_user.project_roles.first.epic_access == true ? "Yes" : "No"
66 changes: 13 additions & 53 deletions app/views/user_mailer/authorized_user_changed.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,16 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

%p= "Dear #{@send_to.try(:full_name)},"
%p
= t(:mailer)[:header1]
= link_to(t(:mailer)[:link1], root_url)
= t(:mailer)[:header2]
= @protocol.try(:type).try(:humanize)
= t(:mailer)[:header3]
%p
= t(:mailer)[:body1]
%strong= link_to t(:mailer)[:link2], dashboard_root_url(:protocol => 'https', :default_protocol => @protocol.id)
%p
%table{:style => 'border: 1px solid #000; width: 100%; border-collapse: collapse;'}
%tr
%td{:colspan => 2, :style => 'border: 1px solid #000'}
%strong= t(:mailer)[:p_info]
%tr
%td{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:p_id]
%td{:style => 'border: 1px solid #000'}= @protocol.id
%tr
%td{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:title1]
%td{:style => 'border: 1px solid #000'}= @protocol.try(:short_title)
%tr
%td{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:title2]
%td{:style => 'border: 1px solid #000'}= @protocol.try(:title)
%tr
%td{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:s_name]
%td{:style => 'border: 1px solid #000'}= @protocol.try(:sponsor_name)
%tr
%td{:style => 'border: 1px solid #000'}
%strong= @protocol.funding_status == "funded" ? "Funding Source:" : "Potential Funding Source:"
%td{:style => 'border: 1px solid #000'}= @protocol.funding_status == "funded" ? @protocol.try(:funding_source) : @protocol.try(:potential_funding_source)
%p
%table{:style => 'border: 1px solid #000; width: 100%; border-collapse: collapse;'}
%tr
%th{:colspan => 3, :style => 'border: 1px solid #000'}
%strong= t(:mailer)[:user_info]
%tr
%th{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:name]
%th{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:role]
%th{:style => 'border: 1px solid #000'}
%strong= t(:mailer)[:proxy]
- @protocol.project_roles(true).each do |user|
%tr
%td{:style => 'border: 1px solid #000'}= user.identity.try(:full_name)
%td{:style => 'border: 1px solid #000; text-align: center'}= USER_ROLES.detect{|k,v| v == user.role}.try(:first)
%td{:style => 'border: 1px solid #000; text-align: center'}= user.try(:display_rights)
%body
= render "intro"
%br
%br
= render "protocol_info_table"
%br
%br
= render "user_info_table"
%br
%br
%p= t(:issue_contact)
%br
= render "acknowledgments"
15 changes: 8 additions & 7 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1514,22 +1514,23 @@ en:

# Mailer
mailer:
body1: "To VIEW and/or MAKE any changes to this request,"
header1: "Authorized Users have been edited or added in"
header2: "for your"
header3: "listed below."
link1: "SPARC Request"
link2: "please click here."
add_intro: "An Authorized User has been added in"
contact_information: "Contact Information"
delete_intro: "An Authorized User has been deleted in"
epic_access: "Epic Access"
name: "Name:"
p_id: "Protocol ID:"
p_info: "Protocol Information"
proxy: "Proxy Rights"
proxy: "SPARC Proxy Rights"
received: "You have received a new notification on your SPARC Request User Dashboard. Please login @ %{portal_root_url} to retrieve it. Thank you."
role: "Role:"
s_name: "Sponsor Name"
sparc_dashboard_period: "SPARCDashboard."
title1: "Short Title:"
title2: "Project Title:"
update: "An Authorized User has been modified in"
user_info: "User Information:"
user_modification: "User Modification"

# New/editing projects
sr_project:
Expand Down
8 changes: 3 additions & 5 deletions lib/dashboard/associated_user_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class AssociatedUserCreator
attr_reader :protocol_role

def initialize(params)
modified_user = Identity.find(params[:identity_id])
protocol = Protocol.find(params[:protocol_id])
@protocol_role = protocol.project_roles.build(params)

Expand All @@ -14,11 +15,8 @@ def initialize(params)
end
end
@protocol_role.save
if SEND_AUTHORIZED_USER_EMAILS
protocol.emailed_associated_users.each do |project_role|
UserMailer.authorized_user_changed(project_role.identity, protocol).deliver unless project_role.identity.email.blank?
end
end

protocol.email_about_change_in_authorized_user(modified_user, "add")

if USE_EPIC && protocol.selected_for_epic && !QUEUE_EPIC
Notifier.notify_for_epic_user_approval(protocol).deliver
Expand Down
7 changes: 2 additions & 5 deletions lib/dashboard/associated_user_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class AssociatedUserUpdater
def initialize(params)
@protocol_role = ProjectRole.find(params[:id])
protocol = @protocol_role.protocol
modified_user = Identity.find(protocol_role.identity_id)

epic_rights = @protocol_role.epic_rights.to_a # use to_a to eval ActiveRecord::Relation
@protocol_role.assign_attributes(params[:project_role])
Expand All @@ -25,11 +26,7 @@ def initialize(params)
# must come after the use of ActiveModel::Dirty methods above
@protocol_role.save

if SEND_AUTHORIZED_USER_EMAILS
protocol.emailed_associated_users.each do |project_role|
UserMailer.authorized_user_changed(project_role.identity, protocol).deliver unless project_role.identity.email.blank?
end
end
protocol.email_about_change_in_authorized_user(modified_user, "update")

if USE_EPIC && protocol.selected_for_epic && !QUEUE_EPIC
if access_removed
Expand Down
54 changes: 44 additions & 10 deletions spec/controllers/dashboard/associated_users/delete_destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
describe 'DELETE destroy' do
context "when not authorized" do
before :each do
@protocol = build_stubbed(:protocol, selected_for_epic: false)
@protocol_role = findable_stub(ProjectRole) do
instance_double(ProjectRole,
id: 1,
epic_access: false,
protocol: build_stubbed(:protocol, selected_for_epic: false))
protocol: @protocol)
end

allow(@protocol).to receive(:email_about_change_in_authorized_user)

log_in_dashboard_identity(obj: build_stubbed(:identity))

xhr :delete, :destroy, id: @protocol_role.id
Expand All @@ -28,15 +30,25 @@
before :each do
@user = create(:identity)
@protocol = create(:protocol_without_validations, selected_for_epic: false, funding_status: 'funded', funding_source: 'federal')
create(:sub_service_request, status: 'not_draft', organization: create(:organization), service_request: create(:service_request_without_validations, protocol: @protocol))
@protocol_role = create(:project_role, protocol: @protocol, identity: @user, project_rights: 'approve', role: 'primary-pi')

allow(Notifier).to receive(:notify_primary_pi_for_epic_user_removal)
allow(UserMailer).to receive(:authorized_user_changed) do
mailer = double()
expect(mailer).to receive(:deliver)
mailer
end

log_in_dashboard_identity(obj: @user)

xhr :delete, :destroy, id: @protocol_role.id
end

it 'should email authorized user' do
expect(UserMailer).to have_received(:authorized_user_changed)
end

it 'should destroy @protocol_role' do
expect(ProjectRole.count).to eq(0)
end
Expand All @@ -58,29 +70,51 @@
@user = create(:identity)
@protocol = create(:protocol_without_validations, selected_for_epic: false, funding_status: 'funded', funding_source: 'federal')
create(:project_role, protocol: @protocol, identity: @user, project_rights: 'approve', role: 'primary-pi')
@protocol_role = create(:project_role, protocol: @protocol, identity: create(:identity), project_rights: 'approve', role: 'consultant')
@ssr = create(:sub_service_request, status: 'not_draft', organization: create(:organization), service_request: create(:service_request_without_validations, protocol: @protocol))
@user_to_delete = create(:identity)
@protocol_role = create(:project_role, protocol: @protocol, identity: @user_to_delete, project_rights: 'approve', role: 'consultant')

allow(Notifier).to receive(:notify_primary_pi_for_epic_user_removal)

log_in_dashboard_identity(obj: @user)

xhr :delete, :destroy, id: @protocol_role.id
allow(UserMailer).to receive(:authorized_user_changed) do
mailer = double()
expect(mailer).to receive(:deliver)
mailer
end
log_in_dashboard_identity(obj: @user)
end

it 'should destroy @protocol_role' do
xhr :delete, :destroy, id: @protocol_role.id
expect(ProjectRole.count).to eq(1)
end

it 'should not set associated fields' do
xhr :delete, :destroy, id: @protocol_role.id
expect(assigns(:current_user_destroyed)).to eq(false)
expect(assigns(:protocol_type)).to eq(nil)
expect(assigns(:permission_to_edit)).to eq(nil)
expect(assigns(:admin)).to eq(false)
expect(assigns(:return_to_dashboard)).to eq(nil)
end

it { is_expected.to render_template "dashboard/associated_users/destroy" }
it { is_expected.to respond_with :ok }
it 'should email authorized user' do
xhr :delete, :destroy, id: @protocol_role.id
expect(UserMailer).to have_received(:authorized_user_changed).twice
end

it 'should render appropriate template' do
xhr :delete, :destroy, id: @protocol_role.id
expect(response).to render_template "dashboard/associated_users/destroy"
expect(response.status).to eq(200)
end

context "SSRs with status draft" do
it 'should not email user' do
@ssr.update_attribute(:status, 'draft')
xhr :delete, :destroy, id: @protocol_role.id
expect(UserMailer).not_to have_received(:authorized_user_changed)
end
end
end

context 'USE_EPIC == true, QUEUE_EPIC == false, Protocol associated with @protocol_role is selected for epic, and @protocol_role had epic access' do
Expand Down Expand Up @@ -135,4 +169,4 @@
end
end
end
end
end