From ce28c10c7ed81f6284ff5ff3c8c21bf10efd16e3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 28 Aug 2024 15:38:45 +1000 Subject: [PATCH] Move sales data generation to a service object There will be lots and lots. The sales data root object is also the authenticated person. The data has its own URL (semantic id) which doens't need to contain the user id. The service object can also be tested more easily. I'm setting up the test data here. --- .../affiliate_sales_data_controller.rb | 3 +- .../services/affiliate_sales_data_builder.rb | 11 ++++ .../requests/affiliate_sales_data_spec.rb | 5 +- .../affiliate_sales_data_builder_spec.rb | 54 +++++++++++++++++++ swagger/dfc.yaml | 10 +++- 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 engines/dfc_provider/app/services/affiliate_sales_data_builder.rb create mode 100644 engines/dfc_provider/spec/services/affiliate_sales_data_builder_spec.rb diff --git a/engines/dfc_provider/app/controllers/dfc_provider/affiliate_sales_data_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/affiliate_sales_data_controller.rb index be26749cbd..73b09af76c 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/affiliate_sales_data_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/affiliate_sales_data_controller.rb @@ -4,7 +4,8 @@ module DfcProvider # Aggregates anonymised sales data for a research project. class AffiliateSalesDataController < DfcProvider::ApplicationController def show - person = PersonBuilder.person(current_user) + person = AffiliateSalesDataBuilder.person + render json: DfcIo.export(person) end end diff --git a/engines/dfc_provider/app/services/affiliate_sales_data_builder.rb b/engines/dfc_provider/app/services/affiliate_sales_data_builder.rb new file mode 100644 index 0000000000..1b2e99ec3c --- /dev/null +++ b/engines/dfc_provider/app/services/affiliate_sales_data_builder.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AffiliateSalesDataBuilder < DfcBuilder + class << self + def person + DataFoodConsortium::Connector::Person.new( + urls.affiliate_sales_data_url, + ) + end + end +end diff --git a/engines/dfc_provider/spec/requests/affiliate_sales_data_spec.rb b/engines/dfc_provider/spec/requests/affiliate_sales_data_spec.rb index 08fc18817f..b8d03b881f 100644 --- a/engines/dfc_provider/spec/requests/affiliate_sales_data_spec.rb +++ b/engines/dfc_provider/spec/requests/affiliate_sales_data_spec.rb @@ -3,7 +3,7 @@ require_relative "../swagger_helper" RSpec.describe "AffiliateSalesData", swagger_doc: "dfc.yaml", rswag_autodoc: true do - let(:user) { create(:oidc_user, id: 10_000) } + let(:user) { create(:oidc_user) } before { login_as user } @@ -14,7 +14,8 @@ RSpec.describe "AffiliateSalesData", swagger_doc: "dfc.yaml", rswag_autodoc: tru response "200", "successful" do run_test! do expect(json_response).to include( - "@id" => "http://test.host/api/dfc/persons/10000", + "@id" => "http://test.host/api/dfc/affiliate_sales_data", + "@type" => "dfc-b:Person", ) end end diff --git a/engines/dfc_provider/spec/services/affiliate_sales_data_builder_spec.rb b/engines/dfc_provider/spec/services/affiliate_sales_data_builder_spec.rb new file mode 100644 index 0000000000..cd8ef1cc54 --- /dev/null +++ b/engines/dfc_provider/spec/services/affiliate_sales_data_builder_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require_relative "../spec_helper" + +RSpec.describe AffiliateSalesDataBuilder do + let(:user) { build(:user) } + + describe ".person" do + let(:person) { described_class.person(user) } + + it "returns data as Person" do + expect(person).to be_a DataFoodConsortium::Connector::Person + expect(person.semanticId).to eq "http://test.host/api/dfc/affiliate_sales_data" + end + + it "returns required sales data" do + supplier = create( + :supplier_enterprise, + owner: user, + users: [user], + address: create(:address, zipcode: "5555"), + ) + product = create( + :product, + supplier_id: supplier.id, + variant_unit: "item", + ) + variant = product.variants.first + distributor = create( + :distributor_enterprise, + address: create(:address, zipcode: "6666"), + ) + line_item = build( + :line_item, + variant:, + quantity: 2, + price: 3, + ) + order_cycle = create( + :order_cycle, + suppliers: [supplier], + distributors: [distributor], + ) + order_cycle.exchanges.incoming.first.variants << variant + order_cycle.exchanges.outgoing.first.variants << variant + create( + :order, + order_cycle:, + distributor:, + line_items: [line_item], + ) + end + end +end diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index d8d0cd8ed6..d302047b7c 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -83,9 +83,17 @@ paths: test_example: value: "@context": https://www.datafoodconsortium.org - "@id": http://test.host/api/dfc/persons/10000 + "@id": http://test.host/api/dfc/affiliate_sales_data "@type": dfc-b:Person dfc-b:logo: '' + dfc-b:firstName: '' + dfc-b:familyName: '' + dfc-b:affiliates: + "@type": dfc-b:Enterprise + dfc-b:logo: '' + dfc-b:name: '' + dfc-b:hasDescription: '' + dfc-b:VATnumber: '' "/api/dfc/enterprises/{enterprise_id}/catalog_items": parameters: - name: enterprise_id