Skip to content

Commit

Permalink
Merge pull request #583 from sparc-request/saw-dashboard-history-add-…
Browse files Browse the repository at this point in the history
…changed-by-column

SAW - dashboard history add changed by column
  • Loading branch information
amcates committed Jul 27, 2016
2 parents 50928c3 + c036f5e commit 56e5a5d
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def show

def update
if @sub_service_request.update_attributes(params[:sub_service_request])
@sub_service_request.update_past_status(current_user)
flash[:success] = 'Request Updated!'
else
@errors = @sub_service_request.errors
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/service_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ def obtain_research_pricing
@service_request.previous_submitted_at = @service_request.submitted_at

to_notify = []

if @sub_service_request
if @sub_service_request.status != 'get_a_cost_estimate'
to_notify << @sub_service_request.id
end

@sub_service_request.update_attribute(:status, 'get_a_cost_estimate')
@sub_service_request.update_past_status(current_user)
else
to_notify = update_service_request_status(@service_request, 'get_a_cost_estimate')
end
Expand All @@ -268,6 +268,7 @@ def confirmation
end

@sub_service_request.update_attributes(status: 'submitted', nursing_nutrition_approved: false, lab_approved: false, imaging_approved: false, committee_approved: false)
@sub_service_request.update_past_status(current_user)
else
to_notify = update_service_request_status(@service_request, 'submitted')
@service_request.update_arm_minimum_counts
Expand Down Expand Up @@ -332,6 +333,7 @@ def approve_changes
def save_and_exit
if @sub_service_request #if editing a sub service request, update status
@sub_service_request.update_attribute(:status, 'draft')
@sub_service_request.update_past_status(current_user)
else
update_service_request_status(@service_request, 'draft')
@service_request.ensure_ssr_ids
Expand Down Expand Up @@ -385,7 +387,9 @@ def add_service
if @service_request.status == 'first_draft'
ssr.update_attribute :status, 'first_draft'
elsif ssr.status.nil? || (ssr.can_be_edited? && ssr_has_changed?(@service_request, ssr) && (ssr.status != 'complete'))
previous_status = ssr.status
ssr.update_attribute :status, 'draft'
ssr.update_past_status(current_user) unless previous_status.nil?
end
end

Expand All @@ -410,7 +414,10 @@ def remove_service

@line_items.where(service_id: service.id).each do |li|
ssr = li.sub_service_request
ssr.update_attribute :status, 'draft' if ssr.can_be_edited? && ssr.status != 'first_draft'
if ssr.can_be_edited? && ssr.status != 'first_draft'
ssr.update_attribute(:status, 'draft')
ssr.update_past_status(current_user)
end
li.destroy
end

Expand Down Expand Up @@ -655,6 +662,7 @@ def update_service_request_status(service_request, status)
service_request.update_attribute(:submitted_at, Time.now)
end
service_request.update_status(status)
service_request.sub_service_requests.each {|ssr| ssr.update_past_status(current_user)}
end

def authorize_protocol_edit_request
Expand Down
3 changes: 3 additions & 0 deletions app/models/past_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class PastStatus < ActiveRecord::Base

belongs_to :sub_service_request

belongs_to :changer, class_name: 'Identity', foreign_key: 'changed_by_id'

attr_accessible :sub_service_request_id
attr_accessible :status
attr_accessible :date
attr_accessible :changed_by_id

attr_accessor :changed_to

Expand Down
6 changes: 3 additions & 3 deletions app/models/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SubServiceRequest < ActiveRecord::Base

audited

after_save :update_past_status, :update_org_tree
after_save :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"
Expand Down Expand Up @@ -379,10 +379,10 @@ def candidate_statuses
# Callback which gets called after the ssr is saved to ensure that the
# past status is properly updated. It should not normally be
# necessarily to call this method.
def update_past_status
def update_past_status identity
old_status = self.past_statuses.last
if @prev_status and (not old_status or old_status.status != @prev_status)
self.past_statuses.create(status: @prev_status, date: Time.now)
self.past_statuses.create(status: @prev_status, date: Time.now, changed_by_id: identity.id)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
= t(:dashboard)[:sub_service_requests][:tabs][:history][:status][:from]
%th{ data: { class: 'changed_to', align: "left", sortable: "true", field: "changed_to" } }
= t(:dashboard)[:sub_service_requests][:tabs][:history][:status][:to]
%th{ data: { class: 'changed_by', align: "left", sortable: "true", field: "changed_by" } }
= t(:dashboard)[:sub_service_requests][:tabs][:history][:status][:by]
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ json.(@past_statuses) do |status|
json.created_at format_datetime(status.created_at)
json.changed_from AVAILABLE_STATUSES[status.status]
json.changed_to AVAILABLE_STATUSES[status.changed_to]
json.changed_by status.changer.try(:full_name)
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ en:
date: "Date Changed"
from: "Status Changed From"
to: "Status Changed To"
by: "Changed by"
approval:
header: "Approval History"
date: "Date Approved"
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20160712155630_add_changed_by_to_past_statuses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddChangedByToPastStatuses < ActiveRecord::Migration
def change
add_column :past_statuses, :changed_by_id, :integer
add_index :past_statuses, :changed_by_id
end
end
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,10 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.integer "changed_by", limit: 4
end

add_index "past_statuses", ["changed_by"], name: "index_past_statuses_on_changed_by", using: :btree
add_index "past_statuses", ["sub_service_request_id"], name: "index_past_statuses_on_sub_service_request_id", using: :btree

