diff --git a/app/lib/forms/resources/upsert_ledger_resources.rb b/app/lib/forms/resources/upsert_ledger_resources.rb deleted file mode 100644 index 5c06753..0000000 --- a/app/lib/forms/resources/upsert_ledger_resources.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -module Forms - module Resources - class UpsertLedgerResources - include Formify::Form - - attr_accessor :resource - - validates_presence_of :resource - - def save - with_advisory_lock_transaction(:foo) do - validate_or_fail - .and_then { upsert_ledger_resource_resource } - .and_then { success(resource) } - end - end - - private - - delegate :organization, - to: :resource - - delegate :ledgers, - to: :organization - - def upsert_ledger_resource_resource - ledgers.inject(success) do |result, ledger| - result.and_then do - Forms::LedgerResources::Upsert.new( - ledger: ledger, - resource: resource - ).save - end - end - - success(resource) - end - end - end -end diff --git a/app/lib/forms/sync_resources/upsert.rb b/app/lib/forms/sync_resources/upsert.rb index b563ae6..04d3ed0 100644 --- a/app/lib/forms/sync_resources/upsert.rb +++ b/app/lib/forms/sync_resources/upsert.rb @@ -35,6 +35,7 @@ def save private def upsert_sync_resource + sync_resource.save! success(sync_resource) end end diff --git a/app/lib/forms/syncs/create.rb b/app/lib/forms/syncs/create.rb index 779eb20..9165ba9 100644 --- a/app/lib/forms/syncs/create.rb +++ b/app/lib/forms/syncs/create.rb @@ -32,13 +32,14 @@ class Create end def save - with_advisory_lock_transaction(:syncs, organization, :create) do + result = with_advisory_lock_transaction(:syncs, organization, :create) do validate_or_fail .and_then { create_sync } .and_then { emit } - .and_then { schedule_upsert_references } - .and_then { success(sync) } end + + result.and_then { schedule_upsert_references } + .and_then { success(sync) } end private @@ -69,7 +70,7 @@ def validate_organization return if organization_external_id.blank? return if organization.present? - errors.add(:base, :test) + errors.add(:organization_external_id, 'is not a valid organization external ID.') end end end diff --git a/app/lib/forms/syncs/setup.rb b/app/lib/forms/syncs/setup.rb index 35fd734..ba7aa17 100644 --- a/app/lib/forms/syncs/setup.rb +++ b/app/lib/forms/syncs/setup.rb @@ -10,15 +10,17 @@ class Setup validates_presence_of :sync def save - with_advisory_lock_transaction(:syncs, sync) do + result = with_advisory_lock_transaction(:syncs, sync) do validate_or_fail .and_then { destroy_sync_resources(sync) } .and_then { |sync| upsert_resources(sync) } .and_then { |sync| set_sync_resource(sync) } .and_then { |sync| upsert_sync_ledgers(sync) } - .and_then { |sync| perform_sync(sync) } .and_then { success(sync) } end + + result.and_then { |sync| schedule_perform_sync(sync) } + .and_then { success(sync) } end private @@ -29,10 +31,9 @@ def destroy_sync_resources(sync) .save end - def perform_sync(sync) - Forms::Syncs::Perform - .new(sync: sync) - .save + def schedule_perform_sync(sync) + SyncJobs::Perform.perform_async(sync.id) + success(sync) end def set_sync_resource(sync) @@ -47,9 +48,11 @@ def set_sync_resource(sync) end def upsert_resources(sync) - Forms::Syncs::UpsertResources - .new(sync: sync) - .save + result = Forms::Syncs::UpsertResources + .new(sync: sync) + .save + + result end def upsert_sync_ledgers(sync) diff --git a/app/models/sync_ledger.rb b/app/models/sync_ledger.rb index 6e62327..8af9981 100644 --- a/app/models/sync_ledger.rb +++ b/app/models/sync_ledger.rb @@ -59,9 +59,9 @@ def adaptor def resources_data ret = {} - ledger_resources.find_each do |ledger_resource| - resource = ledger_resource.resource - sync_resource = sync_resources.find_by!(sync: sync, resource: resource) + sync_resources.find_each do |sync_resource| + resource = sync_resource.resource + ledger_resource = ledger_resources.find_by!(ledger: ledger, resource: resource) ret[resource.type] ||= {} ret[resource.type][resource.external_id] = { diff --git a/spec/lib/forms/resources/upsert_ledger_resources_spec.rb b/spec/lib/forms/resources/upsert_ledger_resources_spec.rb deleted file mode 100644 index 1b7aa9d..0000000 --- a/spec/lib/forms/resources/upsert_ledger_resources_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' -require 'formify/spec_helpers' - -describe Forms::Resources::UpsertLedgerResources, type: :form do - include Formify::SpecHelpers - - let(:resource) { FactoryBot.create(:resource) } - let(:ledger) { FactoryBot.create(:ledger) } - let(:attributes) do - { - resource: resource - } - end - - it { expect_valid } - it { expect(result).to be_success } - it { expect(value).to be_a(Resource) } - - describe '#resource' do - it { expect_error_with_missing_attribute(:resource) } - end - - context 'with two ledgers' do - before { FactoryBot.create_list(:ledger, 2) } - - it { expect { value }.to change(LedgerResource, :count).from(0).to(2) } - end -end diff --git a/spec/lib/forms/syncs/perform_spec.rb b/spec/lib/forms/syncs/perform_spec.rb index 550a933..dd1a6a8 100644 --- a/spec/lib/forms/syncs/perform_spec.rb +++ b/spec/lib/forms/syncs/perform_spec.rb @@ -22,7 +22,11 @@ end context 'when ledger' do - before { sync_ledger } + before do + # resource = FactoryBot.create(:resource, external_id: sync.resource_external_id, type: sync.resource_type) + # FactoryBot.create(:ledger_resource, ledger: ledger, resource: resource) + Forms::Syncs::Setup.new(sync: sync_ledger.sync).save + end it { expect_valid } it { expect(result).to be_success } @@ -34,8 +38,6 @@ it do sync.update!(without_create_confirmation: true) - resource = FactoryBot.create(:resource, external_id: sync.resource_external_id, type: sync.resource_type) - FactoryBot.create(:ledger_resource, ledger: ledger, resource: resource) expect(value.reload).to be_succeeded end end diff --git a/spec/lib/forms/syncs/setup_spec.rb b/spec/lib/forms/syncs/setup_spec.rb index 610b258..6626a8f 100644 --- a/spec/lib/forms/syncs/setup_spec.rb +++ b/spec/lib/forms/syncs/setup_spec.rb @@ -4,6 +4,7 @@ require 'formify/spec_helpers' describe Forms::Syncs::Setup, type: :form do + include ActiveJob::TestHelper include Formify::SpecHelpers let(:ledger) { FactoryBot.create(:ledger) } @@ -19,6 +20,31 @@ it { expect(value).to be_a(Sync) } it { expect { value }.to change(SyncLedger, :count).from(0).to(1) } + it do + external_id = '928db55e-6552-4aaf-96d7-10c693922b1f' + + sync.update!( + operation_method: 'upsert', + resource_type: :customer, + without_create_confirmation: true, + resource_external_id: external_id, + references: { + 'customer' => { + external_id => { + 'data' => { + 'name' => 'John Smith', + 'email' => 'renato_powlowski@oconner.com' + } + } + } + } + ) + + perform_enqueued_jobs do + expect(value.resources.first.external_id).to eq(external_id) + end + end + describe '#sync' do it { expect_error_with_missing_attribute(:sync) } end diff --git a/spec/lib/forms/syncs/upsert_resources_spec.rb b/spec/lib/forms/syncs/upsert_resources_spec.rb index dd749ac..392f78a 100644 --- a/spec/lib/forms/syncs/upsert_resources_spec.rb +++ b/spec/lib/forms/syncs/upsert_resources_spec.rb @@ -17,5 +17,6 @@ it { expect_valid } it { expect(result).to be_success } it { expect(value).to be_a(Sync) } + it { expect { value }.to change(Resource, :count).from(0).to(1) } it { expect { value }.to change(SyncResource, :count).from(0).to(1) } end