mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-30 06:31:16 +00:00
Import products from the FDC (Shopify) API
This commit is contained in:
committed by
zanetagebka
parent
1807bc1788
commit
29adbcb479
@@ -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
|
||||
|
||||
206
spec/fixtures/vcr_cassettes/DFC_Product_Import/imports_from_a_FDC_catalog.yml
vendored
Normal file
206
spec/fixtures/vcr_cassettes/DFC_Product_Import/imports_from_a_FDC_catalog.yml
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user