mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Update existing enterprises
This commit is contained in:
@@ -77,6 +77,7 @@ class Enterprise < ApplicationRecord
|
||||
has_many :connected_apps, dependent: :destroy
|
||||
has_many :dfc_permissions, dependent: :destroy
|
||||
has_one :custom_tab, dependent: :destroy
|
||||
has_one :semantic_link, as: :subject, dependent: :delete
|
||||
|
||||
delegate :latitude, :longitude, :city, :state_name, to: :address
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@ class DfcImporter
|
||||
|
||||
def import_profile(farm)
|
||||
owner = find_or_import_user(farm)
|
||||
EnterpriseImporter.new.import(owner, farm)
|
||||
enterprise = EnterpriseImporter.new(owner, farm).import
|
||||
enterprise.save! if enterprise.changed?
|
||||
enterprise.address.save! if enterprise.address.changed?
|
||||
end
|
||||
|
||||
def find_or_import_user(farm)
|
||||
|
||||
@@ -1,18 +1,44 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class EnterpriseImporter
|
||||
def import(owner, dfc_enterprise)
|
||||
address = dfc_enterprise.localizations.first
|
||||
def initialize(owner, dfc_enterprise)
|
||||
@owner = owner
|
||||
@dfc_enterprise = dfc_enterprise
|
||||
end
|
||||
|
||||
def import
|
||||
enterprise = find || new
|
||||
|
||||
apply(enterprise)
|
||||
|
||||
enterprise
|
||||
end
|
||||
|
||||
def find
|
||||
semantic_id = @dfc_enterprise.semanticId
|
||||
|
||||
@owner.owned_enterprises.includes(:semantic_link)
|
||||
.find_by(semantic_link: { semantic_id: })
|
||||
end
|
||||
|
||||
def new
|
||||
@owner.owned_enterprises.new(
|
||||
address: Spree::Address.new,
|
||||
semantic_link: SemanticLink.new(semantic_id: @dfc_enterprise.semanticId),
|
||||
)
|
||||
end
|
||||
|
||||
def apply(enterprise)
|
||||
address = @dfc_enterprise.localizations.first
|
||||
state = Spree::State.find_by(name: address.region) || Spree::State.first
|
||||
owner.owned_enterprises.create!(
|
||||
name: dfc_enterprise.name,
|
||||
address: Spree::Address.new(
|
||||
address1: address.street,
|
||||
city: address.city,
|
||||
zipcode: address.postalCode,
|
||||
state: state,
|
||||
country: state.country,
|
||||
),
|
||||
|
||||
enterprise.name = @dfc_enterprise.name
|
||||
enterprise.address.assign_attributes(
|
||||
address1: address.street,
|
||||
city: address.city,
|
||||
zipcode: address.postalCode,
|
||||
state: state,
|
||||
country: state.country,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,5 +18,13 @@ RSpec.describe DfcImporter do
|
||||
# Consider creating a test helper if you find this useful elsewhere.
|
||||
# allow(ApplicationMailer).to receive(:delivery_method).and_return(:letter_opener)
|
||||
# perform_enqueued_jobs(only: ActionMailer::MailDeliveryJob)
|
||||
|
||||
enterprise = Enterprise.last
|
||||
expect(enterprise.semantic_link.semantic_id).to match /litefarm\.org/
|
||||
|
||||
# Repeating works without creating duplicates:
|
||||
expect {
|
||||
subject.import_enterprise_profiles("lf-dev")
|
||||
}.not_to have_enqueued_mail
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user