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

KG - Display Service Requester in Notifications Dropdown (1.7.5) #554

Merged
merged 12 commits into from
Jul 13, 2016
5 changes: 2 additions & 3 deletions app/api/v1/entities/full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,13 @@ class ServiceRequestFull < ServiceRequestShallow

expose :protocol_id,
:status,
:service_requester_id,
:approved,
:subject_count

with_options(format_with: :iso_timestamp) do
expose :consult_arranged_date
expose :pppv_complete_date
expose :pppv_in_process_date
expose :requester_contacted_date
expose :submitted_at
end
end
Expand All @@ -195,7 +193,8 @@ class SubServiceRequestFull < SubServiceRequestShallow
:in_work_fulfillment,
:routing,
:org_tree_display,
:grand_total
:grand_total,
:service_requester_id

expose :formatted_status, as: :status

Expand Down
9 changes: 4 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def create_new_service_request(from_portal=false)
"PROTOCOL#{params[:protocol_id]}"
else # otherwise associate the service request with this protocol
@service_request.protocol_id = params[:protocol_id]
@service_request.service_requester_id = current_user.id
@service_request.sub_service_requests.update_all(service_requester_id: current_user.id)
end
end

Expand All @@ -265,20 +265,19 @@ def authorize_identity

# we have a current user
if current_user
if @sub_service_request.nil? and current_user.can_edit_service_request?(@service_request)
if @sub_service_request.nil? and (@service_request.status == 'first_draft' || current_user.can_edit_service_request?(@service_request))
return true
elsif @sub_service_request and current_user.can_edit_sub_service_request?(@sub_service_request)
return true
end

# the service request is in first draft and has yet to be submitted (catalog page only)
elsif @service_request.status == 'first_draft' and @service_request.service_requester_id.nil?
elsif @service_request.status == 'first_draft'
return true
elsif !@service_request.status.nil? # this is a previous service request so we should attempt to sign in
authenticate_identity!
return true
end

if @sub_service_request.nil?
authorization_error "The service request you are trying to access is not editable.",
"SR#{session[:service_request_id]}"
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/service_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def catalog

def protocol
cookies.delete :current_step
@service_request.update_attribute(:service_requester_id, current_user.id) if @service_request.service_requester_id.nil?

@service_request.sub_service_requests.where(service_requester_id: nil).update_all(service_requester_id: current_user.id)

if session[:saved_protocol_id]
@service_request.protocol = Protocol.find session[:saved_protocol_id]
Expand Down Expand Up @@ -659,7 +660,7 @@ def authorize_protocol_edit_request
authorized = if @sub_service_request
current_user.can_edit_sub_service_request?(@sub_service_request)
else
current_user.can_edit_service_request?(@service_request)
@service_request.status == 'first_draft' || current_user.can_edit_service_request?(@service_request)
end

protocol = @sub_service_request ? @sub_service_request.service_request.protocol : @service_request.protocol
Expand Down
3 changes: 1 addition & 2 deletions app/models/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Identity < ActiveRecord::Base
has_many :catalog_managers, dependent: :destroy
has_many :clinical_providers, dependent: :destroy
has_many :protocol_service_requests, through: :protocols, source: :service_requests
has_many :requested_service_requests, class_name: 'ServiceRequest', foreign_key: 'service_requester_id'
has_many :catalog_manager_rights, class_name: 'CatalogManager'
has_many :service_providers, dependent: :destroy
has_many :received_toast_messages, class_name: 'ToastMessage', foreign_key: 'to', dependent: :destroy
Expand Down Expand Up @@ -232,7 +231,7 @@ def send_admin_mail

# Only users with request or approve rights can edit.
def can_edit_service_request? sr
(sr.service_requester_id == self.id or sr.service_requester_id.nil?) || has_correct_project_role?(sr)
has_correct_project_role?(sr)
end

# If a user has request or approve rights AND the request is editable, then the user can edit.
Expand Down
3 changes: 0 additions & 3 deletions app/models/service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ServiceRequest < ActiveRecord::Base

audited

belongs_to :service_requester, :class_name => "Identity", :foreign_key => "service_requester_id"
belongs_to :protocol
has_many :sub_service_requests, :dependent => :destroy
has_many :line_items, -> { includes(:service) }, :dependent => :destroy
Expand Down Expand Up @@ -90,13 +89,11 @@ class ServiceRequest < ActiveRecord::Base

