From 4f2c091766e70e67ff4d00173145f41c553a2a26 Mon Sep 17 00:00:00 2001 From: bedrock-adam Date: Sat, 28 Dec 2024 10:04:33 +1100 Subject: [PATCH 1/5] update generate job specs --- .../accountify/invoice_status_summary.rb | 10 ++- .../invoice_status_summary/generate_job.rb | 3 +- .../accountify/organisation/created_job.rb | 8 +-- .../organisation/created_job_spec.rb | 3 +- .../invoice_status_summary/generate_spec.rb | 64 +++++-------------- 5 files changed, 28 insertions(+), 60 deletions(-) diff --git a/app/domains/accountify/invoice_status_summary.rb b/app/domains/accountify/invoice_status_summary.rb index 905e0e8..66b6b61 100644 --- a/app/domains/accountify/invoice_status_summary.rb +++ b/app/domains/accountify/invoice_status_summary.rb @@ -2,11 +2,19 @@ module Accountify module InvoiceStatusSummary extend self - def generate(tenant_id:, organisation_id:, time: ::Time) + def generate(event_id:, time: ::Time) + tenant_id = nil + organisation_id = nil + current_utc_time = time.now.utc ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.transaction(isolation: :repeatable_read) do + event = Models::Event.find(event_id) + + tenant_id = event.tenant_id + organisation_id = event.body['organisation']['id'] + grouped_invoices = Models::Invoice .where(tenant_id: tenant_id, organisation_id: organisation_id).group(:status) .count diff --git a/app/jobs/accountify/invoice_status_summary/generate_job.rb b/app/jobs/accountify/invoice_status_summary/generate_job.rb index 6e89f7d..22e727a 100644 --- a/app/jobs/accountify/invoice_status_summary/generate_job.rb +++ b/app/jobs/accountify/invoice_status_summary/generate_job.rb @@ -6,8 +6,7 @@ class GenerateJob sidekiq_options queue: 'reporting', backtrace: true def perform(args) - InvoiceStatusSummary.generate( - tenant_id: args['tenant_id'], organisation_id: args['organisation_id']) + InvoiceStatusSummary.generate(event_id: args['event_id']) end end end diff --git a/app/jobs/accountify/organisation/created_job.rb b/app/jobs/accountify/organisation/created_job.rb index 4acb2a7..4dae0a7 100644 --- a/app/jobs/accountify/organisation/created_job.rb +++ b/app/jobs/accountify/organisation/created_job.rb @@ -6,13 +6,7 @@ class CreatedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Organisation::CreatedEvent.find(args['id']) - end - - InvoiceStatusSummary::GenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'] }) + InvoiceStatusSummary::GenerateJob.perform_async({ 'event_id' => args['id'] }) end end end diff --git a/spec/jobs/accountify/organisation/created_job_spec.rb b/spec/jobs/accountify/organisation/created_job_spec.rb index 07c881d..667c713 100644 --- a/spec/jobs/accountify/organisation/created_job_spec.rb +++ b/spec/jobs/accountify/organisation/created_job_spec.rb @@ -41,8 +41,7 @@ module Organisation hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/lib/accountify/invoice_status_summary/generate_spec.rb b/spec/lib/accountify/invoice_status_summary/generate_spec.rb index 4563d46..474177a 100644 --- a/spec/lib/accountify/invoice_status_summary/generate_spec.rb +++ b/spec/lib/accountify/invoice_status_summary/generate_spec.rb @@ -3,71 +3,39 @@ module Accountify RSpec.describe InvoiceStatusSummary do describe '.generate' do - let(:tenant_id) { 1 } - let(:organisation) { create(:accountify_organisation) } - let(:organisation_id) { organisation.id } - let(:current_utc_time) { ::Time.now.utc } let(:time) { double('Time', now: double('Time', utc: current_utc_time)) } - let(:contact) do - create(:accountify_contact, - tenant_id: tenant_id, - organisation_id: organisation.id) - end - - let!(:drafted_invoice) do - create(:accountify_invoice, - tenant_id: tenant_id, - organisation_id: organisation_id, - contact_id: contact.id, - status: Invoice::Status::DRAFTED) - end - - let!(:issued_invoice) do - create(:accountify_invoice, - tenant_id: tenant_id, - organisation_id: organisation_id, - contact_id: contact.id, - status: Invoice::Status::ISSUED) - end - - let!(:paid_invoice) do - create(:accountify_invoice, - tenant_id: tenant_id, - organisation_id: organisation_id, - contact_id: contact.id, - status: Invoice::Status::PAID) - end + let(:user_id) { 1 } + let(:tenant_id) { 2 } + let(:organisation) { create(:accountify_organisation) } - let!(:voided_invoice) do - create(:accountify_invoice, + let(:event) do + create( + :accountify_organisation_created_event, + user_id: user_id, tenant_id: tenant_id, - organisation_id: organisation_id, - contact_id: contact.id, - status: Invoice::Status::VOIDED) + eventable: organisation, + body: { 'organisation' => { 'id' => organisation.id } }) end it 'creates a new invoice status summary' do expect do - InvoiceStatusSummary.generate( - tenant_id: tenant_id, organisation_id: organisation_id) + InvoiceStatusSummary.generate(event_id: event.id) end.to change { Models::InvoiceStatusSummary.count }.by(1) end it 'creates a summary with the correct counts' do - summary = InvoiceStatusSummary.generate( - tenant_id: tenant_id, organisation_id: organisation_id) + summary = InvoiceStatusSummary.generate(event_id: event.id) - expect(summary[:drafted_count]).to eq(1) - expect(summary[:issued_count]).to eq(1) - expect(summary[:paid_count]).to eq(1) - expect(summary[:voided_count]).to eq(1) + expect(summary[:drafted_count]).to eq(0) + expect(summary[:issued_count]).to eq(0) + expect(summary[:paid_count]).to eq(0) + expect(summary[:voided_count]).to eq(0) end it 'uses the current time as the generated_at time' do - summary = InvoiceStatusSummary.generate( - tenant_id: tenant_id, organisation_id: organisation_id, time: time) + summary = InvoiceStatusSummary.generate(event_id: event.id, time: time) expect(summary[:generated_at]).to be_within(1.second).of(current_utc_time) end From 6beae13df888477ebb6f0093fef308e22bcecfe8 Mon Sep 17 00:00:00 2001 From: bedrock-adam Date: Sat, 28 Dec 2024 11:32:28 +1100 Subject: [PATCH 2/5] update specs --- .../accountify/invoice_status_summary.rb | 11 ++++++-- app/jobs/accountify/invoice/deleted_job.rb | 9 +------ app/jobs/accountify/invoice/drafted_job.rb | 9 +------ app/jobs/accountify/invoice/issued_job.rb | 9 +------ app/jobs/accountify/invoice/paid_job.rb | 9 +------ app/jobs/accountify/invoice/updated_job.rb | 9 +------ app/jobs/accountify/invoice/voided_job.rb | 9 +------ .../invoice_status_summary/regenerate_job.rb | 5 +--- .../accountify/organisation/created_job.rb | 2 +- .../accountify/invoice/deleted_job_spec.rb | 10 +++---- .../accountify/invoice/drafted_job_spec.rb | 10 +++---- .../accountify/invoice/issued_job_spec.rb | 10 +++---- spec/jobs/accountify/invoice/paid_job_spec.rb | 10 +++---- .../accountify/invoice/updated_job_spec.rb | 10 +++---- .../accountify/invoice/voided_job_spec.rb | 10 +++---- .../organisation/created_job_spec.rb | 16 +++--------- .../invoice_status_summary/regenerate_spec.rb | 26 +++++++++++-------- 17 files changed, 59 insertions(+), 115 deletions(-) diff --git a/app/domains/accountify/invoice_status_summary.rb b/app/domains/accountify/invoice_status_summary.rb index 66b6b61..d87606c 100644 --- a/app/domains/accountify/invoice_status_summary.rb +++ b/app/domains/accountify/invoice_status_summary.rb @@ -33,12 +33,19 @@ def generate(event_id:, time: ::Time) find_by_organisation_id(tenant_id: tenant_id, organisation_id: organisation_id) end - def regenerate(tenant_id:, organisation_id:, - invoice_updated_at: ::Time.now.utc, time: ::Time) + def regenerate(event_id:, invoice_updated_at: ::Time.now.utc, time: ::Time) + tenant_id = nil + organisation_id = nil + current_utc_time = time.now.utc ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.transaction(isolation: :repeatable_read) do + event = Models::Event.find(event_id) + + tenant_id = event.tenant_id + organisation_id = event.body['organisation']['id'] + summary = Models::InvoiceStatusSummary .where('generated_at <= ?', invoice_updated_at) .lock('FOR UPDATE NOWAIT') diff --git a/app/jobs/accountify/invoice/deleted_job.rb b/app/jobs/accountify/invoice/deleted_job.rb index 809a841..9d072b4 100644 --- a/app/jobs/accountify/invoice/deleted_job.rb +++ b/app/jobs/accountify/invoice/deleted_job.rb @@ -6,14 +6,7 @@ class DeletedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::DeletedEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice/drafted_job.rb b/app/jobs/accountify/invoice/drafted_job.rb index 22e88a1..2d02e68 100644 --- a/app/jobs/accountify/invoice/drafted_job.rb +++ b/app/jobs/accountify/invoice/drafted_job.rb @@ -6,14 +6,7 @@ class DraftedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::DraftedEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice/issued_job.rb b/app/jobs/accountify/invoice/issued_job.rb index 336f6f2..daf97cc 100644 --- a/app/jobs/accountify/invoice/issued_job.rb +++ b/app/jobs/accountify/invoice/issued_job.rb @@ -6,14 +6,7 @@ class IssuedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::IssuedEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice/paid_job.rb b/app/jobs/accountify/invoice/paid_job.rb index 39ee08e..c510aaf 100644 --- a/app/jobs/accountify/invoice/paid_job.rb +++ b/app/jobs/accountify/invoice/paid_job.rb @@ -6,14 +6,7 @@ class PaidJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::PaidEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice/updated_job.rb b/app/jobs/accountify/invoice/updated_job.rb index 8604331..2910362 100644 --- a/app/jobs/accountify/invoice/updated_job.rb +++ b/app/jobs/accountify/invoice/updated_job.rb @@ -6,14 +6,7 @@ class UpdatedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::UpdatedEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice/voided_job.rb b/app/jobs/accountify/invoice/voided_job.rb index b059668..4f371b9 100644 --- a/app/jobs/accountify/invoice/voided_job.rb +++ b/app/jobs/accountify/invoice/voided_job.rb @@ -6,14 +6,7 @@ class VoidedJob sidekiq_options retry: false, backtrace: true def perform(args) - event = ActiveRecord::Base.connection_pool.with_connection do - Models::Invoice::VoidedEvent.find(args['id']) - end - - InvoiceStatusSummary::RegenerateJob.perform_async({ - 'tenant_id' => event.tenant_id, - 'organisation_id' => event.body['organisation']['id'], - 'invoice_updated_at' => event.created_at.utc.iso8601 }) + InvoiceStatusSummary::RegenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/app/jobs/accountify/invoice_status_summary/regenerate_job.rb b/app/jobs/accountify/invoice_status_summary/regenerate_job.rb index 5c01cd0..328436c 100644 --- a/app/jobs/accountify/invoice_status_summary/regenerate_job.rb +++ b/app/jobs/accountify/invoice_status_summary/regenerate_job.rb @@ -6,10 +6,7 @@ class RegenerateJob sidekiq_options queue: 'reporting', backtrace: true def perform(args) - InvoiceStatusSummary.regenerate( - tenant_id: args['tenant_id'], - organisation_id: args['organisation_id'], - invoice_updated_at: args['invoice_updated_at']) + InvoiceStatusSummary.regenerate(event_id: args['event_id']) rescue NotAvailable RegenerateJob.perform_in(1.minute, args) end diff --git a/app/jobs/accountify/organisation/created_job.rb b/app/jobs/accountify/organisation/created_job.rb index 4dae0a7..c4243c2 100644 --- a/app/jobs/accountify/organisation/created_job.rb +++ b/app/jobs/accountify/organisation/created_job.rb @@ -6,7 +6,7 @@ class CreatedJob sidekiq_options retry: false, backtrace: true def perform(args) - InvoiceStatusSummary::GenerateJob.perform_async({ 'event_id' => args['id'] }) + InvoiceStatusSummary::GenerateJob.perform_async({ 'event_id' => args['event_id'] }) end end end diff --git a/spec/jobs/accountify/invoice/deleted_job_spec.rb b/spec/jobs/accountify/invoice/deleted_job_spec.rb index 7a12bdb..2424e6d 100644 --- a/spec/jobs/accountify/invoice/deleted_job_spec.rb +++ b/spec/jobs/accountify/invoice/deleted_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe DeletedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - DeletedJob.new.perform({ 'id' => event.id }) + DeletedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/invoice/drafted_job_spec.rb b/spec/jobs/accountify/invoice/drafted_job_spec.rb index 940e963..77e1eda 100644 --- a/spec/jobs/accountify/invoice/drafted_job_spec.rb +++ b/spec/jobs/accountify/invoice/drafted_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe DraftedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - DraftedJob.new.perform({ 'id' => event.id }) + DraftedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/invoice/issued_job_spec.rb b/spec/jobs/accountify/invoice/issued_job_spec.rb index 9d4b507..f3aaba0 100644 --- a/spec/jobs/accountify/invoice/issued_job_spec.rb +++ b/spec/jobs/accountify/invoice/issued_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe IssuedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - IssuedJob.new.perform({ 'id' => event.id }) + IssuedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/invoice/paid_job_spec.rb b/spec/jobs/accountify/invoice/paid_job_spec.rb index 8aaafad..93cc275 100644 --- a/spec/jobs/accountify/invoice/paid_job_spec.rb +++ b/spec/jobs/accountify/invoice/paid_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe PaidJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - PaidJob.new.perform({ 'id' => event.id }) + PaidJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/invoice/updated_job_spec.rb b/spec/jobs/accountify/invoice/updated_job_spec.rb index d7e1938..a1eb56b 100644 --- a/spec/jobs/accountify/invoice/updated_job_spec.rb +++ b/spec/jobs/accountify/invoice/updated_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe UpdatedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - UpdatedJob.new.perform({ 'id' => event.id }) + UpdatedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/invoice/voided_job_spec.rb b/spec/jobs/accountify/invoice/voided_job_spec.rb index 2dfdb59..763ac67 100644 --- a/spec/jobs/accountify/invoice/voided_job_spec.rb +++ b/spec/jobs/accountify/invoice/voided_job_spec.rb @@ -4,11 +4,11 @@ module Accountify module Invoice RSpec.describe VoidedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end @@ -36,7 +36,7 @@ module Invoice end before do - VoidedJob.new.perform({ 'id' => event.id }) + VoidedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do @@ -44,9 +44,7 @@ module Invoice hash_including( 'args' => [ hash_including( - 'tenant_id' => tenant_id, - 'organisation_id' => accountify_organisation.id, - 'invoice_updated_at' => event.created_at.utc.iso8601 )])]) + 'event_id' => event.id )])]) end end end diff --git a/spec/jobs/accountify/organisation/created_job_spec.rb b/spec/jobs/accountify/organisation/created_job_spec.rb index 667c713..5248d53 100644 --- a/spec/jobs/accountify/organisation/created_job_spec.rb +++ b/spec/jobs/accountify/organisation/created_job_spec.rb @@ -3,25 +3,15 @@ module Accountify module Organisation RSpec.describe CreatedJob, type: :job do + let(:current_time) { Time.now } + let(:user_id) { 123 } let(:tenant_id) { 456 } - let(:current_time) { Time.now } - let(:accountify_organisation) do create(:accountify_organisation, tenant_id: tenant_id) end - let(:accountify_contact) do - create(:accountify_contact, - tenant_id: tenant_id, organisation_id: organisation.id) - end - - let(:accountify_invoice) do - create(:accountify_invoice, - tenant_id: tenant_id, organisation_id: organisation.id, contact_id: contact.id) - end - describe 'when Accountify::Models::Organisation::CreatedEvent' do let(:event) do create( @@ -33,7 +23,7 @@ module Organisation end before do - CreatedJob.new.perform({ 'id' => event.id }) + CreatedJob.new.perform({ 'event_id' => event.id }) end it 'performs Accountify::InvoiceStatusSummary::GenerateJob async' do diff --git a/spec/lib/accountify/invoice_status_summary/regenerate_spec.rb b/spec/lib/accountify/invoice_status_summary/regenerate_spec.rb index 91955ad..4c1d345 100644 --- a/spec/lib/accountify/invoice_status_summary/regenerate_spec.rb +++ b/spec/lib/accountify/invoice_status_summary/regenerate_spec.rb @@ -3,6 +3,7 @@ module Accountify RSpec.describe InvoiceStatusSummary do describe '.regenerate' do + let(:user_id) { 1 } let(:tenant_id) { 1 } let(:organisation) { create(:accountify_organisation) } let(:organisation_id) { organisation.id } @@ -48,6 +49,16 @@ module Accountify status: Invoice::Status::VOIDED) end + let(:event) do + create( + :accountify_invoice_voided_event, + user_id: user_id, + tenant_id: tenant_id, + eventable: organisation, + created_at: invoice_updated_at, + body: { 'organisation' => { 'id' => organisation_id } }) + end + let!(:invoice_status_summary) do create(:accountify_invoice_status_summary, tenant_id: tenant_id, @@ -61,11 +72,7 @@ module Accountify it 'updates the existing invoice status summary' do expect do - InvoiceStatusSummary.regenerate( - tenant_id: tenant_id, - organisation_id: organisation_id, - invoice_updated_at: invoice_updated_at, - time: time) + InvoiceStatusSummary.regenerate(event_id: event.id, time: time) end.to change { Models::InvoiceStatusSummary.count }.by(0) summary = Models::InvoiceStatusSummary.find(invoice_status_summary.id) @@ -78,8 +85,7 @@ module Accountify it 'returns the summary if not updated' do summary = InvoiceStatusSummary.regenerate( - tenant_id: tenant_id, - organisation_id: organisation_id, + event_id: event.id, invoice_updated_at: current_utc_time - 2.days, time: time) @@ -92,8 +98,7 @@ module Accountify expect do InvoiceStatusSummary.regenerate( - tenant_id: tenant_id, - organisation_id: organisation_id, + event_id: event.id, invoice_updated_at: invoice_updated_at, time: time) end.to raise_error(Accountify::NotAvailable) @@ -105,8 +110,7 @@ module Accountify expect do InvoiceStatusSummary.regenerate( - tenant_id: tenant_id, - organisation_id: organisation_id, + event_id: event.id, invoice_updated_at: invoice_updated_at, time: time) end.to raise_error(Accountify::NotFound) From fc5b890f52fa684b891679d6ca8753b503b47b20 Mon Sep 17 00:00:00 2001 From: bedrock-adam Date: Sat, 28 Dec 2024 11:58:38 +1100 Subject: [PATCH 3/5] fix build --- app/domains/accountify/invoice_status_summary.rb | 4 ++-- app/domains/accountify/models/contact.rb | 2 +- app/domains/accountify/models/contact/created_event.rb | 2 +- app/domains/accountify/models/contact/deleted_event.rb | 2 +- app/domains/accountify/models/contact/updated_event.rb | 2 +- app/domains/accountify/models/event.rb | 7 ------- app/domains/accountify/models/invoice.rb | 2 +- app/domains/accountify/models/invoice/deleted_event.rb | 2 +- app/domains/accountify/models/invoice/drafted_event.rb | 2 +- app/domains/accountify/models/invoice/issued_event.rb | 2 +- app/domains/accountify/models/invoice/paid_event.rb | 2 +- app/domains/accountify/models/invoice/updated_event.rb | 2 +- app/domains/accountify/models/invoice/voided_event.rb | 2 +- app/domains/accountify/models/organisation.rb | 2 +- .../accountify/models/organisation/created_event.rb | 2 +- .../accountify/models/organisation/deleted_event.rb | 2 +- .../accountify/models/organisation/updated_event.rb | 2 +- 17 files changed, 17 insertions(+), 24 deletions(-) delete mode 100644 app/domains/accountify/models/event.rb diff --git a/app/domains/accountify/invoice_status_summary.rb b/app/domains/accountify/invoice_status_summary.rb index d87606c..4240011 100644 --- a/app/domains/accountify/invoice_status_summary.rb +++ b/app/domains/accountify/invoice_status_summary.rb @@ -10,7 +10,7 @@ def generate(event_id:, time: ::Time) ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.transaction(isolation: :repeatable_read) do - event = Models::Event.find(event_id) + event = ::Models::Event.find(event_id) tenant_id = event.tenant_id organisation_id = event.body['organisation']['id'] @@ -41,7 +41,7 @@ def regenerate(event_id:, invoice_updated_at: ::Time.now.utc, time: ::Time) ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.transaction(isolation: :repeatable_read) do - event = Models::Event.find(event_id) + event = ::Models::Event.find(event_id) tenant_id = event.tenant_id organisation_id = event.body['organisation']['id'] diff --git a/app/domains/accountify/models/contact.rb b/app/domains/accountify/models/contact.rb index 1727cec..022769a 100644 --- a/app/domains/accountify/models/contact.rb +++ b/app/domains/accountify/models/contact.rb @@ -5,7 +5,7 @@ class Contact < ActiveRecord::Base validates :organisation_id, presence: true - has_many :events, -> { order(created_at: :asc) }, as: :eventable + has_many :events, -> { order(created_at: :asc) }, as: :eventable, class_name: '::Models::Event' end end end diff --git a/app/domains/accountify/models/contact/created_event.rb b/app/domains/accountify/models/contact/created_event.rb index 9364833..3ce6d5b 100644 --- a/app/domains/accountify/models/contact/created_event.rb +++ b/app/domains/accountify/models/contact/created_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Contact - class CreatedEvent < Models::Event + class CreatedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/contact/deleted_event.rb b/app/domains/accountify/models/contact/deleted_event.rb index 4879887..e18bc8c 100644 --- a/app/domains/accountify/models/contact/deleted_event.rb +++ b/app/domains/accountify/models/contact/deleted_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Contact - class DeletedEvent < Models::Event + class DeletedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/contact/updated_event.rb b/app/domains/accountify/models/contact/updated_event.rb index 73c745d..6a0114d 100644 --- a/app/domains/accountify/models/contact/updated_event.rb +++ b/app/domains/accountify/models/contact/updated_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Contact - class UpdatedEvent < Models::Event + class UpdatedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/event.rb b/app/domains/accountify/models/event.rb deleted file mode 100644 index d75a4d2..0000000 --- a/app/domains/accountify/models/event.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Accountify - module Models - class Event < ::Models::Event - - end - end -end diff --git a/app/domains/accountify/models/invoice.rb b/app/domains/accountify/models/invoice.rb index d0a3356..e6ba0ad 100644 --- a/app/domains/accountify/models/invoice.rb +++ b/app/domains/accountify/models/invoice.rb @@ -7,7 +7,7 @@ class Invoice < ActiveRecord::Base has_many :line_items, -> { order(id: :asc) } - has_many :events, -> { order(created_at: :asc) }, as: :eventable + has_many :events, -> { order(created_at: :asc) }, as: :eventable, class_name: '::Models::Event' has_one :invoice_status_summary diff --git a/app/domains/accountify/models/invoice/deleted_event.rb b/app/domains/accountify/models/invoice/deleted_event.rb index 1f1cfdf..7d0ccee 100644 --- a/app/domains/accountify/models/invoice/deleted_event.rb +++ b/app/domains/accountify/models/invoice/deleted_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class DeletedEvent < Models::Event + class DeletedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/invoice/drafted_event.rb b/app/domains/accountify/models/invoice/drafted_event.rb index 61e5b1d..07cd494 100644 --- a/app/domains/accountify/models/invoice/drafted_event.rb +++ b/app/domains/accountify/models/invoice/drafted_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class DraftedEvent < Models::Event + class DraftedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/invoice/issued_event.rb b/app/domains/accountify/models/invoice/issued_event.rb index 9e29489..feb5602 100644 --- a/app/domains/accountify/models/invoice/issued_event.rb +++ b/app/domains/accountify/models/invoice/issued_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class IssuedEvent < Models::Event + class IssuedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/invoice/paid_event.rb b/app/domains/accountify/models/invoice/paid_event.rb index 66507f4..8a4b01b 100644 --- a/app/domains/accountify/models/invoice/paid_event.rb +++ b/app/domains/accountify/models/invoice/paid_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class PaidEvent < Models::Event + class PaidEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/invoice/updated_event.rb b/app/domains/accountify/models/invoice/updated_event.rb index 6bc3b5b..051525a 100644 --- a/app/domains/accountify/models/invoice/updated_event.rb +++ b/app/domains/accountify/models/invoice/updated_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class UpdatedEvent < Models::Event + class UpdatedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/invoice/voided_event.rb b/app/domains/accountify/models/invoice/voided_event.rb index 18c4f48..799e1fe 100644 --- a/app/domains/accountify/models/invoice/voided_event.rb +++ b/app/domains/accountify/models/invoice/voided_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Invoice - class VoidedEvent < Models::Event + class VoidedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/organisation.rb b/app/domains/accountify/models/organisation.rb index 0e687a5..bf1b4dc 100644 --- a/app/domains/accountify/models/organisation.rb +++ b/app/domains/accountify/models/organisation.rb @@ -3,7 +3,7 @@ module Models class Organisation < ActiveRecord::Base self.table_name = 'accountify_organisations' - has_many :events, -> { order(created_at: :asc) }, as: :eventable, class_name: 'Models::Event' + has_many :events, -> { order(created_at: :asc) }, as: :eventable, class_name: '::Models::Event' has_one :invoice_status_summary end diff --git a/app/domains/accountify/models/organisation/created_event.rb b/app/domains/accountify/models/organisation/created_event.rb index b0b28c0..1c498bd 100644 --- a/app/domains/accountify/models/organisation/created_event.rb +++ b/app/domains/accountify/models/organisation/created_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Organisation - class CreatedEvent < Models::Event + class CreatedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/organisation/deleted_event.rb b/app/domains/accountify/models/organisation/deleted_event.rb index 15fe82e..ccb2cac 100644 --- a/app/domains/accountify/models/organisation/deleted_event.rb +++ b/app/domains/accountify/models/organisation/deleted_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Organisation - class DeletedEvent < Models::Event + class DeletedEvent < ::Models::Event end end diff --git a/app/domains/accountify/models/organisation/updated_event.rb b/app/domains/accountify/models/organisation/updated_event.rb index 84a230c..40d61ec 100644 --- a/app/domains/accountify/models/organisation/updated_event.rb +++ b/app/domains/accountify/models/organisation/updated_event.rb @@ -1,7 +1,7 @@ module Accountify module Models class Organisation - class UpdatedEvent < Models::Event + class UpdatedEvent < ::Models::Event end end From 8242c6931e04366dc9bab0067feddf87eed9c440 Mon Sep 17 00:00:00 2001 From: bedrock-adam Date: Sat, 28 Dec 2024 12:01:56 +1100 Subject: [PATCH 4/5] move invoice line item entity --- app/domains/accountify/models/invoice.rb | 2 -- app/domains/accountify/models/invoice/line_item.rb | 9 +++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 app/domains/accountify/models/invoice/line_item.rb diff --git a/app/domains/accountify/models/invoice.rb b/app/domains/accountify/models/invoice.rb index e6ba0ad..2b73262 100644 --- a/app/domains/accountify/models/invoice.rb +++ b/app/domains/accountify/models/invoice.rb @@ -10,8 +10,6 @@ class Invoice < ActiveRecord::Base has_many :events, -> { order(created_at: :asc) }, as: :eventable, class_name: '::Models::Event' has_one :invoice_status_summary - - class LineItem < ActiveRecord::Base; end end end end diff --git a/app/domains/accountify/models/invoice/line_item.rb b/app/domains/accountify/models/invoice/line_item.rb new file mode 100644 index 0000000..0e2adb7 --- /dev/null +++ b/app/domains/accountify/models/invoice/line_item.rb @@ -0,0 +1,9 @@ +module Accountify + module Models + class Invoice < ActiveRecord::Base + class LineItem < ActiveRecord::Base + + end + end + end +end From 3997baa1d2b5e3000c3fbe7cc9aaaa85bc15715b Mon Sep 17 00:00:00 2001 From: bedrock-adam Date: Sat, 28 Dec 2024 12:24:34 +1100 Subject: [PATCH 5/5] update publish message --- .../message/publish_job.rb | 9 +++++++-- .../message/publish_job_spec.rb | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/jobs/outboxer_integration/message/publish_job.rb b/app/jobs/outboxer_integration/message/publish_job.rb index f699f98..c951d45 100644 --- a/app/jobs/outboxer_integration/message/publish_job.rb +++ b/app/jobs/outboxer_integration/message/publish_job.rb @@ -16,8 +16,13 @@ def perform(args) namespace, model, event = messageable_type.match(MESSAGEABLE_TYPE_REGEX).captures job_class_name = "#{namespace}::#{model}::#{event}Job" - job_class = job_class_name.constantize - job_class.perform_async({ 'id' => args['messageable_id'] }) + + begin + job_class = job_class_name.constantize + job_class.perform_async({ 'id' => args['messageable_id'] }) + rescue NameError + # no-op + end end end end diff --git a/spec/jobs/outboxer_integration/message/publish_job_spec.rb b/spec/jobs/outboxer_integration/message/publish_job_spec.rb index e682192..faff478 100644 --- a/spec/jobs/outboxer_integration/message/publish_job_spec.rb +++ b/spec/jobs/outboxer_integration/message/publish_job_spec.rb @@ -35,6 +35,25 @@ module Message }.to raise_error(StandardError, "Unexpected class name format: Wrong::Format::Test") end end + + context 'when job class does not exist' do + let(:args) do + { + 'messageable_type' => 'Accountify::Models::Invoice::NonexistentEvent', + 'messageable_id' => '123' + } + end + + it 'completes gracefully without raising an error' do + allow_any_instance_of(String) + .to receive(:constantize) + .and_raise(NameError.new("uninitialized constant")) + + expect { + PublishJob.new.perform(args) + }.not_to raise_error + end + end end end end