From 6810bc55480cbede477250372e71ded4aa4a125b Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Mon, 9 Nov 2020 09:36:07 +0100 Subject: [PATCH] Fixes #3270 - Ticket selector "within last (relative)" and "within next (relative)" not working correctly. --- app/models/ticket.rb | 6 ++- spec/models/ticket_spec.rb | 70 +++++++++++++++++++++++++++++++ test/unit/ticket_selector_test.rb | 46 -------------------- 3 files changed, 74 insertions(+), 48 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 9d91e91f0..41526f321 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -813,7 +813,7 @@ condition example query += "#{attribute} >= ?" bind_params.push selector['value'] elsif selector['operator'] == 'within last (relative)' - query += "#{attribute} >= ?" + query += "#{attribute} BETWEEN ? AND ?" time = nil case selector['range'] when 'minute' @@ -830,8 +830,9 @@ condition example raise "Unknown selector attributes '#{selector.inspect}'" end bind_params.push time + bind_params.push Time.zone.now elsif selector['operator'] == 'within next (relative)' - query += "#{attribute} <= ?" + query += "#{attribute} BETWEEN ? AND ?" time = nil case selector['range'] when 'minute' @@ -847,6 +848,7 @@ condition example else raise "Unknown selector attributes '#{selector.inspect}'" end + bind_params.push Time.zone.now bind_params.push time elsif selector['operator'] == 'before (relative)' query += "#{attribute} <= ?" diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 8ad3328d8..a60d34ac1 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -828,6 +828,76 @@ RSpec.describe Ticket, type: :model do .to change { ticket.reload.escalation_at }.to(nil) end end + + context 'when within last (relative)' do + let(:first_response_time) { 5 } + let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) } + let(:within_condition) do + { 'ticket.escalation_at'=>{ 'operator' => 'within last (relative)', 'value' => '30', 'range' => 'minute' } } + end + + before do + sla + + travel_to '2020-11-05 11:37:00' + + ticket = create(:ticket) + create(:ticket_article, :inbound_email, ticket: ticket) + + travel_to '2020-11-05 11:50:00' + end + + context 'when in range' do + it 'does find the ticket' do + count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true) + expect(count).to eq(1) + end + end + + context 'when out of range' do + let(:first_response_time) { 500 } + + it 'does not find the ticket' do + count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true) + expect(count).to eq(0) + end + end + end + + context 'when within next (relative)' do + let(:first_response_time) { 5 } + let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) } + let(:within_condition) do + { 'ticket.escalation_at'=>{ 'operator' => 'within next (relative)', 'value' => '30', 'range' => 'minute' } } + end + + before do + sla + + travel_to '2020-11-05 11:50:00' + + ticket = create(:ticket) + create(:ticket_article, :inbound_email, ticket: ticket) + + travel_to '2020-11-05 11:37:00' + end + + context 'when in range' do + it 'does find the ticket' do + count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true) + expect(count).to eq(1) + end + end + + context 'when out of range' do + let(:first_response_time) { 500 } + + it 'does not find the ticket' do + count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true) + expect(count).to eq(0) + end + end + end end describe '#first_response_escalation_at' do diff --git a/test/unit/ticket_selector_test.rb b/test/unit/ticket_selector_test.rb index f79577a10..a64a22245 100644 --- a/test/unit/ticket_selector_test.rb +++ b/test/unit/ticket_selector_test.rb @@ -409,29 +409,6 @@ class TicketSelectorTest < ActiveSupport::TestCase ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer2) assert_equal(ticket_count, 2) - condition = { - 'ticket.group_id' => { - operator: 'is', - value: @group.id, - }, - 'ticket.created_at' => { - operator: 'within next (relative)', - range: 'year', # minute|hour|day|month| - value: '10', - }, - } - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @agent1) - assert_equal(ticket_count, 3) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @agent2) - assert_equal(ticket_count, 0) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer1) - assert_equal(ticket_count, 1) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer2) - assert_equal(ticket_count, 2) - condition = { 'ticket.group_id' => { operator: 'is', @@ -567,29 +544,6 @@ class TicketSelectorTest < ActiveSupport::TestCase ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer2) assert_equal(ticket_count, 0) - condition = { - 'ticket.group_id' => { - operator: 'is', - value: @group.id, - }, - 'ticket.updated_at' => { - operator: 'within next (relative)', - range: 'year', # minute|hour|day|month| - value: '10', - }, - } - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @agent1) - assert_equal(ticket_count, 3) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @agent2) - assert_equal(ticket_count, 0) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer1) - assert_equal(ticket_count, 1) - - ticket_count, tickets = Ticket.selectors(condition, limit: 10, current_user: @customer2) - assert_equal(ticket_count, 2) - condition = { 'ticket.group_id' => { operator: 'is',