FIX: CSV Exports were throwing errors with invalid dates

This fix will consider any invalid dates to be non-existant.
This commit is contained in:
Robin Ward 2020-04-07 14:05:27 -04:00
parent e8fad7a69e
commit ce663d67e1
2 changed files with 16 additions and 2 deletions

View File

@ -182,8 +182,14 @@ module Jobs
def report_export def report_export
return enum_for(:report_export) unless block_given? return enum_for(:report_export) unless block_given?
@extra[:start_date] = @extra[:start_date].to_date.beginning_of_day if @extra[:start_date].is_a?(String) # If dates are invalid consider then `nil`
@extra[:end_date] = @extra[:end_date].to_date.end_of_day if @extra[:end_date].is_a?(String) if @extra[:start_date].is_a?(String)
@extra[:start_date] = @extra[:start_date].to_date.beginning_of_day rescue nil
end
if @extra[:end_date].is_a?(String)
@extra[:end_date] = @extra[:end_date].to_date.end_of_day rescue nil
end
@extra[:filters] = {} @extra[:filters] = {}
if @extra[:category_id].present? if @extra[:category_id].present?
@extra[:filters][:category] = @extra[:category_id].to_i @extra[:filters][:category] = @extra[:category_id].to_i

View File

@ -85,6 +85,14 @@ describe Jobs::ExportCsvFile do
exporter exporter
end end
it "does not throw an error when the dates are invalid" do
Jobs::ExportCsvFile.new.execute(
entity: 'report',
user_id: user.id,
args: { start_date: 'asdfasdf', end_date: 'not-a-date', name: 'dau_by_mau' }
)
end
it 'works with single-column reports' do it 'works with single-column reports' do
user.user_visits.create!(visited_at: '2010-01-01', posts_read: 42) user.user_visits.create!(visited_at: '2010-01-01', posts_read: 42)
Fabricate(:user).user_visits.create!(visited_at: '2010-01-03', posts_read: 420) Fabricate(:user).user_visits.create!(visited_at: '2010-01-03', posts_read: 420)