create_table "past_subsidies", force: :cascade do |t|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@
expect(ssr2.status).to eq 'submitted'
end

it 'should create a past status for each sub service request' do
service_request.sub_service_requests.each { |ssr| ssr.destroy }

ssr1 = create(:sub_service_request,
service_request_id: service_request.id,
status: 'draft',
organization_id: provider.id)
ssr2 = create(:sub_service_request,
service_request_id: service_request.id,
status: 'draft',
organization_id: core.id)

xhr :get, :confirmation, id: service_request.id

ps1 = PastStatus.find_by(sub_service_request_id: ssr1.id)
ps2 = PastStatus.find_by(sub_service_request_id: ssr2.id)

expect(ps1.status).to eq('draft')
expect(ps2.status).to eq('draft')
end

it 'should send an email if services are set to send to epic' do
stub_const("QUEUE_EPIC", false)
stub_const("USE_EPIC", true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails_helper'

RSpec.describe ServiceRequestsController do

describe 'GET obtain_research_pricing'
stub_controller

let_there_be_lane
let_there_be_j

before :each do
session[:identity_id] = jug2.id
@protocol = create(:study_without_validations,
primary_pi: jug2)
@organization = create(:provider)
@service_request = create(:service_request_without_validations,
status: 'draft',
protocol: @protocol)
@sub_service_request = create(:sub_service_request,
service_request_id: @service_request.id,
status: 'draft',
organization_id: @organization.id)
end

context 'Editing a sub_service_request' do
it 'Should create a past_status for the sub_service_request' do
xhr :get, :obtain_research_pricing,
id: @service_request.id,
sub_service_request_id: @sub_service_request.id

ps = PastStatus.find_by(sub_service_request_id: @sub_service_request.id)

expect(ps.status).to eq('draft')
end
end

context 'Editing a service_request' do
it 'Should create a past_status for the sub_service_request' do
xhr :get, :obtain_research_pricing,
id: @service_request.id

ps = PastStatus.find_by(sub_service_request_id: @sub_service_request.id)

expect(ps.status).to eq('draft')
end
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@
expect(ssr2.status).to eq 'draft'
end

it 'should create a past status for each SubServiceRequest' do
service_request.sub_service_requests.each(&:destroy)

session[:identity_id] = jug2.id

ssr1 = create(:sub_service_request,
service_request_id: service_request.id,
status: 'first_draft',
organization_id: provider.id)
ssr2 = create(:sub_service_request,
service_request_id: service_request.id,
status: 'first_draft',
organization_id: core.id)

xhr :get, :save_and_exit, id: service_request.id

ps1 = PastStatus.find_by(sub_service_request_id: ssr1.id)
ps2 = PastStatus.find_by(sub_service_request_id: ssr2.id)

expect(ps1.status).to eq('first_draft')
expect(ps2.status).to eq('first_draft')
end

it 'should set ssr_id correctly when next_ssr_id > 9999' do
service_request.protocol.update_attribute(:next_ssr_id, 10_042)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,31 @@
expect(service_request.line_items[-2].sub_service_request).to eq core_ssr
expect(service_request.line_items[-1].sub_service_request).to eq core2_ssr
end

it 'should create a past status for the SubServiceRequest' do
protocol = create(:study_without_validations,
primary_pi: jug2)

service_request = create(:service_request_without_validations,
status: 'submitted',
protocol: protocol)

ssr1 = create(:sub_service_request,
service_request_id: service_request.id,
status: 'submitted',
organization_id: core.id)
service = create(:service,
organization_id: core.id)

post :add_service, {
id: service_request.id,
service_id: service.id,
format: :js
}.with_indifferent_access

ps1 = PastStatus.find_by(sub_service_request_id: ssr1.id)

expect(ps1.status).to eq('submitted')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,45 @@
expect(service_request.sub_service_requests).not_to include(ssr2)
end

it 'should create a past status for the SubServiceRequest that still has a Service in the ServiceRequest' do
protocol = create(:study_without_validations,
primary_pi: jug2)

service_request = create(:service_request_without_validations,
status: 'submitted',
protocol: protocol)

ssr = create(:sub_service_request,
service_request_id: service_request.id,
status: 'submitted',
organization_id: core.id)

service1 = create(:service,
organization_id: core.id)
service2 = create(:service,
organization_id: core.id)

line_item1 = create(:line_item_without_validations,
service_request: service_request,
sub_service_request: ssr,
service: service1)
line_item2 = create(:line_item_without_validations,
service_request: service_request,
sub_service_request: ssr,
service: service2)

post :remove_service, {
id: service_request.id,
service_id: service1.id,
line_item_id: line_item1.id,
format: :js
}.with_indifferent_access

ps1 = PastStatus.find_by(sub_service_request_id: ssr.id)

expect(ps1.status).to eq('submitted')
end

it 'should set @page' do
allow(controller.request).to receive(:referrer).and_return('http://example.com/foo/bar')

Expand Down
1 change: 1 addition & 0 deletions spec/factories/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
factory :line_item_with_service, traits: [:with_service, :with_service_request]
factory :one_time_fee_line_item, traits: [:one_time_fee]
factory :per_patient_per_visit_line_item, traits: [:per_patient_per_visit]
factory :line_item_without_validations, traits: [:without_validations]
end
end

0 comments on commit 56e5a5d

Please sign in to comment.