attr_accessible :protocol_id
attr_accessible :status
attr_accessible :service_requester_id
attr_accessible :notes
attr_accessible :approved
attr_accessible :consult_arranged_date
attr_accessible :pppv_complete_date
attr_accessible :pppv_in_process_date
attr_accessible :requester_contacted_date
attr_accessible :submitted_at
attr_accessible :line_items_attributes
attr_accessible :sub_service_requests_attributes
Expand Down
3 changes: 3 additions & 0 deletions app/models/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SubServiceRequest < ActiveRecord::Base

after_save :update_past_status, :update_org_tree

belongs_to :service_requester, class_name: "Identity", foreign_key: "service_requester_id"
belongs_to :owner, :class_name => 'Identity', :foreign_key => "owner_id"
belongs_to :service_request
belongs_to :organization
Expand Down Expand Up @@ -65,6 +66,8 @@ class SubServiceRequest < ActiveRecord::Base
attr_accessible :in_work_fulfillment
attr_accessible :routing
attr_accessible :documents
attr_accessible :service_requester_id
attr_accessible :requester_contacted_date

accepts_nested_attributes_for :line_items, allow_destroy: true
accepts_nested_attributes_for :payments, allow_destroy: true
Expand Down
2 changes: 0 additions & 2 deletions app/reports/admin_time_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def column_attrs

attrs["Service Request Owner"] = "owner.try(:full_name)"

attrs["Requester Contacted Date"] = "service_request.try(:requester_contacted_date).try(:strftime, \"%D\")"

attrs["Consult Arranged Date"] = "service_request.try(:consult_arranged_date).try(:strftime, \"%D\")"

if params[:service_id]
Expand Down
3 changes: 2 additions & 1 deletion app/views/dashboard/notifications/_dropdown.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
%select.selectpicker.new-notification{ title: t(:dashboard)[:notifications][:table][:send], data: { style: "btn-success new_notification_button", live_search: 'true', sub_service_request_id: sub_service_request.ssr_id } }
%optgroup{ label: t(:dashboard)[:notifications][:table][:authorized_users]}
- sub_service_request.service_request.protocol.project_roles.each do |pr|
%option{ data: { identity_id: pr.identity_id, sub_service_request_id: sub_service_request.id, is_service_provider: 'false', current_user_id: user.id } } #{display_user_role(pr)}: #{pr.identity.full_name}
%option{ data: { identity_id: pr.identity_id, sub_service_request_id: sub_service_request.id, is_service_provider: 'false', current_user_id: user.id } }
= "#{display_user_role(pr)}: #{pr.identity.full_name} #{sub_service_request.service_requester_id == pr.identity_id ? '(Requester)' : ''}"
%optgroup{ label: t(:dashboard)[:notifications][:table][:service_providers]}
- sub_service_request.organization.service_providers.includes(:identity).sort_by { |sp| sp.identity.last_name }.each do |sp|
%option{ data: { identity_id: sp.identity.id, sub_service_request_id: sub_service_request.id, is_service_provider: 'true', current_user_id: user.id } }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ReassociateRequesterFromServiceRequestToSubServiceRequest < ActiveRecord::Migration
def change

add_column :sub_service_requests, :service_requester_id, :integer
add_index :sub_service_requests, :service_requester_id

ServiceRequest.where.not(service_requester_id: nil).each do |sr|
sr.sub_service_requests.update_all(service_requester_id: sr.service_requester_id)
end

remove_column :service_requests, :service_requester_id
remove_column :service_requests, :requester_contacted_date
end
end
17 changes: 9 additions & 8 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
t.datetime "document_updated_at"
t.string "doc_type_other", limit: 255
t.integer "protocol_id", limit: 4
t.integer "service_request_id", limit: 4
end

add_index "documents", ["protocol_id"], name: "index_documents_on_protocol_id", using: :btree
Expand Down Expand Up @@ -693,6 +694,7 @@
t.boolean "selected_for_epic"
t.boolean "archived", default: false
t.integer "study_type_question_group_id", limit: 4
t.integer "requester_id", limit: 4
end

