Class: Api::V1::ContactsController

Inherits:
BaseController show all
Defined in:
app/controllers/api/v1/contacts_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#aspects_membership_serviceObject


57
58
59
# File 'app/controllers/api/v1/contacts_controller.rb', line 57

def aspects_membership_service
  AspectsMembershipService.new(current_user)
end

#createObject


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/api/v1/contacts_controller.rb', line 29

def create
  aspect_id = params.require(:aspect_id)
  person = Person.find_by(guid: params.require(:person_guid))
  aspect_membership = aspects_membership_service.create(aspect_id, person.id) if person.present?

  if aspect_membership
    head :no_content
  else
    render_error 422, "Failed to add user to aspect"
  end
rescue ActiveRecord::RecordNotUnique
  render_error 422, "Failed to add user to aspect"
end

#destroyObject


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/api/v1/contacts_controller.rb', line 43

def destroy
  aspect_id = params.require(:aspect_id)
  person = Person.find_by(guid: params[:id])
  result = aspects_membership_service.destroy_by_ids(aspect_id, person.id) if person.present?

  if result && result[:success]
    head :no_content
  else
    render_error 422, "Failed to remove user from aspect"
  end
rescue ActiveRecord::RecordNotFound
  render_error 404, "Aspect or contact on aspect not found"
end

#indexObject


20
21
22
23
24
25
26
27
# File 'app/controllers/api/v1/contacts_controller.rb', line 20

def index
  contacts_query = aspects_membership_service.contacts_in_aspect(params.require(:aspect_id))
  contacts_page = index_pager(contacts_query).response
  contacts_page[:data] = contacts_page[:data].map do |c|
    ContactPresenter.new(c, current_user).as_api_json_without_contact
  end
  render_paged_api_response contacts_page
end