diff --git a/app/helpers/date_range_helper.rb b/app/helpers/date_range_helper.rb index 8d3db3a7a5..cc73af80d8 100644 --- a/app/helpers/date_range_helper.rb +++ b/app/helpers/date_range_helper.rb @@ -18,6 +18,10 @@ def date_range_label "this month" when "last month" "last month" + when "last 12 months" + "last 12 months" + when "prior year" + "prior year" else selected_range_described end diff --git a/app/javascript/application.js b/app/javascript/application.js index c94704d50d..bb8df687e1 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -99,7 +99,9 @@ $(document).ready(function(){ 'This Month': [today.startOf('month').toJSDate(), today.endOf('month').toJSDate()], 'Last Month': [today.minus({'months': 1}).startOf('month').toJSDate(), today.minus({'month': 1}).endOf('month').toJSDate()], - 'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()] + 'Last 12 Months': [today.minus({'months': 12}).plus({'days': 1}).toJSDate(), today.toJSDate()], + 'Prior Year': [today.startOf('year').minus({'years': 1}).toJSDate(), today.minus({'year': 1}).endOf('year').toJSDate()], + 'This Year': [today.startOf('year').toJSDate(), today.endOf('year').toJSDate()], } } }); diff --git a/spec/system/distributions_by_county_system_spec.rb b/spec/system/distributions_by_county_system_spec.rb index a9ca7c8de9..e0cc2188cf 100644 --- a/spec/system/distributions_by_county_system_spec.rb +++ b/spec/system/distributions_by_county_system_spec.rb @@ -1,8 +1,8 @@ RSpec.feature "Distributions by County", type: :system do include_examples "distribution_by_county" - let(:year) { Time.current.year } - let(:issued_at_last_year) { Time.current.utc.change(year: year - 1).to_datetime } + let(:current_year) { Time.current.year } + let(:issued_at_last_year) { Time.current.utc.change(year: current_year - 1).to_datetime } before do sign_in(user) @@ -34,6 +34,50 @@ expect(page).to have_text("25", count: 4) expect(page).to have_text("$262.50", count: 4) end + + it("works for prior year") do + # Should NOT return distribution issued before previous calendar year + last_day_of_two_years_ago = Time.current.utc.change(year: current_year - 2, month: 12, day: 31).to_datetime + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: last_day_of_two_years_ago) + + # Should return distribution issued during previous calendar year + one_year_ago = issued_at_last_year + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_ago) + + # Should NOT return distribution issued after previous calendar year + first_day_of_current_year = Time.current.utc.change(year: current_year, month: 1, day: 1).to_datetime + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: first_day_of_current_year) + + visit_distribution_by_county_with_specified_date_range("Prior Year") + + partner_1.profile.served_areas.each do |served_area| + expect(page).to have_text(served_area.county.name) + end + expect(page).to have_text("25", count: 4) + expect(page).to have_text("$262.50", count: 4) + end + + it("works for last 12 months") do + # Should NOT return disitribution issued before 12 months ago + one_year_and_one_day_ago = 1.year.ago.prev_day.to_datetime + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: one_year_and_one_day_ago) + + # Should return distribution issued during previous 12 months + today = issued_at_present + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: today) + + # Should NOT return distribution issued in the future + tomorrow = 1.day.from_now.to_datetime + create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: tomorrow) + + visit_distribution_by_county_with_specified_date_range("Last 12 Months") + + partner_1.profile.served_areas.each do |served_area| + expect(page).to have_text(served_area.county.name) + end + expect(page).to have_text("25", count: 4) + expect(page).to have_text("$262.50", count: 4) + end end def visit_distribution_by_county_with_specified_date_range(date_range_string)