diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index fc22b9bda9..e44c660c7d 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -31,7 +31,8 @@ class BackorderJob < ApplicationJob end def self.place_backorder(order, linked_variants) - backorder = FdcBackorderer.new.find_or_build_order(order) + orderer = FdcBackorderer.new + backorder = orderer.find_or_build_order(order) catalog = load_catalog(order.distributor.owner) linked_variants.each_with_index do |variant, index| @@ -44,16 +45,7 @@ class BackorderJob < ApplicationJob backorder.lines << line end - lines = backorder.lines - offers = lines.map(&:offer) - products = offers.map(&:offeredItem) - session = build_sale_session(order) - json = DfcIo.export(backorder, *lines, *offers, *products, session) - - api = DfcRequest.new(order.distributor.owner) - - # Create order via POST: - api.call(FDC_ORDERS_URL, json) + backorderer.send_order(order, backorder) # Once we have transformations and know the quantities in bulk products # we will need to increase on_hand by the ordered quantity. diff --git a/app/services/fdc_backorderer.rb b/app/services/fdc_backorderer.rb index 14e5015593..0a9caadc11 100644 --- a/app/services/fdc_backorderer.rb +++ b/app/services/fdc_backorderer.rb @@ -7,6 +7,64 @@ class FdcBackorderer FDC_NEW_ORDER_URL = "#{FDC_ORDERS_URL}/#".freeze def find_or_build_order(ofn_order) - OrderBuilder.new_order(ofn_order, FDC_NEW_ORDER_URL) + remote_order = find_open_order(ofn_order.distributor.owner) + remote_order || OrderBuilder.new_order(ofn_order, FDC_NEW_ORDER_URL) + end + + def find_open_order(user) + graph = import(user, FDC_ORDERS_URL) + open_orders = graph&.select do |o| + o.semanticType == "dfc-b:Order" && o.orderStatus[:path] == "Held" + end + + return if open_orders.blank? + + # If there are multiple open orders, we don't know which one to choose. + # We want the order we placed for the same distributor in the same order + # cycle before. So here are some assumptions for this to work: + # + # * We see only orders for our distributor. The endpoint URL contains the + # the distributor name and is currently hardcoded. + # * There's only one open order cycle at a time. Otherwise we may select + # an order of an old order cycle. + # * Orders are finalised when the order cycle closes. So _Held_ orders + # always belong to an open order cycle. + # * We see only our own orders. This assumption is wrong. The Shopify + # integration places held orders as well and they are visible to us. + # + # Unfortunately, the endpoint doesn't tell who placed the order. + # TODO: We need to remember the link to the order locally. + # Or the API is updated to include the orderer. + # + # For now, we just guess: + open_orders.last.tap do |order| + # The DFC Connector doesn't recognise status values properly yet. + # So we are overriding the value with something that can be exported. + order.orderStatus = "dfc-v:Held" + end + end + + def import(user, url) + api = DfcRequest.new(user) + json = api.call(url) + DfcIo.import(json) + end + + def send_order(ofn_order, backorder) + lines = backorder.lines + offers = lines.map(&:offer) + products = offers.map(&:offeredItem) + session = build_sale_session(ofn_order) + json = DfcIo.export(backorder, *lines, *offers, *products, session) + + api = DfcRequest.new(ofn_order.distributor.owner) + + if backorder.semanticId == FDC_NEW_ORDER_URL + # Create order via POST: + api.call(FDC_ORDERS_URL, json) + else + # Update existing: + api.call(backorder.semanticId, json) + end end end diff --git a/spec/fixtures/vcr_cassettes/FdcBackorderer/_find_or_build_order/finds_an_order_object.yml b/spec/fixtures/vcr_cassettes/FdcBackorderer/_find_or_build_order/finds_an_order_object.yml new file mode 100644 index 0000000000..8af5bd676d --- /dev/null +++ b/spec/fixtures/vcr_cassettes/FdcBackorderer/_find_or_build_order/finds_an_order_object.yml @@ -0,0 +1,199 @@ +--- +http_interactions: +- request: + method: get + uri: https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders + body: + encoding: US-ASCII + string: '' + headers: + Content-Type: + - application/json + Authorization: + - "" + User-Agent: + - Faraday v2.9.0 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 403 + message: Forbidden + headers: + Server: + - openresty + Date: + - Thu, 05 Sep 2024 03:42:46 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '78' + Connection: + - keep-alive + X-Powered-By: + - Express + Access-Control-Allow-Origin: + - "*" + Etag: + - W/"4e-vJeBLxgahmv23yP9gdPJW/woako" + Strict-Transport-Security: + - max-age=15811200 + body: + encoding: UTF-8 + string: '{"message":"User access denied - token missing","error":"User not authorized"}' + recorded_at: Thu, 05 Sep 2024 03:42:46 GMT +- request: + method: get + uri: https://login.lescommuns.org/auth/realms/data-food-consortium/.well-known/openid-configuration + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - SWD 2.0.3 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 05 Sep 2024 03:42:47 GMT + Content-Type: + - application/json;charset=UTF-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Vary: + - Accept-Encoding + Set-Cookie: + - AUTH_SESSION_ID=1725507768.852.28453.168|78230f584c0d7db97d376e98de5321dc; + Path=/; Secure; HttpOnly + Cache-Control: + - no-cache, must-revalidate, no-transform, no-store + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + body: + encoding: ASCII-8BIT + string: '{"issuer":"https://login.lescommuns.org/auth/realms/data-food-consortium","authorization_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/auth","token_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/token","introspection_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/token/introspect","userinfo_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/userinfo","end_session_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/logout","frontchannel_logout_session_supported":true,"frontchannel_logout_supported":true,"jwks_uri":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/certs","check_session_iframe":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/login-status-iframe.html","grant_types_supported":["authorization_code","implicit","refresh_token","password","client_credentials","urn:openid:params:grant-type:ciba","urn:ietf:params:oauth:grant-type:device_code"],"acr_values_supported":["0","1"],"response_types_supported":["code","none","id_token","token","id_token + token","code id_token","code token","code id_token token"],"subject_types_supported":["public","pairwise"],"id_token_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512"],"id_token_encryption_alg_values_supported":["RSA-OAEP","RSA-OAEP-256","RSA1_5"],"id_token_encryption_enc_values_supported":["A256GCM","A192GCM","A128GCM","A128CBC-HS256","A192CBC-HS384","A256CBC-HS512"],"userinfo_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512","none"],"userinfo_encryption_alg_values_supported":["RSA-OAEP","RSA-OAEP-256","RSA1_5"],"userinfo_encryption_enc_values_supported":["A256GCM","A192GCM","A128GCM","A128CBC-HS256","A192CBC-HS384","A256CBC-HS512"],"request_object_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512","none"],"request_object_encryption_alg_values_supported":["RSA-OAEP","RSA-OAEP-256","RSA1_5"],"request_object_encryption_enc_values_supported":["A256GCM","A192GCM","A128GCM","A128CBC-HS256","A192CBC-HS384","A256CBC-HS512"],"response_modes_supported":["query","fragment","form_post","query.jwt","fragment.jwt","form_post.jwt","jwt"],"registration_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/clients-registrations/openid-connect","token_endpoint_auth_methods_supported":["private_key_jwt","client_secret_basic","client_secret_post","tls_client_auth","client_secret_jwt"],"token_endpoint_auth_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512"],"introspection_endpoint_auth_methods_supported":["private_key_jwt","client_secret_basic","client_secret_post","tls_client_auth","client_secret_jwt"],"introspection_endpoint_auth_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512"],"authorization_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512"],"authorization_encryption_alg_values_supported":["RSA-OAEP","RSA-OAEP-256","RSA1_5"],"authorization_encryption_enc_values_supported":["A256GCM","A192GCM","A128GCM","A128CBC-HS256","A192CBC-HS384","A256CBC-HS512"],"claims_supported":["aud","sub","iss","auth_time","name","given_name","family_name","preferred_username","email","acr"],"claim_types_supported":["normal"],"claims_parameter_supported":true,"scopes_supported":["openid","microprofile-jwt","phone","roles","profile","email","address","web-origins","acr","offline_access"],"request_parameter_supported":true,"request_uri_parameter_supported":true,"require_request_uri_registration":true,"code_challenge_methods_supported":["plain","S256"],"tls_client_certificate_bound_access_tokens":true,"revocation_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/revoke","revocation_endpoint_auth_methods_supported":["private_key_jwt","client_secret_basic","client_secret_post","tls_client_auth","client_secret_jwt"],"revocation_endpoint_auth_signing_alg_values_supported":["PS384","ES384","RS384","HS256","HS512","ES256","RS256","HS384","ES512","PS256","PS512","RS512"],"backchannel_logout_supported":true,"backchannel_logout_session_supported":true,"device_authorization_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/auth/device","backchannel_token_delivery_modes_supported":["poll","ping"],"backchannel_authentication_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/ext/ciba/auth","backchannel_authentication_request_signing_alg_values_supported":["PS384","ES384","RS384","ES256","RS256","ES512","PS256","PS512","RS512"],"require_pushed_authorization_requests":false,"pushed_authorization_request_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/ext/par/request","mtls_endpoint_aliases":{"token_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/token","revocation_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/revoke","introspection_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/token/introspect","device_authorization_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/auth/device","registration_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/clients-registrations/openid-connect","userinfo_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/userinfo","pushed_authorization_request_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/ext/par/request","backchannel_authentication_endpoint":"https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/ext/ciba/auth"},"authorization_response_iss_parameter_supported":true}' + recorded_at: Thu, 05 Sep 2024 03:42:48 GMT +- request: + method: post + uri: https://login.lescommuns.org/auth/realms/data-food-consortium/protocol/openid-connect/token + body: + encoding: UTF-8 + string: grant_type=refresh_token&refresh_token= + headers: + User-Agent: + - Rack::OAuth2 (2.2.1) + Authorization: + - "" + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 05 Sep 2024 03:42:49 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Vary: + - Accept-Encoding + Set-Cookie: + - AUTH_SESSION_ID=1725507769.981.16671.25506|78230f584c0d7db97d376e98de5321dc; + Path=/; Secure; HttpOnly + Cache-Control: + - no-store + Pragma: + - no-cache + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + body: + encoding: ASCII-8BIT + string: '{"access_token":"","expires_in":1800,"refresh_expires_in":31535425,"refresh_token":"","token_type":"Bearer","id_token":"","not-before-policy":0,"session_state":"78c74723-7be8-4383-a612-ad9e0bd2ddf9","scope":"openid + profile email"}' + recorded_at: Thu, 05 Sep 2024 03:42:49 GMT +- request: + method: get + uri: https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders + body: + encoding: US-ASCII + string: '' + headers: + Content-Type: + - application/json + Authorization: + - "" + User-Agent: + - Faraday v2.9.0 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - openresty + Date: + - Thu, 05 Sep 2024 03:42:51 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '1266' + Connection: + - keep-alive + X-Powered-By: + - Express + Access-Control-Allow-Origin: + - "*" + Pageinfo: + - '{"hasPreviousPage":false,"hasNextPage":false,"startCursor":"eyJsYXN0X2lkIjoxMTQ5MTQ0NDk4NDgzLCJsYXN0X3ZhbHVlIjoxMTQ5MTQ0NDk4NDgzfQ==","endCursor":"eyJsYXN0X2lkIjoxMTc1MjAzMTg0OTQ3LCJsYXN0X3ZhbHVlIjoxMTc1MjAzMTg0OTQ3fQ=="}' + Etag: + - W/"4f2-+Z4Mv8gUlC55eOmBrmUXSM9VtOc" + Set-Cookie: + - SRVGROUP=common; path=/; HttpOnly + X-Resolver-Ip: + - 185.172.100.60 + Strict-Transport-Security: + - max-age=15811200 + body: + encoding: UTF-8 + string: '{"@context":"https://www.datafoodconsortium.org","@graph":[{"@id":"_:b7046","@type":"dfc-b:Price","dfc-b:hasUnit":"dfc-m:PoundSterling","dfc-b:value":"2.09"},{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Offers/44519466467635","@type":"dfc-b:Offer","dfc-b:offeredItem":{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635"}},{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders/1175203184947","@type":"dfc-b:Order","dfc-b:hasOrderStatus":{"@id":"dfc-v:Held"},"dfc-b:hasPart":{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders/1175203184947/orderLines/117"}},{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders/1175203184947/orderLines/117","@type":"dfc-b:OrderLine","dfc-b:concerns":{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Offers/44519466467635"},"dfc-b:hasPrice":{"@id":"_:b7046"},"dfc-b:quantity":"3"},{"@id":"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635","@type":"dfc-b:SuppliedProduct"}]}' + recorded_at: Thu, 05 Sep 2024 03:42:51 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/services/fdc_backorderer_spec.rb b/spec/services/fdc_backorderer_spec.rb index 3f5da299ac..fa3ed78409 100644 --- a/spec/services/fdc_backorderer_spec.rb +++ b/spec/services/fdc_backorderer_spec.rb @@ -4,13 +4,35 @@ require 'spec_helper' RSpec.describe FdcBackorderer do let(:order) { create(:completed_order_with_totals) } + let(:account) { + OidcAccount.new( + uid: "testdfc@protonmail.com", + refresh_token: ENV.fetch("OPENID_REFRESH_TOKEN"), + updated_at: 1.day.ago, + ) + } + + before do + order.distributor.owner.oidc_account = account + end describe "#find_or_build_order" do it "builds an order object" do + account.updated_at = Time.zone.now + stub_request(:get, FdcBackorderer::FDC_ORDERS_URL) + .to_return(status: 200, body: "{}") + backorder = subject.find_or_build_order(order) expect(backorder.semanticId).to match %r{^https.*/\#$} expect(backorder.lines).to eq [] end + + it "finds an order object", vcr: true do + backorder = subject.find_or_build_order(order) + + expect(backorder.semanticId).to match %r{^https.*/[0-9]+$} + expect(backorder.lines.count).to eq 1 + end end end