diff --git a/app/controllers/accountify/contact_controller.rb b/app/controllers/accountify/contact_controller.rb index 5061557..0162b39 100644 --- a/app/controllers/accountify/contact_controller.rb +++ b/app/controllers/accountify/contact_controller.rb @@ -1,7 +1,7 @@ module Accountify class ContactController < AccountifyController def create - contact_id, event_id = Contact.create( + contact = Contact.create( user_id: user_id, tenant_id: tenant_id, organisation_id: params[:organisation_id], @@ -9,7 +9,7 @@ def create last_name: params[:last_name], email: params[:email]) - render json: { contact_id: contact_id, event_id: event_id }, status: :created + render json: contact, status: :created end def show @@ -22,7 +22,7 @@ def show end def update - event_id = Contact.update( + contact = Contact.update( user_id: user_id, tenant_id: tenant_id, id: params[:id], @@ -30,16 +30,16 @@ def update last_name: params[:last_name], email: params[:email]) - render json: { event_id: event_id }, status: :ok + render json: contact, status: :ok end def destroy - event_id = Contact.delete( + contact = Contact.delete( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: contact, status: :ok end end end diff --git a/app/controllers/accountify/invoice_controller.rb b/app/controllers/accountify/invoice_controller.rb index 83842c4..86f77ee 100644 --- a/app/controllers/accountify/invoice_controller.rb +++ b/app/controllers/accountify/invoice_controller.rb @@ -1,7 +1,7 @@ module Accountify class InvoiceController < AccountifyController def create - id, event_id = Invoice.draft( + invoice = Invoice.draft( user_id: user_id, tenant_id: tenant_id, organisation_id: params[:organisation_id], @@ -10,7 +10,7 @@ def create due_date: params[:due_date], line_items: params[:line_items]) - render json: { id: id, event_id: event_id }, status: :created + render json: invoice, status: :created end def show @@ -23,7 +23,7 @@ def show end def update - event_id = Invoice.update( + invoice = Invoice.update( user_id: user_id, tenant_id: tenant_id, id: params[:id], @@ -32,43 +32,43 @@ def update due_date: params[:due_date], line_items: params[:line_items]) - render json: { event_id: event_id }, status: :ok + render json: invoice, status: :ok end def destroy - event_id = Invoice.delete( + invoice = Invoice.delete( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: invoice, status: :ok end def issue - event_id = Invoice.issue( + invoice = Invoice.issue( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: invoice, status: :ok end def paid - event_id = Invoice.paid( + invoice = Invoice.paid( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: invoice, status: :ok end def void - event_id = Invoice.void( + invoice = Invoice.void( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: invoice, status: :ok end end end diff --git a/app/controllers/accountify/organisation_controller.rb b/app/controllers/accountify/organisation_controller.rb index 74a27b5..23c2dc4 100644 --- a/app/controllers/accountify/organisation_controller.rb +++ b/app/controllers/accountify/organisation_controller.rb @@ -1,13 +1,12 @@ module Accountify class OrganisationController < AccountifyController def create - organisation_id, event_id = Organisation.create( + organisation = Organisation.create( user_id: user_id, tenant_id: tenant_id, name: params[:name]) - render json: { organisation_id: organisation_id, event_id: event_id }, - status: :created + render json: organisation, status: :created end def show @@ -20,22 +19,22 @@ def show end def update - event_id = Organisation.update( + organisation = Organisation.update( user_id: user_id, tenant_id: tenant_id, id: params[:id], name: params[:name]) - render json: { event_id: event_id }, status: :ok + render json: organisation, status: :ok end def destroy - event_id = Organisation.delete( + organisation = Organisation.delete( user_id: user_id, tenant_id: tenant_id, id: params[:id]) - render json: { event_id: event_id }, status: :ok + render json: organisation, status: :ok end end end diff --git a/lib/accountify/contact.rb b/lib/accountify/contact.rb index 3f6b465..c437cd1 100644 --- a/lib/accountify/contact.rb +++ b/lib/accountify/contact.rb @@ -38,17 +38,30 @@ def create(user_id:, tenant_id:, 'type' => event.type, 'organisation_id' => event.body['contact']['organisation_id'] }) - [contact.id, event.id] + { id: contact.id, events: [{ id: event.id, type: event.type }] } end def find_by_id(user_id:, tenant_id:, id:) - contact = Models::Contact.where(tenant_id: tenant_id).find_by!(id: id) + contact = Models::Contact + .includes(:events) + .where(tenant_id: tenant_id) + .find_by!(id: id) { id: contact.id, first_name: contact.first_name, last_name: contact.last_name, - email: contact.email + email: contact.email, + events: contact.events.map do |event| + { + id: event.id, + type: event.type, + eventable_type: event.eventable_type, + eventable_id: event.eventable_id, + body: event.body, + created_at: event.created_at + } + end } end @@ -56,6 +69,7 @@ class UpdatedEvent < Event; end def update(user_id:, tenant_id:, id:, first_name:, last_name:, email:) + contact = nil event = nil ActiveRecord::Base.transaction do @@ -86,12 +100,13 @@ def update(user_id:, tenant_id:, id:, 'id' => event.id, 'type' => event.type }) - event.id + { id: contact.id, events: [{ id: event.id, type: event.type }] } end class DeletedEvent < Event; end def delete(user_id:, tenant_id:, id:, time: ::Time) + contact = nil event = nil ActiveRecord::Base.transaction do @@ -117,7 +132,7 @@ def delete(user_id:, tenant_id:, id:, time: ::Time) 'id' => event.id, 'type' => event.type }) - event.id + { id: contact.id, events: [{ id: event.id, type: event.type }] } end end end diff --git a/lib/accountify/invoice.rb b/lib/accountify/invoice.rb index 95749bc..26d6b43 100644 --- a/lib/accountify/invoice.rb +++ b/lib/accountify/invoice.rb @@ -85,11 +85,14 @@ def draft(user_id:, tenant_id:, 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - [invoice.id, event.id] + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end def find_by_id(user_id:, tenant_id:, id:) - invoice = Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) + invoice = Models::Invoice + .includes(:events) + .where(tenant_id: tenant_id) + .find_by!(id: id) { id: invoice.id, @@ -109,7 +112,17 @@ def find_by_id(user_id:, tenant_id:, id:) end, sub_total: { amount: invoice.sub_total_amount, - currency_code: invoice.sub_total_currency_code } + currency_code: invoice.sub_total_currency_code }, + events: invoice.events.map do |event| + { + id: event.id, + type: event.type, + eventable_type: event.eventable_type, + eventable_id: event.eventable_id, + body: event.body, + created_at: event.created_at + } + end } end @@ -191,12 +204,13 @@ def update(user_id:, tenant_id:, id:, 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - event.id + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end class DeletedEvent < Event; end def delete(user_id:, tenant_id:, id:, time: ::Time) + invoice = nil event = nil current_utc_time = time.now.utc @@ -226,12 +240,13 @@ def delete(user_id:, tenant_id:, id:, time: ::Time) 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - event.id + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end class IssuedEvent < Event; end def issue(user_id:, tenant_id:, id:, time: ::Time) + invoice = nil event = nil current_utc_time = time.now.utc @@ -265,12 +280,13 @@ def issue(user_id:, tenant_id:, id:, time: ::Time) 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - event.id + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end class PaidEvent < Event; end def paid(user_id:, tenant_id:, id:, time: ::Time) + invoice = nil event = nil current_utc_time = time.now.utc @@ -305,12 +321,13 @@ def paid(user_id:, tenant_id:, id:, time: ::Time) 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - event.id + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end class VoidedEvent < Event; end def void(user_id:, tenant_id:, id:, time: ::Time) + invoice = nil event = nil ActiveRecord::Base.transaction do @@ -337,7 +354,7 @@ def void(user_id:, tenant_id:, id:, time: ::Time) 'occurred_at' => event.created_at.utc.iso8601, 'organisation_id' => event.body['invoice']['organisation_id'] }) - event.id + { id: invoice.id, events: [{ id: event.id, type: event.type }] } end end end diff --git a/lib/accountify/models/contact.rb b/lib/accountify/models/contact.rb index cb65d02..1727cec 100644 --- a/lib/accountify/models/contact.rb +++ b/lib/accountify/models/contact.rb @@ -3,6 +3,8 @@ module Models class Contact < ActiveRecord::Base self.table_name = 'accountify_contacts' + validates :organisation_id, presence: true + has_many :events, -> { order(created_at: :asc) }, as: :eventable end end diff --git a/lib/accountify/models/invoice.rb b/lib/accountify/models/invoice.rb index 2df7060..807970f 100644 --- a/lib/accountify/models/invoice.rb +++ b/lib/accountify/models/invoice.rb @@ -3,7 +3,7 @@ module Models class Invoice < ActiveRecord::Base self.table_name = 'accountify_invoices' - class LineItem < ActiveRecord::Base; end + validates :organisation_id, presence: true has_many :line_items, -> { order(id: :asc) } @@ -11,6 +11,8 @@ class LineItem < ActiveRecord::Base; end as: :eventable has_one :invoice_status_summary + + class LineItem < ActiveRecord::Base; end end end end diff --git a/lib/accountify/organisation.rb b/lib/accountify/organisation.rb index ab27a39..e488b61 100644 --- a/lib/accountify/organisation.rb +++ b/lib/accountify/organisation.rb @@ -30,23 +30,35 @@ def create(user_id:, tenant_id:, name:) 'type' => event.type, 'organisation_id' => event['body']['organisation']['id'] }) - [organisation.id, event.id] + { id: organisation.id, events: [{ id: event.id, type: event.type }] } end def find_by_id(user_id:, tenant_id:, id:) organisation = Models::Organisation + .includes(:events) .where(tenant_id: tenant_id) .find_by!(id: id) { id: organisation.id, - name: organisation.name + name: organisation.name, + events: organisation.events.map do |event| + { + id: event.id, + type: event.type, + eventable_type: event.eventable_type, + eventable_id: event.eventable_id, + body: event.body, + created_at: event.created_at + } + end } end class UpdatedEvent < Event; end def update(user_id:, tenant_id:, id:, name:) + organisation = nil event = nil ActiveRecord::Base.transaction do @@ -71,12 +83,13 @@ def update(user_id:, tenant_id:, id:, name:) 'id' => event.id, 'type' => event.type }) - event.id + { id: organisation.id, events: [{ id: event.id, type: event.type }] } end class DeletedEvent < Event; end def delete(user_id:, tenant_id:, id:, time: ::Time) + organisation = nil event = nil ActiveRecord::Base.transaction do @@ -102,7 +115,7 @@ def delete(user_id:, tenant_id:, id:, time: ::Time) 'id' => event.id, 'type' => event.type }) - event.id + { id: organisation.id, events: [{ id: event.id, type: event.type }] } end end end diff --git a/script/accountify/invoice/test_lifecycle.rb b/script/accountify/invoice/test_lifecycle.rb index 6922e19..778bf60 100644 --- a/script/accountify/invoice/test_lifecycle.rb +++ b/script/accountify/invoice/test_lifecycle.rb @@ -9,26 +9,26 @@ current_date = ::Time.now.to_date -organisation_id, _ = Accountify::Organisation.create( +organisation = Accountify::Organisation.create( user_id: user_id, tenant_id: tenant_id, name: 'Debbies Debts Ltd') sleep 1 -contact_id, _ = Accountify::Contact.create( +contact = Accountify::Contact.create( user_id: user_id, tenant_id: tenant_id, - organisation_id: organisation_id, + organisation_id: organisation[:id], first_name: 'John', last_name: 'Elliot', email: 'john.elliot@tradies.com') -invoice_id, _ = Accountify::Invoice.draft( +invoice = Accountify::Invoice.draft( user_id: user_id, tenant_id: tenant_id, - organisation_id: organisation_id, - contact_id: contact_id, + organisation_id: organisation[:id], + contact_id: contact[:id], currency_code: "AUD", due_date: current_date + 30.days, line_items: [{ @@ -47,9 +47,9 @@ Accountify::Invoice.update( user_id: user_id, tenant_id: tenant_id, - id: invoice_id, - contact_id: contact_id, - organisation_id: organisation_id, + id: invoice[:id], + contact_id: contact[:id], + organisation_id: organisation[:id], due_date: current_date + 14.days, line_items: [{ description: "Green Jumper", @@ -64,13 +64,13 @@ currency_code: "AUD" }, quantity: 4 }]) -Accountify::Invoice.issue(user_id: user_id, tenant_id: tenant_id, id: invoice_id) +Accountify::Invoice.issue(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::Invoice.paid(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) +Accountify::Invoice.void(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) -Accountify::Invoice.delete(user_id: user_id, tenant_id: tenant_id, id: invoice_id) +Accountify::Invoice.delete(user_id: user_id, tenant_id: tenant_id, id: invoice[:id]) puts "Starting Sidekiq..." sidekiq_cmd = "bundle exec sidekiq -r ./config/sidekiq.rb" @@ -87,7 +87,7 @@ invoice_status_summary = Accountify::InvoiceStatusSummary.find_by_organisation_id( tenant_id: tenant_id, - organisation_id: organisation_id) + organisation_id: organisation[:id]) rescue Accountify::NotFound sleep 1 diff --git a/spec/controllers/accountify/contact_controller_spec.rb b/spec/controllers/accountify/contact_controller_spec.rb index 933f09a..1c14b7f 100644 --- a/spec/controllers/accountify/contact_controller_spec.rb +++ b/spec/controllers/accountify/contact_controller_spec.rb @@ -29,8 +29,8 @@ module Accountify } expect(response).to have_http_status(:created) - expect(JSON.parse(response.body)).to have_key('contact_id') - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') end end @@ -53,7 +53,8 @@ module Accountify } expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') contact.reload expect(contact.first_name).to eq('Jane') expect(contact.last_name).to eq('Doe') @@ -66,7 +67,8 @@ module Accountify delete :destroy, params: { id: contact.id } expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') expect(Models::Contact.find_by(deleted_at: nil, id: contact.id)).to be_nil end end diff --git a/spec/controllers/accountify/invoice_controller/create_spec.rb b/spec/controllers/accountify/invoice_controller/create_spec.rb index 52f4ba2..9c017f7 100644 --- a/spec/controllers/accountify/invoice_controller/create_spec.rb +++ b/spec/controllers/accountify/invoice_controller/create_spec.rb @@ -50,13 +50,16 @@ module Accountify let(:event) do Invoice::DraftedEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end describe 'POST #create' do it 'returns 201 status with id and event_id in body' do expect(response).to have_http_status(:created) - expect(response_body_json).to eq({ 'id' => invoice.id, 'event_id' => event.id }) + expect(response_body_json).to eq({ + 'id' => invoice.id, + 'events' => [{ 'id' => event.id, 'type' => event.type }] + }) end it 'creates invoice' do diff --git a/spec/controllers/accountify/invoice_controller/delete_spec.rb b/spec/controllers/accountify/invoice_controller/delete_spec.rb index c29047d..6fdcd22 100644 --- a/spec/controllers/accountify/invoice_controller/delete_spec.rb +++ b/spec/controllers/accountify/invoice_controller/delete_spec.rb @@ -53,14 +53,14 @@ module Accountify let(:event) do Invoice::DeletedEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end describe 'DELETE #destroy' do it 'returns 200 with deleted event id in body' do expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('events') end it 'deletes the invoice' do diff --git a/spec/controllers/accountify/invoice_controller/find_by_id_spec.rb b/spec/controllers/accountify/invoice_controller/find_by_id_spec.rb index dd3e3a4..61c7a1f 100644 --- a/spec/controllers/accountify/invoice_controller/find_by_id_spec.rb +++ b/spec/controllers/accountify/invoice_controller/find_by_id_spec.rb @@ -75,7 +75,8 @@ module Accountify "status" => "draft", "sub_total" => { "amount" => "600.0", - "currency_code" => "AUD" } }) + "currency_code" => "AUD" }, + "events" => []}) end end end diff --git a/spec/controllers/accountify/invoice_controller/issue_spec.rb b/spec/controllers/accountify/invoice_controller/issue_spec.rb index d43fa04..ba8e3f2 100644 --- a/spec/controllers/accountify/invoice_controller/issue_spec.rb +++ b/spec/controllers/accountify/invoice_controller/issue_spec.rb @@ -54,7 +54,7 @@ module Accountify let(:event) do Invoice::IssuedEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end let(:invoice) do @@ -65,7 +65,7 @@ module Accountify it 'returns 200 with deleted event id in body' do expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('events') end it 'updates invoice status to issued' do diff --git a/spec/controllers/accountify/invoice_controller/paid_spec.rb b/spec/controllers/accountify/invoice_controller/paid_spec.rb index 3aee241..7a8abf1 100644 --- a/spec/controllers/accountify/invoice_controller/paid_spec.rb +++ b/spec/controllers/accountify/invoice_controller/paid_spec.rb @@ -54,7 +54,7 @@ module Accountify let(:event) do Invoice::PaidEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end let(:invoice) do @@ -65,7 +65,7 @@ module Accountify it 'returns 200 with paid event id in body' do expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('events') end it 'updates invoice status to paid' do diff --git a/spec/controllers/accountify/invoice_controller/update_spec.rb b/spec/controllers/accountify/invoice_controller/update_spec.rb index 6907eeb..f689024 100644 --- a/spec/controllers/accountify/invoice_controller/update_spec.rb +++ b/spec/controllers/accountify/invoice_controller/update_spec.rb @@ -77,13 +77,16 @@ module Accountify let(:event) do Invoice::UpdatedEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end describe 'PUT #update' do it 'returns 200 status with event_id in body' do expect(response).to have_http_status(:ok) - expect(response_body_json).to eq({ 'event_id' => event.id }) + expect(response_body_json).to eq({ + 'id' => id, + 'events' => [{ 'id' => event.id, 'type' => event.type }] + }) end it 'updates model' do diff --git a/spec/controllers/accountify/invoice_controller/void_spec.rb b/spec/controllers/accountify/invoice_controller/void_spec.rb index a91f9c6..b720d87 100644 --- a/spec/controllers/accountify/invoice_controller/void_spec.rb +++ b/spec/controllers/accountify/invoice_controller/void_spec.rb @@ -54,14 +54,14 @@ module Accountify let(:event) do Invoice::VoidedEvent .where(tenant_id: tenant_id) - .find_by!(id: response_body_json['event_id']) + .find_by!(id: response_body_json['events'].last['id']) end describe 'PATCH #void' do it 'returns 200 with voided event id in body' do expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('events') end it 'updates the invoice status to voided' do diff --git a/spec/controllers/accountify/organisation_controller_spec.rb b/spec/controllers/accountify/organisation_controller_spec.rb index 1acc982..4384b04 100644 --- a/spec/controllers/accountify/organisation_controller_spec.rb +++ b/spec/controllers/accountify/organisation_controller_spec.rb @@ -20,8 +20,8 @@ module Accountify post :create, params: { name: 'New Organisation' } expect(response).to have_http_status(:created) - expect(JSON.parse(response.body)).to have_key('organisation_id') - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') end end @@ -39,7 +39,8 @@ module Accountify put :update, params: { id: organisation.id, name: 'Updated Organisation' } expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') organisation.reload expect(organisation.name).to eq('Updated Organisation') end @@ -50,7 +51,10 @@ module Accountify delete :destroy, params: { id: organisation.id } expect(response).to have_http_status(:ok) - expect(JSON.parse(response.body)).to have_key('event_id') + + expect(JSON.parse(response.body)).to have_key('id') + expect(JSON.parse(response.body)).to have_key('events') + expect( Models::Organisation.find_by(deleted_at: nil, id: organisation.id) ).to be_nil diff --git a/spec/lib/accountify/contact/create_spec.rb b/spec/lib/accountify/contact/create_spec.rb index 41a6b45..5c3b680 100644 --- a/spec/lib/accountify/contact/create_spec.rb +++ b/spec/lib/accountify/contact/create_spec.rb @@ -11,7 +11,7 @@ module Accountify let(:last_name) { 'Doe' } let(:email) { 'john.doe@example.com' } - let!(:result) do + let!(:contact) do Contact.create( user_id: user_id, tenant_id: tenant_id, @@ -21,38 +21,34 @@ module Accountify email: email) end - let(:id) { result[0] } - - let(:event_id) { result[1] } - - let(:contact) do - Models::Contact.where(tenant_id: tenant_id).find_by!(id: id) + let(:contact_model) do + Models::Contact.where(tenant_id: tenant_id).find_by!(id: contact[:id]) end - let(:event) do + let(:event_model) do Contact::CreatedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: contact[:events].last[:id]) end describe '.create' do it 'creates model' do - expect(contact.first_name).to eq(first_name) - expect(contact.last_name).to eq(last_name) - expect(contact.email).to eq(email) + expect(contact_model.first_name).to eq(first_name) + expect(contact_model.last_name).to eq(last_name) + expect(contact_model.email).to eq(email) end it 'creates created event' do - expect(event.body).to eq({ + expect(event_model.body).to eq({ 'contact' => { - 'id' => id, + 'id' => contact[:id], 'first_name' => first_name, 'last_name' => last_name, 'email' => email } }) end it 'associates event with model' do - expect(contact.events.last.id).to eq(event_id) + expect(contact_model.events.last.id).to eq(contact[:events].last[:id]) end it 'queues event created job' do @@ -62,7 +58,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => contact[:events].last[:id], 'type' => 'Accountify::Contact::CreatedEvent')])]) end end diff --git a/spec/lib/accountify/contact/delete_spec.rb b/spec/lib/accountify/contact/delete_spec.rb index 70f494a..fadee37 100644 --- a/spec/lib/accountify/contact/delete_spec.rb +++ b/spec/lib/accountify/contact/delete_spec.rb @@ -23,34 +23,34 @@ module Accountify ).id end - let!(:event_id) do + let!(:contact) do Contact.delete(user_id: user_id, tenant_id: tenant_id, id: id) end - let(:contact) do + let(:contact_model) do Models::Contact.where(tenant_id: tenant_id).find_by!(id: id) end - let(:event) do + let(:event_model) do Contact::DeletedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: contact[:events].last[:id]) end describe '.delete' do it "updates model deleted at" do - expect(contact.deleted_at).not_to be_nil + expect(contact_model.deleted_at).not_to be_nil end it 'creates deleted event' do - expect(event.body).to include( + expect(event_model.body).to include( 'contact' => a_hash_including( - 'id' => id, + 'id' => contact[:id], 'deleted_at' => be_present )) end it 'associates event with model' do - expect(contact.events.last.id).to eq event_id + expect(contact_model.events.last.id).to eq contact[:events].last[:id] end it 'queues event created job' do @@ -60,7 +60,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => contact[:events].last[:id], 'type' => 'Accountify::Contact::DeletedEvent')])]) end end diff --git a/spec/lib/accountify/contact/find_by_id_spec.rb b/spec/lib/accountify/contact/find_by_id_spec.rb index 0cf91bf..f8a9906 100644 --- a/spec/lib/accountify/contact/find_by_id_spec.rb +++ b/spec/lib/accountify/contact/find_by_id_spec.rb @@ -32,7 +32,8 @@ module Accountify id: id, first_name: first_name, last_name: last_name, - email: email }) + email: email, + events: []}) end end end diff --git a/spec/lib/accountify/contact/update_spec.rb b/spec/lib/accountify/contact/update_spec.rb index 8ab7fc9..eda8725 100644 --- a/spec/lib/accountify/contact/update_spec.rb +++ b/spec/lib/accountify/contact/update_spec.rb @@ -21,7 +21,7 @@ module Accountify email: email).id end - let!(:event_id) do + let!(:contact) do Contact.update( user_id: user_id, tenant_id: tenant_id, @@ -31,34 +31,34 @@ module Accountify email: 'johnny.doherty@coolbincompany.org') end - let(:contact) do - Models::Contact.where(tenant_id: tenant_id).find_by!(id: id) + let(:contact_model) do + Models::Contact.where(tenant_id: tenant_id).find_by!(id: contact[:id]) end - let(:event) do + let(:event_model) do Contact::UpdatedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: contact[:events].last[:id]) end describe '.update' do it 'updates model' do - expect(contact.first_name).to eq('Johnny') - expect(contact.last_name).to eq('Doherty') - expect(contact.email).to eq('johnny.doherty@coolbincompany.org') + expect(contact_model.first_name).to eq('Johnny') + expect(contact_model.last_name).to eq('Doherty') + expect(contact_model.email).to eq('johnny.doherty@coolbincompany.org') end it 'creates updated event' do - expect(event.body).to eq({ + expect(event_model.body).to eq({ 'contact' => { - 'id' => id, + 'id' => contact[:id], 'first_name' => "Johnny", 'last_name' => "Doherty", 'email' => 'johnny.doherty@coolbincompany.org' } }) end it 'associates event with model' do - expect(contact.events.last.id).to eq(event_id) + expect(event_model.id).to eq(contact[:events].last[:id]) end it 'queues event created job' do @@ -68,7 +68,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => contact[:events].last[:id], 'type' => 'Accountify::Contact::UpdatedEvent')])]) end end diff --git a/spec/lib/accountify/invoice/delete_spec.rb b/spec/lib/accountify/invoice/delete_spec.rb index b6626e6..c1d518b 100644 --- a/spec/lib/accountify/invoice/delete_spec.rb +++ b/spec/lib/accountify/invoice/delete_spec.rb @@ -50,30 +50,30 @@ module Accountify let!(:line_items) { [line_item_1, line_item_2] } - let(:invoice) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } - - let(:event) do - Invoice::DeletedEvent.where(tenant_id: tenant_id).find_by!(id: event_id) + let!(:invoice) do + Invoice.delete(user_id: user_id, tenant_id: tenant_id, id: id) end - let!(:event_id) do - Invoice.delete(user_id: user_id, tenant_id: tenant_id, id: id) + let(:invoice_model) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } + + let(:event_model) do + Invoice::DeletedEvent.where(tenant_id: tenant_id).find_by!(id: invoice[:events].last[:id]) end describe '.delete' do it "updates model deleted at" do - expect(invoice.deleted_at).not_to be_nil + expect(invoice_model.deleted_at).not_to be_nil end it 'creates deleted event' do - expect(event.body).to include( + expect(event_model.body).to include( 'invoice' => a_hash_including( - 'id' => id, + 'id' => invoice[:id], 'deleted_at' => be_present)) end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -83,7 +83,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::DeletedEvent')])]) end end diff --git a/spec/lib/accountify/invoice/draft_spec.rb b/spec/lib/accountify/invoice/draft_spec.rb index 6f6961b..2d26be9 100644 --- a/spec/lib/accountify/invoice/draft_spec.rb +++ b/spec/lib/accountify/invoice/draft_spec.rb @@ -17,7 +17,7 @@ module Accountify tenant_id: tenant_id, organisation_id: organisation.id) end - let(:result) do + let(:invoice) do Invoice.draft( user_id: user_id, tenant_id: tenant_id, @@ -39,21 +39,17 @@ module Accountify quantity: 3 } ]) end - let(:id) { result[0] } - - let(:event_id) { result[1] } - - let(:invoice) do - Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) + let(:invoice_model) do + Models::Invoice.where(tenant_id: tenant_id).find_by!(id: invoice[:id]) end - let(:event) do - Invoice::DraftedEvent.where(tenant_id: tenant_id).find_by!(id: event_id) + let(:event_model) do + Invoice::DraftedEvent.where(tenant_id: tenant_id).find_by!(id: invoice[:events].last[:id]) end describe '.draft' do it 'creates invoice' do - expect(invoice).to have_attributes( + expect(invoice_model).to have_attributes( organisation_id: organisation.id, contact_id: contact.id, status: Invoice::Status::DRAFT, @@ -75,9 +71,9 @@ module Accountify end it 'creates drafted event' do - expect(event.body).to eq({ + expect(event_model.body).to eq({ 'invoice' => { - 'id' => id, + 'id' => invoice[:id], 'organisation_id' => organisation.id, 'contact_id' => contact.id, 'status' => Invoice::Status::DRAFT, @@ -99,7 +95,7 @@ module Accountify end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -109,7 +105,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::DraftedEvent')])]) end end diff --git a/spec/lib/accountify/invoice/find_by_id_spec.rb b/spec/lib/accountify/invoice/find_by_id_spec.rb index d8bf195..c5e769e 100644 --- a/spec/lib/accountify/invoice/find_by_id_spec.rb +++ b/spec/lib/accountify/invoice/find_by_id_spec.rb @@ -77,7 +77,8 @@ module Accountify quantity: 3 }], sub_total: { amount: BigDecimal("1800.00"), - currency_code: "AUD" } }) + currency_code: "AUD" }, + events: []}) end end end diff --git a/spec/lib/accountify/invoice/issue_spec.rb b/spec/lib/accountify/invoice/issue_spec.rb index f1bc07c..3a8a3de 100644 --- a/spec/lib/accountify/invoice/issue_spec.rb +++ b/spec/lib/accountify/invoice/issue_spec.rb @@ -50,35 +50,35 @@ module Accountify let!(:line_items) { [line_item_1, line_item_2] } - let(:invoice) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } - - let(:event) do - Invoice::IssuedEvent.where(tenant_id: tenant_id).find_by!(id: event_id) + let!(:invoice) do + Invoice.issue(user_id: user_id, tenant_id: tenant_id, id: id) end - let!(:event_id) do - Invoice.issue(user_id: user_id, tenant_id: tenant_id, id: id) + let(:invoice_model) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } + + let(:event_model) do + Invoice::IssuedEvent.where(tenant_id: tenant_id).find_by!(id: invoice[:events].last[:id]) end describe '.issue' do it "updates model status" do - expect(invoice.status).to eq(Invoice::Status::ISSUED) + expect(invoice_model.status).to eq(Invoice::Status::ISSUED) end it "updates model issued_at" do - expect(invoice.issued_at).to be_present + expect(invoice_model.issued_at).to be_present end it 'creates issued event' do - expect(event.body).to include( + expect(event_model.body).to include( 'invoice' => a_hash_including( - 'id' => id, + 'id' => invoice[:id], 'status' => Invoice::Status::ISSUED, 'issued_at' => be_present ) ) end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -88,7 +88,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::IssuedEvent')])]) end end diff --git a/spec/lib/accountify/invoice/paid_spec.rb b/spec/lib/accountify/invoice/paid_spec.rb index 8db4200..f58dc94 100644 --- a/spec/lib/accountify/invoice/paid_spec.rb +++ b/spec/lib/accountify/invoice/paid_spec.rb @@ -41,37 +41,37 @@ module Accountify ).id end - let!(:event_id) do + let!(:invoice) do Invoice.paid(user_id: user_id, tenant_id: tenant_id, id: id) end - let(:invoice) do + let(:invoice_model) do Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) end - let(:event) do - Invoice::PaidEvent.where(tenant_id: tenant_id).find_by!(id: event_id) + let(:event_model) do + Invoice::PaidEvent.where(tenant_id: tenant_id).find_by!(id: invoice[:events].last[:id]) end describe '.paid' do it "updates model status to PAID" do - expect(invoice.status).to eq(Invoice::Status::PAID) + expect(invoice_model.status).to eq(Invoice::Status::PAID) end it "sets model paid_at" do - expect(invoice.paid_at).to be_present + expect(invoice_model.paid_at).to be_present end it 'creates paid event' do - expect(event.body).to include( + expect(event_model.body).to include( 'invoice' => a_hash_including( - 'id' => id, + 'id' => invoice[:id], 'status' => Invoice::Status::PAID, 'paid_at' => be_present ) ) end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -81,7 +81,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::PaidEvent')])]) end end diff --git a/spec/lib/accountify/invoice/update_spec.rb b/spec/lib/accountify/invoice/update_spec.rb index bbafde7..4586791 100644 --- a/spec/lib/accountify/invoice/update_spec.rb +++ b/spec/lib/accountify/invoice/update_spec.rb @@ -59,7 +59,7 @@ module Accountify let!(:line_items) { [line_item_1, line_item_2] } - let!(:event_id) do + let!(:invoice) do Invoice.update( user_id: user_id, tenant_id: tenant_id, @@ -81,19 +81,19 @@ module Accountify quantity: 4 }]) end - let(:invoice) do - Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) + let(:invoice_model) do + Models::Invoice.where(tenant_id: tenant_id).find_by!(id: invoice[:id]) end - let(:event) do + let(:event_model) do Invoice::UpdatedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: invoice[:events].last[:id]) end describe '.update' do it 'updates model' do - expect(invoice).to have_attributes( + expect(invoice_model).to have_attributes( organisation_id: organisation_2.id, contact_id: contact_2.id, status: Invoice::Status::DRAFT, @@ -115,7 +115,7 @@ module Accountify end it 'creates updated event' do - expect(event.body).to eq({ + expect(event_model.body).to eq({ 'invoice' => { 'id' => id, 'contact_id' => contact_2.id, @@ -139,7 +139,7 @@ module Accountify end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -149,7 +149,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::UpdatedEvent')])]) end end diff --git a/spec/lib/accountify/invoice/void_spec.rb b/spec/lib/accountify/invoice/void_spec.rb index fa97377..52034e0 100644 --- a/spec/lib/accountify/invoice/void_spec.rb +++ b/spec/lib/accountify/invoice/void_spec.rb @@ -50,30 +50,30 @@ module Accountify let!(:line_items) { [line_item_1, line_item_2] } - let(:invoice) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } - - let(:event) do - Invoice::VoidedEvent.where(tenant_id: tenant_id).find_by!(id: event_id) + let!(:invoice) do + Invoice.void(user_id: user_id, tenant_id: tenant_id, id: id) end - let!(:event_id) do - Invoice.void(user_id: user_id, tenant_id: tenant_id, id: id) + let(:invoice_model) { Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id) } + + let(:event_model) do + Invoice::VoidedEvent.where(tenant_id: tenant_id).find_by!(id: invoice[:events].last[:id]) end describe '.void' do it "updates model status" do - expect(invoice.status).to eq(Invoice::Status::VOIDED) + expect(invoice_model.status).to eq(Invoice::Status::VOIDED) end it 'creates voided event' do - expect(event.body).to include( + expect(event_model.body).to include( 'invoice' => a_hash_including( - 'id' => id, + 'id' => invoice[:id], 'status' => Invoice::Status::VOIDED)) end it 'associates event with model' do - expect(invoice.events.last.id).to eq(event_id) + expect(invoice_model.events.last.id).to eq(invoice[:events].last[:id]) end it 'queues event created job' do @@ -83,7 +83,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => invoice[:events].last[:id], 'type' => 'Accountify::Invoice::VoidedEvent')])]) end end diff --git a/spec/lib/accountify/organisation/create_spec.rb b/spec/lib/accountify/organisation/create_spec.rb index 9f42dad..68cd932 100644 --- a/spec/lib/accountify/organisation/create_spec.rb +++ b/spec/lib/accountify/organisation/create_spec.rb @@ -7,39 +7,35 @@ module Accountify let(:name) { 'Big Bin Corp' } - let(:result) do + let(:organisation) do Organisation.create( user_id: user_id, tenant_id: tenant_id, name: name) end - let(:id) { result[0] } - - let(:event_id) { result[1] } - - let(:organisation) do - Models::Organisation.where(tenant_id: tenant_id).find_by!(id: id) + let(:organisation_model) do + Models::Organisation.where(tenant_id: tenant_id).find_by!(id: organisation[:id]) end - let(:event) do + let(:event_model) do Organisation::CreatedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: organisation[:events].last[:id]) end describe '.create' do it 'creates model' do - expect(organisation.name).to eq(name) + expect(organisation_model.name).to eq(name) end it 'creates created event' do - expect(event.body).to eq ({ + expect(event_model.body).to eq ({ 'organisation' => { - 'id' => id, + 'id' => organisation[:id], 'name' => name } }) end it 'associates event with model' do - expect(organisation.events.last.id).to eq(event_id) + expect(organisation_model.events.last.id).to eq(organisation[:events].last[:id]) end it 'queues event created job' do @@ -49,7 +45,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => organisation[:events].last[:id], 'type' => 'Accountify::Organisation::CreatedEvent')])]) end end diff --git a/spec/lib/accountify/organisation/delete_spec.rb b/spec/lib/accountify/organisation/delete_spec.rb index e99211d..0c280b4 100644 --- a/spec/lib/accountify/organisation/delete_spec.rb +++ b/spec/lib/accountify/organisation/delete_spec.rb @@ -12,34 +12,34 @@ module Accountify create(:accountify_organisation, tenant_id: tenant_id).id end - let!(:event_id) do + let!(:organisation) do Organisation.delete(user_id: user_id, tenant_id: tenant_id, id: id) end - let(:organisation) do + let(:organisation_model) do Models::Organisation.where(tenant_id: tenant_id).find_by!(id: id) end - let(:event) do + let(:event_model) do Organisation::DeletedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: organisation[:events].last[:id]) end describe '.delete' do it "updates model deleted at" do - expect(organisation.deleted_at).not_to be_nil + expect(organisation_model.deleted_at).not_to be_nil end it 'creates deleted event' do - expect(event.body).to include( + expect(event_model.body).to include( 'organisation' => a_hash_including( 'id' => id, 'deleted_at' => be_present )) end it 'associates event with model' do - expect(organisation.events.last.id).to eq event_id + expect(organisation_model.events.last.id).to eq organisation[:events].last[:id] end it 'queues event created job' do @@ -49,7 +49,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => organisation[:events].last[:id], 'type' => 'Accountify::Organisation::DeletedEvent')])]) end end diff --git a/spec/lib/accountify/organisation/find_by_id_spec.rb b/spec/lib/accountify/organisation/find_by_id_spec.rb index 6735de3..99380e8 100644 --- a/spec/lib/accountify/organisation/find_by_id_spec.rb +++ b/spec/lib/accountify/organisation/find_by_id_spec.rb @@ -18,7 +18,7 @@ module Accountify describe '.find_by_id' do it 'returns attributes' do - expect(organisation).to eq({ id: id, name: name }) + expect(organisation).to eq({ id: id, name: name, events: [] }) end end end diff --git a/spec/lib/accountify/organisation/update_spec.rb b/spec/lib/accountify/organisation/update_spec.rb index caad9c2..0935861 100644 --- a/spec/lib/accountify/organisation/update_spec.rb +++ b/spec/lib/accountify/organisation/update_spec.rb @@ -10,41 +10,36 @@ module Accountify create(:accountify_organisation, tenant_id: tenant_id).id end - let!(:event_id) do + let!(:organisation) do Organisation.update( user_id: user_id, tenant_id: tenant_id, id: id, name: 'Big Bin Corp updated') end - let(:organisation) do - Models::Organisation - .where(tenant_id: tenant_id) - .find_by!(id: id) + let(:organisation_model) do + Models::Organisation.where(tenant_id: tenant_id).find_by!(id: id) end - let(:event) do + let(:event_model) do Organisation::UpdatedEvent .where(tenant_id: tenant_id) - .find_by!(id: event_id) + .find_by!(id: organisation[:events].last[:id]) end describe '.update' do it 'updates model' do - expect(organisation.name).to eq('Big Bin Corp updated') + expect(organisation_model.name).to eq('Big Bin Corp updated') end it 'creates updated event' do - expect(event.body).to eq({ + expect(event_model.body).to eq({ 'organisation' => { - 'id' => id, + 'id' => organisation[:id], 'name' => 'Big Bin Corp updated' } }) end it 'associates event with model' do - event_id = Organisation.update( - user_id: user_id, tenant_id: tenant_id, id: id, name: 'Big Bin Corp updated') - - expect(organisation.events.last.id).to eq event_id + expect(event_model.id).to eq organisation[:events].last[:id] end it 'queues event created job' do @@ -54,7 +49,7 @@ module Accountify hash_including( 'user_id' => user_id, 'tenant_id' => tenant_id, - 'id' => event_id, + 'id' => organisation[:events].last[:id], 'type' => 'Accountify::Organisation::UpdatedEvent')])]) end end