diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 0cc5e52d8154..f3c85be7a5ba 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -169,6 +169,12 @@ def conversations ) sort_by, sort_order = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['last_activity_at_desc'] - @conversations.send(sort_by, sort_order).page(current_page).per(ENV.fetch('CONVERSATION_RESULTS_PER_PAGE', '25').to_i) + @conversations = @conversations.send(sort_by, sort_order) + + if params[:updated_within].present? + @conversations.where('conversations.updated_at > ?', Time.zone.now - params[:updated_within].to_i.seconds) + else + @conversations.page(current_page).per(ENV.fetch('CONVERSATION_RESULTS_PER_PAGE', '25').to_i) + end end end diff --git a/spec/finders/conversation_finder_spec.rb b/spec/finders/conversation_finder_spec.rb index 2ce5b6bf990f..4d6e9ed40f2c 100644 --- a/spec/finders/conversation_finder_spec.rb +++ b/spec/finders/conversation_finder_spec.rb @@ -146,6 +146,30 @@ end end + context 'with updated_within' do + let(:params) { { updated_within: 20, assignee_type: 'unassigned', sort_by: 'created_at_asc' } } + + it 'filters based on params, sort order but returns all conversations without pagination with in time range' do + # value of updated_within is in seconds + # write spec based on that + conversations = create_list(:conversation, 50, account: account, + inbox: inbox, assignee: nil, + updated_at: Time.now.utc - 30.seconds, + created_at: Time.now.utc - 30.seconds) + # update updated_at of 27 conversations to be with in 20 seconds + conversations[0..27].each do |conversation| + conversation.update(updated_at: Time.now.utc - 10.seconds) + end + result = conversation_finder.perform + # pagination is not applied + # filters are applied + # modified conversations + 1 conversation created during set up + expect(result[:conversations].length).to be 29 + # ensure that the conversations are sorted by created_at + expect(result[:conversations].first.created_at).to be < result[:conversations].last.created_at + end + end + context 'with pagination' do let(:params) { { status: 'open', assignee_type: 'me', page: 1 } }