Skip to content

Commit

Permalink
fixed mailer specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiel86 committed Jun 13, 2017
1 parent 45c0928 commit 96bfa03
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 193 deletions.
130 changes: 69 additions & 61 deletions app/mailers/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ def new_identity_waiting_for_approval identity

def notify_user(project_role, service_request, ssr, approval, user_current, audit_report=nil, individual_ssr=false)

service_list_false = service_request.service_list(false)
service_list_true = service_request.service_list(true)
controller = set_instance_variables(user_current, service_request, service_list_false, service_list_true, service_request.line_items, service_request.protocol)
@protocol = service_request.protocol
@service_request = service_request

xls = controller.render_to_string action: 'show', formats: [:xlsx]
### ATTACHMENTS ###
service_list_false = @service_request.service_list(false)
service_list_true = @service_request.service_list(true)
controller = set_instance_variables(user_current, @service_request, service_list_false, service_list_true, @service_request.line_items, @protocol)

xls = controller.render_to_string action: 'show', formats: [:xlsx]
### END ATTACHMENTS ###

if audit_report.present?
@status = 'request_amendment'
elsif individual_ssr
@status = ssr.status
else
@status = service_request.status
@status = @service_request.status
end

@notes = []
Expand All @@ -56,14 +61,12 @@ def notify_user(project_role, service_request, ssr, approval, user_current, audi
@full_name = @identity.full_name
@audit_report = audit_report

@protocol = service_request.protocol
@service_request = service_request
@service_requester_id = @service_request.sub_service_requests.first.service_requester_id

@portal_link = DASHBOARD_LINK + "/protocols/#{@protocol.id}"
@ssrs_to_be_displayed = individual_ssr ? [ssr] : service_request.sub_service_requests
@ssrs_to_be_displayed = individual_ssr ? [ssr] : @service_request.sub_service_requests

attachments["service_request_#{@service_request.protocol.id}.xlsx"] = xls
attachments["service_request_#{@protocol.id}.xlsx"] = xls

# only send these to the correct person in the production env
email = @identity.email
Expand All @@ -73,30 +76,25 @@ def notify_user(project_role, service_request, ssr, approval, user_current, audi
end

def notify_admin(submission_email_address, user_current, ssr, audit_report=nil, ssr_destroyed=false, individual_ssr=false)
service_request = ssr.service_request
service_list_false = service_request.service_list(false, nil, sub_service_request)
service_list_true = service_request.service_list(true, nil, sub_service_request)
controller = set_instance_variables(user_current, service_request, service_list_false, service_list_true, ssr.line_items, service_request.protocol)

@protocol = ssr.protocol
@service_request = ssr.service_request

### ATTACHMENTS ###
service_list_false = @service_request.service_list(false, nil, ssr)
service_list_true = @service_request.service_list(true, nil, ssr)
controller = set_instance_variables(user_current, @service_request, service_list_false, service_list_true, ssr.line_items, @protocol)
xls = controller.render_to_string action: 'show', formats: [:xlsx]
### END ATTACHMENTS ###

@ssr_deleted = false
@notes = ssr.protocol.notes
@notes = @protocol.notes

if ssr_destroyed
@status = 'ssr_destroyed'
elsif audit_report.present?
@status = 'request_amendment'
elsif individual_ssr
@status = ssr.status
else
@status = ssr.service_request.status
end
@status = status(ssr_destroyed, audit_report.present?, individual_ssr, ssr, @service_request)

@role = 'none'
@full_name = submission_email_address

@protocol = ssr.protocol
@service_request = ssr.service_request
@service_requester_id = @service_request.sub_service_requests.first.service_requester_id
@ssrs_to_be_displayed = [ssr]

Expand All @@ -106,7 +104,7 @@ def notify_admin(submission_email_address, user_current, ssr, audit_report=nil,
@audit_report = audit_report

if !ssr_destroyed
attachments["service_request_#{@service_request.protocol.id}.xlsx"] = xls
attachments["service_request_#{@protocol.id}.xlsx"] = xls
end

email = submission_email_address
Expand All @@ -116,57 +114,54 @@ def notify_admin(submission_email_address, user_current, ssr, audit_report=nil,
end

def notify_service_provider(service_provider, service_request, user_current, ssr, audit_report=nil, ssr_destroyed=false, request_amendment=false, individual_ssr=false)
@protocol = service_request.protocol
@service_request = service_request
@notes = @protocol.notes

attachments = {}
service_list_true = service_request.service_list(true, service_provider)
service_list_false = service_request.service_list(false, service_provider)

# Retrieves the valid line items for service provider to calculate total direct cost in the xls
line_items = []
service_request.sub_service_requests.each do |ssr|
if service_provider.identity.is_service_provider?(ssr)
line_items << ssr.line_items
end
end

controller = set_instance_variables(user_current, service_request, service_list_false, service_list_true, line_items.flatten, service_request.protocol)
xls = controller.render_to_string action: 'show', formats: [:xlsx]
attachments["service_request_#{sub_service_request.service_request.id}.xlsx"] = xls

if ssr.organization.tag_list.include? 'required forms'
request_for_grant_billing_form = RequestGrantBillingPdf.generate_pdf service_request
attachments["request_for_grant_billing_#{sub_service_request.service_request.id}.pdf"] = request_for_grant_billing_form
end

@notes = service_request.protocol.notes

if ssr_destroyed
@status = 'ssr_destroyed'
elsif request_amendment
@status = 'request_amendment'
elsif individual_ssr
@status = ssr.status
else
@status = service_request.status
end
@status = status(ssr_destroyed, request_amendment, individual_ssr, ssr, @service_request)

@role = 'none'
@full_name = service_provider.identity.full_name

@protocol = service_request.protocol
@service_request = service_request
@service_requester_id = @service_request.sub_service_requests.first.service_requester_id

@audit_report = audit_report

@portal_link = DASHBOARD_LINK + "/protocols/#{@protocol.id}"
@portal_text = "Administrators/Service Providers, Click Here"

### ATTACHMENTS ###
attachments_to_add = {}
service_list_true = @service_request.service_list(true, service_provider)
service_list_false = @service_request.service_list(false, service_provider)

# Retrieves the valid line items for service provider to calculate total direct cost in the xls
line_items = []
@service_request.sub_service_requests.each do |sub_service_request|
if service_provider.identity.is_service_provider?(sub_service_request)
line_items << sub_service_request.line_items
end
end

line_items = line_items.flatten
controller = set_instance_variables(user_current, @service_request, service_list_false, service_list_true, line_items, @protocol)

xls = controller.render_to_string action: 'show', formats: [:xlsx]
attachments_to_add["service_request_#{@service_request.id}.xlsx"] = xls
#TODO this is not very multi-institutional
# generate the required forms pdf if it's required

if ssr.organization.tag_list.include? 'required forms'
request_for_grant_billing_form = RequestGrantBillingPdf.generate_pdf @service_request
attachments_to_add["request_for_grant_billing_#{@service_request.id}.pdf"] = request_for_grant_billing_form
end
### END ATTACHMENTS ###

# only display the ssrs that are associated with service_provider
@ssrs_to_be_displayed = [ssr] if service_provider.identity.is_service_provider?(ssr)

if !ssr_destroyed
attachments.each do |file_name, document|
attachments_to_add.each do |file_name, document|
next if document.nil?
attachments["#{file_name}"] = document
end
Expand Down Expand Up @@ -292,4 +287,17 @@ def set_instance_variables(current_user, service_request, service_list_false, se
controller.instance_variable_set(:"@protocol", protocol)
controller
end

def status(ssr_destroyed, request_amendment, individual_ssr, ssr, service_request)
if ssr_destroyed
status = 'ssr_destroyed'
elsif request_amendment
status = 'request_amendment'
elsif individual_ssr
status = ssr.status
else
status = service_request.status
end
status
end
end
6 changes: 6 additions & 0 deletions spec/factories/pricing_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@
trait :charge_master do
charge_master true
end

trait :without_validations do
to_create { |instance| instance.save(validate: false) }
end

factory :pricing_setup_without_validations, traits: [:without_validations]
end
end
4 changes: 2 additions & 2 deletions spec/lib/notifier_logic/ssr_deletion_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).ssr_deletion_emails(@ssr, ssr_destroyed: true, request_amendment: false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, anything, logged_in_user, @ssr, nil, true, false, false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, logged_in_user, @ssr, nil, true, false, false)
end

it 'should notify admin (deletion email)' do
Expand All @@ -97,7 +97,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).ssr_deletion_emails(@ssr, ssr_destroyed: true, request_amendment: false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, anything, logged_in_user, @ssr, nil, true, false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, logged_in_user, @ssr, nil, true, false)
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/lib/notifier_logic/update_ssrs_and_send_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
end
project_role = @sr.protocol.project_roles.first
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, anything, false, logged_in_user, nil, false)
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, false, logged_in_user, nil, false)
end

it 'should notify service providers (initial submission email)' do
Expand All @@ -81,7 +81,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, anything, logged_in_user, @ssr2, nil, false, false, false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, logged_in_user, @ssr2, nil, false, false, false)
end

