Import products from the FDC (Shopify) API

This commit is contained in:
Maikel Linke
2024-05-24 16:52:35 +10:00
committed by zanetagebka
parent 1807bc1788
commit 29adbcb479
3 changed files with 240 additions and 1 deletions

View File

@@ -20,7 +20,7 @@ module Admin
catalog_url = params.require(:catalog_url)
json_catalog = DfcRequest.new(spree_current_user).call(catalog_url)
json_catalog = fetch_catalog(catalog_url)
graph = DfcIo.import(json_catalog)
# * First step: import all products for given enterprise.
@@ -34,6 +34,16 @@ module Admin
private
def fetch_catalog(url)
if url =~ /food-data-collaboration/
fdc_json = FdcRequest.new(spree_current_user).call(url)
fdc_message = JSON.parse(fdc_json)
fdc_message["products"]
else
DfcRequest.new(spree_current_user).call(url)
end
end
# Most of this code is the same as in the DfcProvider::SuppliedProductsController.
def import_product(subject, enterprise)
return unless subject.is_a? DataFoodConsortium::Connector::SuppliedProduct

File diff suppressed because one or more lines are too long

View File

@@ -41,4 +41,27 @@ RSpec.describe "DFC Product Import" do
expect(page).to have_content "Importing a DFC product catalog"
expect(page).to have_content "Imported products: 1"
end
it "imports from a FDC catalog", vcr: true do
user.oidc_account.update!(
uid: "testdfc@protonmail.com",
refresh_token: ENV.fetch("OPENID_REFRESH_TOKEN"),
updated_at: 1.day.ago,
)
visit admin_product_import_path
select enterprise.name, from: "Enterprise"
url = "https://food-data-collaboration-produc-fe870152f634.herokuapp.com/fdc/products?shop=test-hodmedod.myshopify.com"
fill_in "catalog_url", with: url
expect {
click_button "Import"
}.to change {
enterprise.supplied_products.count
}
expect(page).to have_content "Importing a DFC product catalog"
end
end