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.
This commit is contained in:
Maikel Linke
2024-08-28 15:38:45 +10:00
parent 4342d3b912
commit ce28c10c7e
5 changed files with 79 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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