it 'should notify admin (initial submission email)' do
Expand All @@ -92,7 +92,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_admin).with(@admin_email, anything, logged_in_user, @ssr2, nil, false, false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, logged_in_user, @ssr2, nil, false, false)
end

it 'should send_user_notifications request_amendment=>false' do
Expand Down Expand Up @@ -162,7 +162,7 @@
audit = { :line_items => @deleted_li }
project_role = @sr.protocol.project_roles.first
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, anything, false, logged_in_user, audit, false)
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, false, logged_in_user, audit, false)
end

it 'should NOT notify service providers' do
Expand Down Expand Up @@ -317,7 +317,7 @@
audit = { :line_items => @added_li }
project_role = @sr.protocol.project_roles.first
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, anything, false, logged_in_user, audit, false)
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, false, logged_in_user, audit, false)
end

it 'should notify service providers (initial submission email)' do
Expand All @@ -328,7 +328,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, anything, logged_in_user, @ssr2, nil, false, false, false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, logged_in_user, @ssr2, nil, false, false, false)
end

it 'should notify admin (initial submission email)' do
Expand All @@ -339,7 +339,7 @@
end

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_admin).with(@admin_email, anything, logged_in_user, @ssr2, nil, false, false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, logged_in_user, @ssr2, nil, false, false)
end

