Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/hub_spot_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Api::HubSpotController < ApplicationController

def create
inquiry = Inquiry.find(params[:inquiry_id])
HubSpotService.move(inquiry)
HubSpotService.send(inquiry)
inquiry.notes.create(body: "This inquiry was exported to HubSpot", creator: current_user)
render json: { message: 'Successfully added to HubSpot' }
rescue => error
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/inquiries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Api::InquiriesController < ApplicationController

def create
if @office_provider_inquiry
HubSpotService.move(params[:inquiry])
HubSpotService.send(params[:inquiry])
render json: { message: 'Thanks for your answers! We\'ll be in touch' } and return
else
inquiry = Inquiry.create(inquiry_params)
Expand Down
2 changes: 1 addition & 1 deletion app/models/inquiry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def aasm_event_failed(event_name, old_state_name)

after_create :send_notifications

enum office_type: { office: 1, open_space: 2, combined: 3 }
enum office_type: { office_space: 1, office_room: 2, fixed_space: 3, flexible_space: 4 }
enum inquiry_status: { pending: 1, started: 2, done: 3 }
enum flexible: { yes: 1, no: 2, mixed: 3 }
enum start_date: { now: 1, quarter: 2, unsure: 3 }
Expand Down
48 changes: 35 additions & 13 deletions app/services/hub_spot_service.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module HubSpotService
@api_key = Rails.application.credentials.dig(:hub_spot, :api_key)
def self.move(inquiry)
def self.send(inquiry)
inquiry = OpenStruct.new(inquiry) unless inquiry.is_a?(Inquiry)
contact = create_contact(inquiry)
id = JSON.parse(contact.body)['vid']
contact = find_or_create_contact(inquiry)
id = contact[:vid]
note = if inquiry.try(:officeProvider).nil?
format_note(inquiry)
else
Expand All @@ -14,16 +14,27 @@ def self.move(inquiry)
true
end

def self.create_contact(inquiry)
RestClient.post(
"https://api.hubapi.com/contacts/v1/contact?hapikey=#{@api_key}",
{
properties: [
{ property: 'email', value: inquiry.email },
{ property: 'phone', value: inquiry.phone }
]
}.to_json, { content_type: :json, accept: :json }
)
def self.find_or_create_contact(inquiry)
data = format_data(inquiry)
begin
contact = JSON.parse(
RestClient.get("https://api.hubapi.com/contacts/v1/contact/email/#{inquiry.email}/profile?hapikey=#{@api_key}")
).symbolize_keys
rescue StandardError => e
contact = JSON.parse(e.response.body).symbolize_keys
end
if contact[:status] != 'error'
RestClient.post(
"https://api.hubapi.com/contacts/v1/contact/vid/#{contact[:vid]}/profile?hapikey=#{@api_key}",
data.to_json, { content_type: :json, accept: :json }
)
else
RestClient.post(
"https://api.hubapi.com/contacts/v1/contact?hapikey=#{@api_key}",
data.to_json, { content_type: :json, accept: :json }
)
end
contact
end

def self.create_note(note, id, timestamp)
Expand Down Expand Up @@ -70,4 +81,15 @@ def self.format_rent_out_note(inquiry)
<li>Notes: #{inquiry.notes}</li>
</ul>"
end

def self.format_data(inquiry)
data = {
properties: [
{ property: 'email', value: inquiry.email },
{ property: 'phone', value: inquiry.phone }
]
}
inquiry.try(:name).try(:present?) && data[:properties].push({ property: 'firstname', value: inquiry.name })
data
end
end
Loading