Class: IntegrationsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/integrations_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/integrations_controller.rb', line 75

def agendrix_consent_url
  Integration
    .available_for_company(current_company_id)
    .find_by!(slug: Integration::Slugs::AGENDRIX)

  url = AgendrixApiService.consent_url

  render json: {
    data: {
      url: url
    }
  }
end

#deactivateObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/integrations_controller.rb', line 134

def deactivate
  integration = Integration
                .available_for_company(current_company_id)
                .find_by!(integration_id: params[:id])

  company_integration = CompanyIntegration.find_by!(
    integration_id: integration.integration_id,
    company_id: current_company_id
  )
  company_integration.update!(active: false)

  render json: {}
end

#indexObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/integrations_controller.rb', line 13

def index
  # needs to be refactored so that company integrations
  # are not fetched in another loop,
  # it's fine for now becuase we only have 1 integration
  # but this will cause N+1 queries in the future
  # and should be fixed so it's cleaner
  query = params[:query] || ''
  order = params[:order] || 'asc'
  order_by = params[:orderBy] || 'integration_id'
  limit = params[:limit] || 100
  offset = params[:offset] || 0
  tag_ids = params[:tag_ids] || []

  integrations = Integration
                 .with_integrated_to_company(current_company_id)
                 .for_tags(tag_ids)
                 .includes(
                   :tags,
                   :company_integrations
                 )

  data,  = fetch_records(
    records: integrations,
    order_by: order_by,
    order: order,
    query: query,
    limit: limit,
    offset: offset,
    search_fields: Integration.search_fields,
    fields: %i[
      integration_id
      logo_url
      slug
      active
    ]
  ).values_at(:data, :metadata)

  data = data.as_json(
    methods: %i[
      name
      description
      logo_url
    ],
    include: %i[
      tags
    ]
  )

  data.map! do |integration|
    integration['integration_error'] = CompanyIntegration.find_by(
      company_id: current_company_id,
      integration_id: integration['integration_id']
    )&.error
    integration
  end

  render json: {
    data: data,
    metadata: 
  }
end

#integrate_agendrixObject



89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/integrations_controller.rb', line 89

def integrate_agendrix
  code = params[:code]
  return render json: { message: 'Missing code' }, status: :bad_request if code.blank?

  AgendrixApiService.new(company_id: current_company_id).integrate(
    code: code
  )

  render json: {}
end

#integrate_quickbooksObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/integrations_controller.rb', line 117

def integrate_quickbooks
  code = params[:code]
  realm_id = params[:realm_id]
  state = params[:state]

  if code.blank? || realm_id.blank?
    return render json: { message: 'Missing code or realm_id' }, status: :bad_request
  end

  QuickbooksApiService.new(company_id: current_company_id).integrate(
    code: code,
    realm_id: realm_id
  )

  render json: {}
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/integrations_controller.rb', line 100

def quickbooks_consent_url
  Integration
    .available_for_company(current_company_id)
    .find_by!(slug: Integration::Slugs::QUICKBOOKS)

  state = SecureRandom.hex(10)
  session[:quickbooks_oauth_state] = state

  url = QuickbooksApiService.consent_url(state: state)

  render json: {
    data: {
      url: url
    }
  }
end

#resyncObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/integrations_controller.rb', line 148

def resync
  integration = Integration
                .available_for_company(current_company_id)
                .find_by!(integration_id: params[:id])

  case integration.slug.to_sym
  when Integration::Slugs::AGENDRIX
    AgendrixMembersPollJob.perform_later(company_id: current_company_id)
    message = I18n.t('messages.resyncing_agendrix_data_started')
  when Integration::Slugs::QUICKBOOKS
    QuickbooksCustomersPollJob.perform_later(company_id: current_company_id)
    message = I18n.t('messages.resyncing_quickbooks_data_started')
  else
    return render json: { error: 'Unsupported integration type for resyncing' }, status: :bad_request
  end

  render json: {
    message: message
  }
end