Skip to content

Commit

Permalink
Import DFC SuppliedProduct as new variant of existing product
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Sep 20, 2023
1 parent fc112a8 commit 8b32ac2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
24 changes: 12 additions & 12 deletions app/models/spree/product.rb
Expand Up @@ -272,18 +272,6 @@ def destruction
end
end

private

def update_units
return unless saved_change_to_variant_unit? || saved_change_to_variant_unit_name?

variants.each(&:update_units)
end

def touch_distributors
Enterprise.distributing_products(id).each(&:touch)
end

def ensure_standard_variant
return unless variants.empty?

Expand All @@ -298,6 +286,18 @@ def ensure_standard_variant
variants << variant
end

private

def update_units
return unless saved_change_to_variant_unit? || saved_change_to_variant_unit_name?

variants.each(&:update_units)
end

def touch_distributors
Enterprise.distributing_products(id).each(&:touch)
end

def validate_image
return if image.blank? || image.valid?

Expand Down
Expand Up @@ -14,13 +14,19 @@ def create

return head :bad_request unless supplied_product

product = SuppliedProductBuilder.import(supplied_product)
product.supplier = current_enterprise
product.save!
variant = SuppliedProductBuilder.import_variant(supplied_product)
product = variant.product

supplied_product = SuppliedProductBuilder.supplied_product(
product.variants.first
)
if product.new_record?
product.supplier = current_enterprise
product.save!
end

if variant.new_record?
variant.save!
end

supplied_product = SuppliedProductBuilder.supplied_product(variant)
render json: DfcIo.export(supplied_product)
end

Expand Down
20 changes: 19 additions & 1 deletion engines/dfc_provider/app/services/supplied_product_builder.rb
Expand Up @@ -17,7 +17,25 @@ def self.supplied_product(variant)
)
end

def self.import(supplied_product)
def self.import_variant(supplied_product)
product_id = supplied_product.spree_product_id

if product_id.present?
product = Spree::Product.find(product_id)
Spree::Variant.new(
product: product,

Check warning on line 26 in engines/dfc_provider/app/services/supplied_product_builder.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Omit the hash value. Raw Output: engines/dfc_provider/app/services/supplied_product_builder.rb:26:18: C: Style/HashSyntax: Omit the hash value.
price: 0,
).tap do |variant|
apply(supplied_product, variant)
end
else
product = import_product(supplied_product)
product.ensure_standard_variant
product.variants.first
end
end

def self.import_product(supplied_product)
Spree::Product.new(
name: supplied_product.name,
description: supplied_product.description,
Expand Down
Expand Up @@ -127,11 +127,15 @@ def skos_concept(object)
end

def guess_setter_name(predicate)
fragment = predicate.fragment
name =

# Some predicates are named like `hasQuantity`
# but the attribute name would be `quantity`.
predicate.fragment&.sub(/^has/, "")&.camelize(:lower) ||

# And sometimes the URI looks like `ofn:spree_product_id`.
predicate.to_s.split(":").last

# Some predicates are named like `hasQuantity`
# but the attribute name would be `quantity`.
name = fragment.sub(/^has/, "").camelize(:lower)

"#{name}="
end
Expand Down
21 changes: 21 additions & 0 deletions engines/dfc_provider/spec/requests/supplied_products_spec.rb
Expand Up @@ -96,6 +96,27 @@
expect(variant.unit_value).to eq 3
expect(variant.product_id).to eq spree_product_id

# References the associated Spree::Product
product_id = json_response["ofn:spree_product_id"]
product = Spree::Product.find(product_id)
expect(product.name).to eq "Apple"
expect(product.variants).to eq [variant]

# Creates a variant for existing product
supplied_product[:"ofn:spree_product_id"] = product_id

Check warning on line 106 in engines/dfc_provider/spec/requests/supplied_products_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Prefer single-quoted symbols when you don't need string interpolation or special symbols. Raw Output: engines/dfc_provider/spec/requests/supplied_products_spec.rb:106:28: C: Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.
supplied_product[:"dfc-b:hasQuantity"][:"dfc-b:value"] = 6

Check warning on line 107 in engines/dfc_provider/spec/requests/supplied_products_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Prefer single-quoted symbols when you don't need string interpolation or special symbols. Raw Output: engines/dfc_provider/spec/requests/supplied_products_spec.rb:107:28: C: Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.

Check warning on line 107 in engines/dfc_provider/spec/requests/supplied_products_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Prefer single-quoted symbols when you don't need string interpolation or special symbols. Raw Output: engines/dfc_provider/spec/requests/supplied_products_spec.rb:107:50: C: Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.

expect {
submit_request(example.metadata)
product.variants.reload
}
.to change { product.variants.count }.by(1)

variant_id = json_response["@id"].split("/").last.to_i
second_variant = Spree::Variant.find(variant_id)
expect(product.variants).to match_array [variant, second_variant]
expect(second_variant.unit_value).to eq 6

# Insert static value to keep documentation deterministic:
response.body.gsub!(
"supplied_products/#{variant_id}",
Expand Down

0 comments on commit 8b32ac2

Please sign in to comment.