Skip to content
This repository has been archived by the owner. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions app/lib/forms/resources/upsert_ledger_resources.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/lib/forms/sync_resources/upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def save
private

def upsert_sync_resource
sync_resource.save!
success(sync_resource)
end
end
Expand Down
9 changes: 5 additions & 4 deletions app/lib/forms/syncs/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions app/lib/forms/syncs/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions app/models/sync_ledger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
30 changes: 0 additions & 30 deletions spec/lib/forms/resources/upsert_ledger_resources_spec.rb

This file was deleted.

8 changes: 5 additions & 3 deletions spec/lib/forms/syncs/perform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/forms/syncs/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/lib/forms/syncs/upsert_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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