Skip to content

Commit 677146d

Browse files
authored
add OutboxerIntegration::Message::PublishJob with updated conventions (#52)
1 parent 3ae08c8 commit 677146d

File tree

16 files changed

+555
-261
lines changed

16 files changed

+555
-261
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class DeletedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::DeletedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class DraftedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::DraftedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class IssuedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::IssuedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class PaidJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::PaidEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class UpdatedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::UpdatedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Accountify
2+
module Invoice
3+
class VoidedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Invoice::VoidedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::RegenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'],
16+
'invoice_updated_at' => event.created_at.utc.iso8601 })
17+
end
18+
end
19+
end
20+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Accountify
2+
module Organisation
3+
class CreatedJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options retry: false, backtrace: true
7+
8+
def perform(args)
9+
event = ActiveRecord::Base.connection_pool.with_connection do
10+
Models::Organisation::CreatedEvent.find(args['id'])
11+
end
12+
13+
InvoiceStatusSummary::GenerateJob.perform_async({
14+
'tenant_id' => event.tenant_id,
15+
'organisation_id' => event.body['organisation']['id'] })
16+
end
17+
end
18+
end
19+
end

app/jobs/outboxer_integration/message/publish_job.rb

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,21 @@ module Message
33
class PublishJob
44
include Sidekiq::Job
55

6-
sidekiq_options queue: 'events', retry: false, backtrace: true
6+
sidekiq_options retry: false, backtrace: true
77

8-
def perform(args)
9-
messageable = ActiveRecord::Base.connection_pool.with_connection do
10-
args['messageable_type'].constantize.find(args['messageable_id'])
11-
end
12-
13-
case args['messageable_type']
14-
when 'Accountify::Models::Organisation::CreatedEvent'
15-
Accountify::InvoiceStatusSummary::GenerateJob.perform_async({
16-
'tenant_id' => messageable.tenant_id,
17-
'organisation_id' => messageable.body['organisation']['id'] })
18-
19-
when 'Accountify::Models::Invoice::DraftedEvent'
20-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
21-
'tenant_id' => messageable.tenant_id,
22-
'organisation_id' => messageable.body['organisation']['id'],
23-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
24-
25-
when 'Accountify::Models::Invoice::UpdatedEvent'
26-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
27-
'tenant_id' => messageable.tenant_id,
28-
'organisation_id' => messageable.body['organisation']['id'],
29-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
8+
MESSAGEABLE_TYPE_REGEX = /\A([A-Za-z]+)::Models::([A-Za-z]+)::([A-Za-z]+)Event\z/
309

31-
when 'Accountify::Models::Invoice::IssuedEvent'
32-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
33-
'tenant_id' => messageable.tenant_id,
34-
'organisation_id' => messageable.body['organisation']['id'],
35-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
36-
37-
when 'Accountify::Models::Invoice::PaidEvent'
38-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
39-
'tenant_id' => messageable.tenant_id,
40-
'organisation_id' => messageable.body['organisation']['id'],
41-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
42-
43-
when 'Accountify::Models::Invoice::VoidedEvent'
44-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
45-
'tenant_id' => messageable.tenant_id,
46-
'organisation_id' => messageable.body['organisation']['id'],
47-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
10+
def perform(args)
11+
messageable_type = args['messageable_type']
4812

49-
when 'Accountify::Models::Invoice::DeletedEvent'
50-
Accountify::InvoiceStatusSummary::RegenerateJob.perform_async({
51-
'tenant_id' => messageable.tenant_id,
52-
'organisation_id' => messageable.body['organisation']['id'],
53-
'invoice_updated_at' => messageable.created_at.utc.iso8601 })
13+
if !messageable_type.match(MESSAGEABLE_TYPE_REGEX)
14+
raise StandardError, "Unexpected class name format: #{messageable_type}"
5415
end
16+
17+
namespace, model, event = messageable_type.match(MESSAGEABLE_TYPE_REGEX).captures
18+
job_class_name = "#{namespace}::#{model}::#{event}Job"
19+
job_class = job_class_name.constantize
20+
job_class.perform_async({ 'id' => args['messageable_id'] })
5521
end
5622
end
5723
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
require 'rails_helper'
3+
4+
module Accountify
5+
module Invoice
6+
RSpec.describe DeletedJob, type: :job do
7+
let(:user_id) { 123 }
8+
let(:tenant_id) { 456 }
9+
10+
let(:current_time) { Time.now }
11+
12+
let(:accountify_organisation) do
13+
create(:accountify_organisation, tenant_id: tenant_id)
14+
end
15+
16+
let(:accountify_contact) do
17+
create(:accountify_contact,
18+
tenant_id: tenant_id, organisation_id: organisation.id)
19+
end
20+
21+
let(:accountify_invoice) do
22+
create(:accountify_invoice,
23+
tenant_id: tenant_id, organisation_id: organisation.id, contact_id: contact.id)
24+
end
25+
26+
describe 'when Accountify::Models::Invoice::DeletedEvent' do
27+
let(:event) do
28+
create(
29+
:accountify_invoice_deleted_event,
30+
user_id: user_id,
31+
tenant_id: tenant_id,
32+
eventable: accountify_organisation,
33+
created_at: current_time.utc,
34+
body: {
35+
'organisation' => { 'id' => accountify_organisation.id } })
36+
end
37+
38+
before do
39+
DeletedJob.new.perform({ 'id' => event.id })
40+
end
41+
42+
it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do
43+
expect(Accountify::InvoiceStatusSummary::RegenerateJob.jobs).to match([
44+
hash_including(
45+
'args' => [
46+
hash_including(
47+
'tenant_id' => tenant_id,
48+
'organisation_id' => accountify_organisation.id,
49+
'invoice_updated_at' => event.created_at.utc.iso8601 )])])
50+
end
51+
end
52+
end
53+
end
54+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
require 'rails_helper'
3+
4+
module Accountify
5+
module Invoice
6+
RSpec.describe DraftedJob, type: :job do
7+
let(:user_id) { 123 }
8+
let(:tenant_id) { 456 }
9+
10+
let(:current_time) { Time.now }
11+
12+
let(:accountify_organisation) do
13+
create(:accountify_organisation, tenant_id: tenant_id)
14+
end
15+
16+
let(:accountify_contact) do
17+
create(:accountify_contact,
18+
tenant_id: tenant_id, organisation_id: organisation.id)
19+
end
20+
21+
let(:accountify_invoice) do
22+
create(:accountify_invoice,
23+
tenant_id: tenant_id, organisation_id: organisation.id, contact_id: contact.id)
24+
end
25+
26+
describe 'when Accountify::Models::Invoice::DraftedEvent' do
27+
let(:event) do
28+
create(
29+
:accountify_invoice_drafted_event,
30+
user_id: user_id,
31+
tenant_id: tenant_id,
32+
eventable: accountify_organisation,
33+
created_at: current_time.utc,
34+
body: {
35+
'organisation' => { 'id' => accountify_organisation.id } })
36+
end
37+
38+
before do
39+
DraftedJob.new.perform({ 'id' => event.id })
40+
end
41+
42+
it 'performs Accountify::InvoiceStatusSummary::RegenerateJob async' do
43+
expect(Accountify::InvoiceStatusSummary::RegenerateJob.jobs).to match([
44+
hash_including(
45+
'args' => [
46+
hash_including(
47+
'tenant_id' => tenant_id,
48+
'organisation_id' => accountify_organisation.id,
49+
'invoice_updated_at' => event.created_at.utc.iso8601 )])])
50+
end
51+
end
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)