diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 943b111..05a20c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,5 +65,14 @@ jobs: bundle exec rails db:create bundle exec rails db:schema:load + - name: add Outboxer + run: | + echo "gem 'outboxer', git: 'https://github.com/fast-programmer/outboxer.git', branch: 'master'" >> Gemfile + bundle install + bin/rails g outboxer:install + bin/rake db:migrate + mkdir lib + { sed '$d' lib/event.rb; printf "\n # callbacks\n\n after_create do |event|\n Outboxer::Message.queue(messageable: event)\n end\nend\n"; } > lib/event.tmp && mv lib/event.tmp lib/event.rb + - name: Run tests run: bundle exec rspec diff --git a/app/jobs/accountify/generate_invoice_status_summary_job.rb b/app/jobs/accountify/generate_invoice_status_summary_job.rb index 83e5e8b..83295dd 100644 --- a/app/jobs/accountify/generate_invoice_status_summary_job.rb +++ b/app/jobs/accountify/generate_invoice_status_summary_job.rb @@ -5,7 +5,7 @@ class GenerateInvoiceStatusSummaryJob sidekiq_options queue: 'reporting', backtrace: true def perform(args) - InvoiceStatusSummary.generate(event_id: args['event_id']) + InvoiceStatusSummaryService.generate(event_id: args['event_id']) end end end diff --git a/app/jobs/accountify/regenerate_invoice_status_summary_job.rb b/app/jobs/accountify/regenerate_invoice_status_summary_job.rb index 927004f..6094b15 100644 --- a/app/jobs/accountify/regenerate_invoice_status_summary_job.rb +++ b/app/jobs/accountify/regenerate_invoice_status_summary_job.rb @@ -5,7 +5,7 @@ class RegenerateInvoiceStatusSummaryJob sidekiq_options queue: 'reporting', backtrace: true def perform(args) - InvoiceStatusSummary.regenerate(event_id: args['event_id']) + InvoiceStatusSummaryService.regenerate(event_id: args['event_id']) rescue NotAvailable RegenerateJob.perform_in(1.minute, args) end diff --git a/script/accountify/invoice/test_lifecycle.rb b/script/accountify/invoice/test_lifecycle.rb index 0baaf43..c84b132 100644 --- a/script/accountify/invoice/test_lifecycle.rb +++ b/script/accountify/invoice/test_lifecycle.rb @@ -1,19 +1,19 @@ -require_relative '../../../config/environment' - require 'open3' +require_relative '../../../config/environment' + user_id = 123 tenant_id = 456 current_date = ::Time.now.to_date -organisation = Accountify::Organisation.create( +organisation = Accountify::OrganisationService.create( user_id: user_id, tenant_id: tenant_id, name: 'Debbies Debts Ltd') -contact = Accountify::Contact.create( +contact = Accountify::ContactService.create( user_id: user_id, tenant_id: tenant_id, organisation_id: organisation[:id], @@ -21,7 +21,7 @@ last_name: 'Elliot', email: 'john.elliot@tradies.com') -invoice = Accountify::Invoice.draft( +invoice = Accountify::InvoiceService.draft( user_id: user_id, tenant_id: tenant_id, organisation_id: organisation[:id], @@ -41,7 +41,7 @@ currency_code: "AUD" }, quantity: 3 } ]) -Accountify::Invoice.update( +Accountify::InvoiceService.update( user_id: user_id, tenant_id: tenant_id, id: invoice[:id], @@ -61,17 +61,23 @@ currency_code: "AUD" }, quantity: 4 }]) -Accountify::Invoice.issue(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) +Accountify::InvoiceService.issue(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) + +Accountify::InvoiceService.paid(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) + +Accountify::InvoiceService.void(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) -Accountify::Invoice.paid(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) +Accountify::InvoiceService.delete(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) -Accountify::Invoice.void(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) +outboxer_env = ENV['OUTBOXER_ENV'] || ENV['RAILS_ENV'] || 'development' -Accountify::Invoice.delete(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) +sidekiq_server_cmd = "RAILS_ENV=#{outboxer_env} bundle exec sidekiq -r ./config/sidekiq.rb" +puts sidekiq_server_cmd +sidekiq_server_process = IO.popen(sidekiq_server_cmd) -puts "Starting Sidekiq..." -sidekiq_cmd = "bundle exec sidekiq -r ./config/sidekiq.rb" -sidekiq_process = IO.popen(sidekiq_cmd) +outboxer_publisher_cmd = "OUTBOXER_ENV=#{outboxer_env} bin/outboxer_publisher" +puts outboxer_publisher_cmd +outboxer_publisher_process = IO.popen(outboxer_publisher_cmd) begin invoice_status_summary = nil @@ -82,7 +88,7 @@ begin attempts += 1 - invoice_status_summary = Accountify::InvoiceStatusSummary.find_by_organisation_id( + invoice_status_summary = Accountify::InvoiceStatusSummaryService.find_by_organisation_id( tenant_id: tenant_id, organisation_id: organisation[:id]) rescue Accountify::NotFound @@ -93,13 +99,16 @@ end if invoice_status_summary.nil? - raise Accountify::NotFound, "Invoice status summary not found after #{max_attempts} attempts." + raise Accountify::NotFound, "Invoice status summary not found after #{max_attempts} attempts" end ensure - puts "Stopping Sidekiq..." + puts "Stopping outboxer publisher..." + Process.kill("TERM", outboxer_publisher_process.pid) + Process.wait(outboxer_publisher_process.pid) - Process.kill("TERM", sidekiq_process.pid) - Process.wait(sidekiq_process.pid) + puts "Stopping sidekiq server..." + Process.kill("TERM", sidekiq_server_process.pid) + Process.wait(sidekiq_server_process.pid) end # bundle exec ruby script/accountify/invoice/test_lifecycle.rb diff --git a/spec/integration/accountify/invoice/test_lifecycle_spec.rb b/spec/integration/accountify/invoice/test_lifecycle_spec.rb index 29f6dd8..1977b4c 100644 --- a/spec/integration/accountify/invoice/test_lifecycle_spec.rb +++ b/spec/integration/accountify/invoice/test_lifecycle_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' RSpec.describe 'Invoice Lifecycle', type: :integration do - xit 'transitions as expected' do + it 'transitions as expected' do Sidekiq::Testing.disable! begin diff --git a/spec/jobs/accountify/organisation_created_job_spec.rb b/spec/jobs/accountify/organisation_created_job_spec.rb index b67e4e0..5035e26 100644 --- a/spec/jobs/accountify/organisation_created_job_spec.rb +++ b/spec/jobs/accountify/organisation_created_job_spec.rb @@ -25,7 +25,7 @@ module Accountify OrganisationCreatedJob.new.perform({ 'event_id' => event.id }) end - it 'performs Accountify::InvoiceStatusSummary::GenerateJob async' do + it 'performs Accountify::GenerateInvoiceStatusSummaryJob async' do expect(Accountify::GenerateInvoiceStatusSummaryJob.jobs).to match([ hash_including( 'args' => [