From 9b9c019de044c10da7440975d3909c11197a5bcc Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Wed, 16 Nov 2022 08:11:48 -0800 Subject: [PATCH] feat: Add support for `after` param in messages API (#5861) --- app/finders/message_finder.rb | 4 +++- spec/finders/message_finder_spec.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/finders/message_finder.rb b/app/finders/message_finder.rb index e2581e0ad..7a00e5cbf 100644 --- a/app/finders/message_finder.rb +++ b/app/finders/message_finder.rb @@ -21,7 +21,9 @@ class MessageFinder end def current_messages - if @params[:before].present? + if @params[:after].present? + messages.reorder('created_at asc').where('id >= ?', @params[:before].to_i).limit(20) + elsif @params[:before].present? messages.reorder('created_at desc').where('id < ?', @params[:before].to_i).limit(20).reverse else messages.reorder('created_at desc').limit(20).reverse diff --git a/spec/finders/message_finder_spec.rb b/spec/finders/message_finder_spec.rb index 58dafcef6..ad43ca50e 100644 --- a/spec/finders/message_finder_spec.rb +++ b/spec/finders/message_finder_spec.rb @@ -47,5 +47,16 @@ describe ::MessageFinder do expect(result.count).to be 6 end end + + context 'with after attribute' do + let(:params) { { after: conversation.messages.first.id } } + + it 'filter conversations by status' do + result = message_finder.perform + expect(result.count).to be 6 + expect(result.first.id).to be conversation.messages.first.id + expect(result.last.message_type).to eq 'outgoing' + end + end end end