Files
chatwoot/app/controllers/api/v1/accounts/canned_responses_controller.rb
Shivam Mishra f8aa544aae fix: order for canned response (#6400)
* 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>
2023-02-06 12:53:48 -08:00

45 lines
1.0 KiB
Ruby

class Api::V1::Accounts::CannedResponsesController < Api::V1::Accounts::BaseController
before_action :fetch_canned_response, only: [:update, :destroy]
def index
render json: canned_responses
end
def create
@canned_response = Current.account.canned_responses.new(canned_response_params)
@canned_response.save!
render json: @canned_response
end
def update
@canned_response.update!(canned_response_params)
render json: @canned_response
end
def destroy
@canned_response.destroy!
head :ok
end
private
def fetch_canned_response
@canned_response = Current.account.canned_responses.find(params[:id])
end
def canned_response_params
params.require(:canned_response).permit(:short_code, :content)
end
def canned_responses
if 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
end
end