add_index "protocols", ["next_ssr_id"], name: "index_protocols_on_next_ssr_id", using: :btree
Expand Down Expand Up @@ -856,24 +858,21 @@
add_index "service_relations", ["service_id"], name: "index_service_relations_on_service_id", using: :btree

create_table "service_requests", force: :cascade do |t|
t.integer "protocol_id", limit: 4
t.string "status", limit: 255
t.integer "service_requester_id", limit: 4
t.integer "protocol_id", limit: 4
t.string "status", limit: 255
t.boolean "approved"
t.integer "subject_count", limit: 4
t.integer "subject_count", limit: 4
t.datetime "consult_arranged_date"
t.datetime "pppv_complete_date"
t.datetime "pppv_in_process_date"
t.datetime "requester_contacted_date"
t.datetime "submitted_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.date "original_submitted_date"
end

add_index "service_requests", ["protocol_id"], name: "index_service_requests_on_protocol_id", using: :btree
add_index "service_requests", ["service_requester_id"], name: "index_service_requests_on_service_requester_id", using: :btree
add_index "service_requests", ["status"], name: "index_service_requests_on_status", using: :btree

create_table "services", force: :cascade do |t|
Expand Down Expand Up @@ -964,11 +963,13 @@
t.boolean "in_work_fulfillment", default: false
t.string "routing", limit: 255
t.text "org_tree_display", limit: 65535
t.integer "service_requester_id", limit: 4
end

add_index "sub_service_requests", ["organization_id"], name: "index_sub_service_requests_on_organization_id", using: :btree
add_index "sub_service_requests", ["owner_id"], name: "index_sub_service_requests_on_owner_id", using: :btree
add_index "sub_service_requests", ["service_request_id"], name: "index_sub_service_requests_on_service_request_id", using: :btree
add_index "sub_service_requests", ["service_requester_id"], name: "index_sub_service_requests_on_service_requester_id", using: :btree
add_index "sub_service_requests", ["ssr_id"], name: "index_sub_service_requests_on_ssr_id", using: :btree
add_index "sub_service_requests", ["status"], name: "index_sub_service_requests_on_status", using: :btree

Expand Down
16 changes: 9 additions & 7 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def navigate

context '@sub_service_request nil and Identity can edit @service_request' do
it 'should authorize Identity' do
controller.instance_variable_set(:@service_request, :service_request)
sr = build(:service_request)
controller.instance_variable_set(:@service_request, sr)
allow(jug2).to receive(:can_edit_service_request?)
.with(:service_request)
.with(sr)
.and_return(true)
expect(controller).to_not receive(:authorization_error)
controller.send(:authorize_identity)
Expand All @@ -83,9 +84,10 @@ def navigate

context '@sub_service_request nil and Identity cannot edit @service_request' do
it 'should not authorize Identity' do
controller.instance_variable_set(:@service_request, :service_request)
sr = build(:service_request)
controller.instance_variable_set(:@service_request, sr)
allow(jug2).to receive(:can_edit_service_request?)
.with(:service_request)
.with(sr)
.and_return(false)
expect(controller).to receive(:authorization_error)
controller.send(:authorize_identity)
Expand All @@ -111,7 +113,7 @@ def navigate

context 'ServiceRequest in first_draft and not submitted yet' do
it 'should authorize Identity' do
service_request = instance_double('ServiceRequest', status: 'first_draft', service_requester_id: nil)
service_request = instance_double('ServiceRequest', status: 'first_draft')
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to_not receive(:authorization_error)
controller.send(:authorize_identity)
Expand All @@ -128,9 +130,9 @@ def navigate
end
end

context 'ServiceRequest has non-nil service_requester_id and status' do
context 'ServiceRequest and status' do
it 'should authorize Identity' do
service_request = instance_double('ServiceRequest', status: 'draft', service_requester_id: 1)
service_request = instance_double('ServiceRequest', status: 'draft')
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to_not receive(:authorization_error)
expect(controller).to receive(:authenticate_identity!)
Expand Down
1 change: 0 additions & 1 deletion spec/factories/service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
factory :service_request do
protocol_id { Random.rand(10000) }
status { Faker::Lorem.sentence(3) }
service_requester_id { Random.rand(1000) }
approved { false }

