Skip to content

Commit

Permalink
fix: order for canned response (#6400)
Browse files Browse the repository at this point in the history
* feat: order canned response

Order canned responses by short_code match first and then with content

* Added specs

---------

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
  • Loading branch information
scmmishra and pranavrajs committed Feb 6, 2023
1 parent d672aa3 commit f8aa544
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def canned_response_params

def canned_responses
if params[:search]
Current.account.canned_responses.where('short_code ILIKE :search OR content ILIKE :search', search: "%#{params[:search]}%")
Current.account.canned_responses
.where('short_code ILIKE :search OR content ILIKE :search', search: "%#{params[:search]}%")
.order_by_search(params[:search])

else
Current.account.canned_responses
end
Expand Down
10 changes: 10 additions & 0 deletions app/models/canned_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ class CannedResponse < ApplicationRecord
validates_uniqueness_of :short_code, scope: :account_id

belongs_to :account

scope :order_by_search, lambda { |search|
short_code_starts_with = sanitize_sql_array(['WHEN short_code ILIKE ? THEN 1', "#{search}%"])
short_code_like = sanitize_sql_array(['WHEN short_code ILIKE ? THEN 0.5', "%#{search}%"])
content_like = sanitize_sql_array(['WHEN content ILIKE ? THEN 0.2', "%#{search}%"])

order_clause = "CASE #{short_code_starts_with} #{short_code_like} #{content_like} ELSE 0 END"

order(Arel.sql(order_clause) => :desc)
}
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:account) { create(:account) }

before do
create(:canned_response, account: account)
create(:canned_response, account: account, content: 'Hey {{ contact.name }}, Thanks for reaching out', short_code: 'name-short-code')
end

describe 'GET /api/v1/accounts/{account.id}/canned_responses' do
Expand All @@ -29,17 +29,22 @@
end

it 'returns all the canned responses the user searched for' do
create(:canned_response, account: account)
cr1 = account.canned_responses.first
create(:canned_response, account: account, content: 'Great! Looking forward', short_code: 'short-code')
cr2 = create(:canned_response, account: account, content: 'Thanks for reaching out', short_code: 'content-with-thanks')
cr3 = create(:canned_response, account: account, content: 'Thanks for reaching out', short_code: 'Thanks')

params = { search: CannedResponse.last.short_code }
params = { search: 'thanks' }

get "/api/v1/accounts/#{account.id}/canned_responses",
params: params,
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)).to eq([CannedResponse.last].as_json)
expect(JSON.parse(response.body)).to eq(
[cr3, cr2, cr1].as_json
)
end
end
end
Expand All @@ -65,7 +70,7 @@
as: :json

expect(response).to have_http_status(:success)
expect(CannedResponse.count).to eq(2)
expect(account.canned_responses.count).to eq(2)
end
end
end
Expand Down

0 comments on commit f8aa544

Please sign in to comment.