Import more fields from Litefarm

This commit is contained in:
Maikel Linke
2025-12-09 12:27:22 +11:00
parent 3331aaa382
commit 3aa4c2a25f
4 changed files with 98 additions and 0 deletions

View File

@@ -1,5 +1,8 @@
# frozen_string_literal: true
require "private_address_check"
require "private_address_check/tcpsocket_ext"
class EnterpriseImporter
def initialize(owner, dfc_enterprise)
@owner = owner
@@ -25,6 +28,7 @@ class EnterpriseImporter
@owner.owned_enterprises.new(
address: Spree::Address.new,
semantic_link: SemanticLink.new(semantic_id: @dfc_enterprise.semanticId),
visible: "public",
)
end
@@ -40,5 +44,41 @@ class EnterpriseImporter
state: state,
country: state.country,
)
enterprise.email_address = @dfc_enterprise.emails.first
enterprise.description = @dfc_enterprise.description
enterprise.phone = @dfc_enterprise.phoneNumbers.first&.phoneNumber
enterprise.website = @dfc_enterprise.websites.first
apply_social_media(enterprise)
apply_logo(enterprise)
end
def apply_social_media(enterprise)
attributes = {}
@dfc_enterprise.socialMedias.each do |media|
attributes[media.name.downcase] = media.url
end
attributes["twitter"] = attributes.delete("x") if attributes.key?("x")
enterprise_attributes = attributes.slice(SocialMediaBuilder::NAMES)
enterprise.assign_attributes(enterprise_attributes)
end
def apply_logo(enterprise)
link = @dfc_enterprise.logo
logo = enterprise.logo
return if link.blank?
return if logo.blob && (logo.blob.custom_metadata&.fetch("origin", nil) == link)
url = URI.parse(link)
filename = File.basename(url.path)
metadata = { custom: { origin: link } }
PrivateAddressCheck.only_public_connections do
logo.attach(io: url.open, filename:, metadata:)
end
rescue StandardError
# Any URL parsing or network error shouldn't impact the import
# at all. Maybe we'll add UX for error handling later.
nil
end
end

View File

@@ -28,5 +28,11 @@ RSpec.describe DfcImporter do
expect {
subject.import_enterprise_profiles("lf-dev", endpoint)
}.not_to have_enqueued_mail
expect(enterprise.name).to eq "DFC Test Farm Beta (All Supplied Fields)"
expect(enterprise.email_address).to eq "dfcshop@example.com"
expect(enterprise.logo.blob.content_type).to eq "image/webp"
expect(enterprise.logo.blob.byte_size).to eq 8974
expect(enterprise.visible).to eq "public"
end
end

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,15 @@ VCR.configure do |config|
config.hook_into :webmock
config.configure_rspec_metadata!
# Change recording mode during development:
#
# VCR_RECORD=new_episodes ./bin/rspec spec/example_spec.rb
# VCR_RECORD=all ./bin/rspec spec/example_spec.rb
#
if ENV.fetch("VCR_RECORD", nil)
config.default_cassette_options = { record: ENV.fetch("VCR_RECORD").to_sym }
end
# Chrome calls a lot of services and they trip us up.
config.ignore_hosts(
"localhost", "127.0.0.1", "0.0.0.0",