trait :without_validations do
Expand Down
1 change: 1 addition & 0 deletions spec/factories/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FactoryGirl.define do
factory :sub_service_request do
owner_id { Random.rand(1000) }
service_requester_id { Random.rand(1000) }

trait :without_validations do
to_create { |instance| instance.save(validate: false) }
Expand Down
12 changes: 6 additions & 6 deletions spec/features/dashboard/protocols/index/dashboard_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def visit_protocols_index_page
describe 'protocol has ssr' do
scenario 'user should see requests button and archive button' do
protocol = create(:study_without_validations, primary_pi: user)
sr = create(:service_request_without_validations, protocol: protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))
page = visit_protocols_index_page

Expand All @@ -79,7 +79,7 @@ def visit_protocols_index_page
end
describe 'protocol has ssr' do
scenario 'user should see requests button and archive button' do
sr = create(:service_request_without_validations, protocol: @protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: @protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))
page = visit_protocols_index_page

Expand All @@ -105,7 +105,7 @@ def visit_protocols_index_page
create(:super_user, identity_id: user.id)
protocol = create(:archived_project_without_validations, primary_pi: user)
create(:project_role, identity: user, role: "very-important", project_rights: "to-party", protocol: protocol)
sr = create(:service_request_without_validations, protocol: protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))

page = visit_protocols_index_page
Expand All @@ -126,7 +126,7 @@ def visit_protocols_index_page
create(:super_user, identity_id: user.id)
protocol = create(:unarchived_project_without_validations, primary_pi: user)
create(:project_role, identity: user, role: "very-important", project_rights: "to-party", protocol: protocol)
sr = create(:service_request_without_validations, protocol: protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))

page = visit_protocols_index_page
Expand All @@ -142,7 +142,7 @@ def visit_protocols_index_page
create(:super_user, identity_id: user.id)
protocol = create(:archived_study_without_validations, primary_pi: user)
create(:project_role, identity: user, role: "very-important", project_rights: "to-party", protocol: protocol)
sr = create(:service_request_without_validations, protocol: protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))

page = visit_protocols_index_page
Expand All @@ -162,7 +162,7 @@ def visit_protocols_index_page
create(:super_user, identity_id: user.id)
protocol = create(:unarchived_study_without_validations, primary_pi: user)
create(:project_role, identity: user, role: "very-important", project_rights: "to-party", protocol: protocol)
sr = create(:service_request_without_validations, protocol: protocol, service_requester: user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request, service_request: sr, organization: create(:organization))

page = visit_protocols_index_page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
fake_login_for_each_test("johnd")

let!(:protocol) { create(:unarchived_study_without_validations, primary_pi: user) }
let!(:sr) { create(:service_request_without_validations, protocol: protocol, service_requester: user) }
let!(:sr) { create(:service_request_without_validations, protocol: protocol) }
let!(:ssr) { create(:sub_service_request, service_request: sr, organization: create(:organization), status: 'draft') }

def index_page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def visit_protocols_index_page
let!(:service_request) do
create(:service_request_without_validations,
protocol: protocol,
service_requester: jug2,
status: 'draft')
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def go_to_show_protocol(protocol_id)
it 'should create a new Note and display it in modal' do
service_request = create(:service_request_without_validations,
protocol: protocol,
service_requester: user,
status: 'draft')
create(:sub_service_request,
service_request: service_request,
Expand All @@ -53,11 +52,9 @@ def go_to_show_protocol(protocol_id)

describe 'displayed SubServiceRequest' do
let!(:protocol) { create(:unarchived_study_without_validations, primary_pi: user) }
let!(:service_requester) { create(:identity, first_name: 'Some', last_name: 'Guy') }
let!(:service_request) do
create(:service_request_without_validations,
protocol: protocol,
service_requester: service_requester,
status: 'draft')
end
let!(:organization) do
Expand Down Expand Up @@ -129,11 +126,9 @@ def go_to_show_protocol(protocol_id)

describe 'buttons' do
let!(:protocol) { create(:unarchived_study_without_validations, primary_pi: user) }
let!(:service_requester) { create(:identity, first_name: 'Some', last_name: 'Guy') }
let!(:service_request) do
create(:service_request_without_validations,
protocol: protocol,
service_requester: service_requester,
status: 'draft')
end
let!(:organization) do
Expand Down