Skip to content

Commit

Permalink
MHV-64025-list-count-per-filter (#19529)
Browse files Browse the repository at this point in the history
* [mhv 64025] added filter counts to meta data object.

* [mhv 64025] added rest of meta object.

* fixes rubocop issues

* added tests

* refactor for rubocop suggestions

* moved fns to helper file

* fixed rubocop

* fixed rubocop

* addresses renewal count

* rubocop fixes

* fixes rubocop

* fixes tests

---------

Co-authored-by: robertbylight <[email protected]>
  • Loading branch information
agravell047 and robertbylight authored Nov 21, 2024
1 parent f7e4f6d commit faa1814
Show file tree
Hide file tree
Showing 12 changed files with 3,598 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/models/prescription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Prescription < Common::Base
attribute :facility_api_name, String
attribute :ordered_date, Common::UTCTime, sortable: { order: 'DESC' }
attribute :quantity, Integer
attribute :expiration_date, Common::UTCTime
attribute :expiration_date, Common::UTCTime, filterable: %w[eq lteq gteq]
attribute :prescription_number, String
attribute :sig, String
attribute :prescription_name, String, sortable: { order: 'ASC', default: true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ class PrescriptionsController < RxController
# (ie: ?sort[]=refill_status&sort[]=-prescription_id)
def index
resource = collection_resource
resource = params[:filter].present? ? resource.find_by(filter_params) : resource
resource.data = filter_non_va_meds(resource.data)
filter_count = set_filter_metadata(resource.data)
renewal_params = 'Active,Expired'
resource = if params[:filter].present?
if filter_params[:disp_status]&.[](:eq) == renewal_params
filter_renewals(resource)
else
resource.find_by(filter_params)
end
else
resource
end
resource = params[:sort].is_a?(Array) ? sort_by(resource, params[:sort]) : resource.sort(params[:sort])
is_using_pagination = params[:page].present? || params[:per_page].present?
resource.data = params[:include_image].present? ? fetch_and_include_images(resource.data) : resource.data
resource = is_using_pagination ? resource.paginate(**pagination_params) : resource
options = { meta: resource.metadata.merge(filter_count) }

options = { meta: resource.metadata }
options[:links] = pagination_links(resource) if is_using_pagination
render json: MyHealth::V1::PrescriptionDetailsSerializer.new(resource.data, options)
end
Expand All @@ -40,6 +50,18 @@ def refill
head :no_content
end

def filter_renewals(resource)
resource.data = resource.data.select(&method(:renewable))
resource.metadata = resource.metadata.merge({
'filter' => {
'disp_status' => {
'eq' => 'Active,Expired'
}
}
})
resource
end

def refill_prescriptions
ids = params[:ids]
successful_ids = []
Expand Down Expand Up @@ -138,6 +160,36 @@ def collection_resource
client.get_active_rxs_with_details
end
end

def set_filter_metadata(list)
{
filter_count: {
all_medications: list.length,
active: count_active_medications(list),
recently_requested: count_recently_requested_medications(list),
renewal: list.select(&method(:renewable)).length,
non_active: count_non_active_medications(list)
}
}
end

def count_active_medications(list)
active_statuses = [
'Active', 'Active: Refill in Process', 'Active: Non-VA', 'Active: On hold',
'Active: Parked', 'Active: Submitted'
]
list.select { |rx| active_statuses.include?(rx.disp_status) }.length
end

def count_recently_requested_medications(list)
recently_requested_statuses = ['Active: Refill in Process', 'Active: Submitted']
list.select { |rx| recently_requested_statuses.include?(rx.disp_status) }.length
end

def count_non_active_medications(list)
non_active_statuses = %w[Discontinued Expired Transferred Unknown]
list.select { |rx| non_active_statuses.include?(rx.disp_status) }.length
end
end
end
end
45 changes: 22 additions & 23 deletions modules/my_health/app/helpers/my_health/prescription_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def filter_non_va_meds(data)
end

def sort_by(resource, sort_params)
sort_orders = get_sort_order(sort_params)
sort_orders = sort_params.map { |param| param.to_s.start_with?('-') }
resource.data = resource.data.sort do |a, b|
comparison = 0
sort_params.each_with_index do |field, index|
Expand All @@ -39,12 +39,6 @@ def sort_by(resource, sort_params)
resource
end

def get_sort_order(fields)
fields.map do |field|
field.to_s.start_with?('-')
end
end

def compare_fields(field_a, field_b, is_descending)
if field_a > field_b
is_descending ? -1 : 1
Expand Down Expand Up @@ -88,36 +82,41 @@ def get_field_data(field, data, is_descending)

def filter_data_by_refill_and_renew(data)
data.select do |item|
disp_status = item[:disp_status]
refill_history_expired_date = item[:rx_rf_records]&.[](0)&.[](1)&.[](0)&.[](:expiration_date)&.to_date
expired_date = refill_history_expired_date || item[:expiration_date]&.to_date

next true if item[:is_refillable]

if item[:refill_remaining].to_i.zero?
next true if disp_status.downcase == 'active'
next true if disp_status.downcase == 'active: parked' && !item[:rx_rf_records].all?(&:empty?)
end
if disp_status == 'Expired' && expired_date.present? && valid_date_within_cut_off_date?(expired_date)
next true
end
next true if renewable(item)

false
end
end

def renewable(item)
disp_status = item[:disp_status]
refill_history_expired_date = item[:rx_rf_records]&.[](0)&.[](1)&.[](0)&.[](:expiration_date)&.to_date
expired_date = refill_history_expired_date || item[:expiration_date]&.to_date
not_refillable = ['false'].include?(item.is_refillable.to_s)
if item[:refill_remaining].to_i.zero? && not_refillable
return true if disp_status&.downcase == 'active'
return true if disp_status&.downcase == 'active: parked' && !item[:rx_rf_records].all?(&:empty?)
end
if disp_status == 'Expired' && expired_date.present? && within_cut_off_date?(expired_date) && not_refillable
return true
end

false
end

private

def valid_date_within_cut_off_date?(date)
cut_off_date = Time.zone.today - 120.days
def within_cut_off_date?(date)
zero_date = Date.new(0, 1, 1)
date.present? && date != zero_date && date >= cut_off_date
date.present? && date != zero_date && date >= Time.zone.today - 120.days
end

module_function :collection_resource,
:filter_data_by_refill_and_renew,
:filter_non_va_meds,
:sort_by
:sort_by,
:renewable
end
end
end
18 changes: 18 additions & 0 deletions modules/my_health/spec/requests/my_health/v1/prescriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@
end
end

it 'responds to GET #index with filter metadata for specific disp_status' do
VCR.use_cassette('rx_client/prescriptions/index_with_disp_status_filter') do
get '/my_health/v1/prescriptions?filter[[disp_status][eq]]=Active,Expired'
end
expect(response).to be_successful
json_response = JSON.parse(response.body)
expect(json_response['meta']['filter_count']).to include(
'all_medications', 'active', 'recently_requested', 'renewal', 'non_active'
)
expect(json_response['meta']['filter_count']['all_medications']).to be >= 0
expect(json_response['meta']['filter_count']['active']).to be >= 0
expect(json_response['meta']['filter_count']['recently_requested']).to be >= 0
expect(json_response['meta']['filter_count']['renewal']).to be >= 0
expect(json_response['meta']['filter_count']['non_active']).to be >= 0
disp_statuses = json_response['data'].map { |prescription| prescription['attributes']['disp_status'] }
expect(disp_statuses).to all(be_in(%w[Active Expired]))
end

it 'responds to GET #index with pagination parameters when camel-inflected' do
VCR.use_cassette('rx_client/prescriptions/gets_a_paginated_list_of_prescriptions') do
get '/my_health/v1/prescriptions?page=2&per_page=20', headers: inflection_header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
},
"filter": {
"type": "object"
},
"filter_count": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"all_medications": { "type":"integer"},
"recently_requested": { "type":"integer"},
"non_active": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"total_pages": { "type": "integer" },
"total_entries": { "type": "integer" }
}
},
"filter_count": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"all_medications": { "type":"integer"},
"recently_requested": { "type":"integer"},
"non_active": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"failed_station_list": { "type": "string" },
"sort": {
"type": "object"
},
"filter_count": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"all_medications": { "type":"integer"},
"recently_requested": { "type":"integer"},
"non_active": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"total_pages": { "type": "integer" },
"total_entries": { "type": "integer" }
}
},
"filter_count": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"all_medications": { "type":"integer"},
"recently_requested": { "type":"integer"},
"non_active": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
},
"filter": {
"type": "object"
},
"filterCount": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"allMedications": { "type":"integer"},
"recentlyRequested": { "type":"integer"},
"nonActive": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"failedStationList": { "type": "string" },
"sort": {
"type": "object"
},
"filterCount": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"allMedications": { "type":"integer"},
"recentlyRequested": { "type":"integer"},
"nonActive": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"totalPages": { "type": "integer" },
"totalEntries": { "type": "integer" }
}
},
"filterCount": {
"type": "object",
"properties": {
"active": { "type":"integer"},
"allMedications": { "type":"integer"},
"recentlyRequested": { "type":"integer"},
"nonActive": { "type":"integer"},
"renewal": { "type":"integer"}
}
}
}
}
Expand Down
Loading

0 comments on commit faa1814

Please sign in to comment.