Skip to content

Commit

Permalink
Merge pull request #6 from DigitalTolk/custom/phone-search-api
Browse files Browse the repository at this point in the history
custom phone search api
  • Loading branch information
jderecho authored Dec 15, 2023
2 parents b1ec835 + a6f14dc commit 7b2bbf4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/v1/accounts/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def search
@contacts = fetch_contacts(contacts)
end

def phone_search
render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return

contacts = resolved_contacts.where(phone_number: params[:q].strip)
render json: { found: contacts.exists?, search_key: params[:q] }
end

def import
render json: { error: I18n.t('errors.contacts.import.failed') }, status: :unprocessable_entity and return if params[:import_file].blank?

Expand Down
4 changes: 4 additions & 0 deletions app/policies/contact_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def search?
true
end

def phone_search?
true
end

def filter?
true
end
Expand Down
10 changes: 10 additions & 0 deletions app/views/api/v1/accounts/contacts/phone_search.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
json.meta do
json.count @contacts_count
json.current_page @current_page
end

json.payload do
json.array! @contacts do |contact|
json.partial! 'api/v1/models/contact', formats: [:json], resource: contact, with_contact_inboxes: @include_contact_inboxes
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
collection do
get :active
get :search
get :phone_search
post :filter
post :import
get :export
Expand Down

0 comments on commit 7b2bbf4

Please sign in to comment.