Class: Usman::Api::V1::ContactsController

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

Instance Method Summary collapse

Methods included from Usman::ApiHelper

#current_device, #current_user, #require_admin_auth_token, #require_api_token, #require_auth_token, #require_super_admin_auth_token, #store_last_accessed_api

Instance Method Details

#indexObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/usman/api/v1/contacts_controller.rb', line 53

def index
  proc_code = Proc.new do
    if @current_registration
      unless @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_does_not_exists.heading"),
          message: I18n.translate("api.profile.user_does_not_exists.message")
        }
      else
        @contacts = @current_user.contacts.page(@current_page).per(@per_page)
        @success = true
        @data = ActiveModelSerializers::SerializableResource.new(@contacts, each_serializer: ContactSerializer)
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end

#showObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/usman/api/v1/contacts_controller.rb', line 78

def show
  proc_code = Proc.new do
    if @current_registration
      unless @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_does_not_exists.heading"),
          message: I18n.translate("api.profile.user_does_not_exists.message")
        }
      else
        @contact = @current_user.contacts.where(id: params[:id]).first || @current_user.contacts.build
        @success = true
        @data = ActiveModelSerializers::SerializableResource.new(@contact, serializer: ContactSerializer)
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end

#syncObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/usman/api/v1/contacts_controller.rb', line 6

def sync
  proc_code = Proc.new do
    if @current_registration
      unless @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_does_not_exists.heading"),
          message: I18n.translate("api.profile.user_does_not_exists.message")
        }
      else
        contacts = []
        params[:contacts].each do |cnt|
          formatted_number = cnt["contact_number"].gsub(' ','').gsub('(','').gsub(')','').gsub('-','').strip
          contact = Usman::Contact.where("contact_number = ?", formatted_number).first || Usman::Contact.where("email = ?", cnt["email"]).first || Usman::Contact.new
          
          contact.name = cnt["name"] unless cnt["name"].blank?
          contact. = cnt["account_type"] unless cnt["account_type"].blank?
          contact.email = cnt["email"] unless cnt["email"].blank?
          contact.contact_number = cnt["contact_number"] unless cnt["contact_number"].blank?
          
          contact.owner = @current_user
          contact.done_deal_user = contact.get_done_deal_user
          contact.registration = @current_registration
          contact.device = @current_device

          contact.save
          contacts << contact if contact.done_deal_user_id
        end

        @success = true
        @alert = {
          heading: I18n.translate("api.contacts.synced_successfully.heading"),
          message: I18n.translate("api.contacts.synced_successfully.message")
        }
        @data = ActiveModelSerializers::SerializableResource.new(contacts, each_serializer: ContactSerializer)
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end