it 'should send_user_notifications request_amendment=>true' do
Expand Down Expand Up @@ -402,7 +402,7 @@
audit = { :line_items => @added_li }
project_role = @sr.protocol.project_roles.first
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, anything, false, logged_in_user, audit, false)
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, false, logged_in_user, audit, false)
end

it 'should notify service providers' do
Expand All @@ -413,7 +413,7 @@
end
audit = { :line_items => @added_li, :sub_service_request_id => @ssr2.id }
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, anything, logged_in_user, @ssr2, audit, false, true, false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, logged_in_user, @ssr2, audit, false, true, false)
end

it 'should notify admin' do
Expand All @@ -424,7 +424,7 @@
end
audit = { :line_items => @added_li, :sub_service_request_id => @ssr2.id }
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_admin).with(@admin_email, anything, logged_in_user, @ssr2, audit, false, false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, logged_in_user, @ssr2, audit, false, false)
end

it 'should send_user_notifications request_amendment=>true' do
Expand Down Expand Up @@ -491,7 +491,7 @@
audit = { :line_items => @deleted_li }
project_role = @sr.protocol.project_roles.first
NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, anything, false, logged_in_user, audit, false)
expect(Notifier).to have_received(:notify_user).with(project_role, @sr, nil, false, logged_in_user, audit, false)
end

it 'should notify service providers' do
Expand All @@ -504,7 +504,7 @@
audit = { :line_items => @deleted_li, :sub_service_request_id => @ssr2.id }

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, anything, logged_in_user, @ssr2, audit, false, true, false)
expect(Notifier).to have_received(:notify_service_provider).with(@service_provider, @sr, logged_in_user, @ssr2, audit, false, true, false)
end

it 'should notify admin' do
Expand All @@ -516,7 +516,7 @@
audit = { :line_items => @deleted_li, :sub_service_request_id => @ssr2.id }

NotifierLogic.new(@sr, nil, logged_in_user).update_ssrs_and_send_emails
expect(Notifier).to have_received(:notify_admin).with(@admin_email, anything, logged_in_user, @ssr2, audit, false, false)
expect(Notifier).to have_received(:notify_admin).with(@admin_email, logged_in_user, @ssr2, audit, false, false)
end

it 'should send_user_notifications request_amendment=>true' do
Expand Down

0 comments on commit 96bfa03

Please sign in to comment.