diff --git a/app/models/report.rb b/app/models/report.rb index 58ba63052b5..d50679a25c3 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -63,11 +63,12 @@ class Report ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter]) end - filtered_results = data.where('date >= ? AND date <= ?', report.start_date.to_date, report.end_date.to_date) - filtered_results = filtered_results.where(category_id: report.category_id) if report.category_id + filtered_results = data + filtered_results = data.filtered_results.where(category_id: report.category_id) if report.category_id report.data = [] - filtered_results.order(date: :asc) + filtered_results.where('date >= ? AND date <= ?', report.start_date.to_date, report.end_date.to_date) + .order(date: :asc) .group(:date) .sum(:count) .each do |date, count| @@ -75,7 +76,10 @@ class Report end report.total = data.sum(:count) - report.prev30Days = filtered_results.sum(:count) + report.prev30Days = filtered_results.where('date >= ? AND date <= ?', + (report.start_date - 31.days).to_date, + (report.end_date - 31.days).to_date ) + .sum(:count) end diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 6edb767429b..c47b24825be 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -132,7 +132,7 @@ describe Report do end it 'returns previous 30 days of data' do - expect(report.prev30Days).to eq 14 + expect(report.prev30Days).to eq 35 end end end