Skip to content
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
21 changes: 18 additions & 3 deletions app/domains/accountify/invoice_status_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,12 +33,19 @@ def generate(tenant_id:, organisation_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')
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/domains/accountify/models/contact/created_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Contact
class CreatedEvent < Models::Event
class CreatedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/contact/deleted_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Contact
class DeletedEvent < Models::Event
class DeletedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/contact/updated_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Contact
class UpdatedEvent < Models::Event
class UpdatedEvent < ::Models::Event

end
end
Expand Down
7 changes: 0 additions & 7 deletions app/domains/accountify/models/event.rb

This file was deleted.

4 changes: 1 addition & 3 deletions app/domains/accountify/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ 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

class LineItem < ActiveRecord::Base; end
end
end
end
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/deleted_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class DeletedEvent < Models::Event
class DeletedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/drafted_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class DraftedEvent < Models::Event
class DraftedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/issued_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class IssuedEvent < Models::Event
class IssuedEvent < ::Models::Event

end
end
Expand Down
9 changes: 9 additions & 0 deletions app/domains/accountify/models/invoice/line_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Accountify
module Models
class Invoice < ActiveRecord::Base
class LineItem < ActiveRecord::Base

end
end
end
end
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/paid_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class PaidEvent < Models::Event
class PaidEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/updated_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class UpdatedEvent < Models::Event
class UpdatedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/invoice/voided_event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Invoice
class VoidedEvent < Models::Event
class VoidedEvent < ::Models::Event

end
end
Expand Down
2 changes: 1 addition & 1 deletion app/domains/accountify/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Organisation
class CreatedEvent < Models::Event
class CreatedEvent < ::Models::Event

end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Organisation
class DeletedEvent < Models::Event
class DeletedEvent < ::Models::Event

end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Accountify
module Models
class Organisation
class UpdatedEvent < Models::Event
class UpdatedEvent < ::Models::Event

end
end
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/deleted_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/drafted_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/issued_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/paid_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/updated_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions app/jobs/accountify/invoice/voided_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/jobs/accountify/invoice_status_summary/generate_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/jobs/accountify/invoice_status_summary/regenerate_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions app/jobs/accountify/organisation/created_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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['event_id'] })
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/jobs/outboxer_integration/message/publish_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions spec/jobs/accountify/invoice/deleted_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,17 +36,15 @@ 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
expect(Accountify::InvoiceStatusSummary::RegenerateJob.jobs).to match([
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
Expand Down
Loading
Loading