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
12 changes: 6 additions & 6 deletions app/controllers/accountify/contact_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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],
first_name: params[:first_name],
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
Expand All @@ -22,24 +22,24 @@ def show
end

def update
event_id = Contact.update(
contact = Contact.update(
user_id: user_id,
tenant_id: tenant_id,
id: params[:id],
first_name: params[:first_name],
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
24 changes: 12 additions & 12 deletions app/controllers/accountify/invoice_controller.rb
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -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
13 changes: 6 additions & 7 deletions app/controllers/accountify/organisation_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
25 changes: 20 additions & 5 deletions lib/accountify/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,38 @@ 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

class UpdatedEvent < Event; end

def update(user_id:, tenant_id:, id:,
first_name:, last_name:, email:)
contact = nil
event = nil

ActiveRecord::Base.transaction do
Expand Down Expand Up @@ -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
Expand All @@ -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
33 changes: 25 additions & 8 deletions lib/accountify/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions lib/accountify/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/accountify/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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) }

has_many :events, -> { order(created_at: :asc) },
as: :eventable

has_one :invoice_status_summary

class LineItem < ActiveRecord::Base; end
end
end
end
Loading
Loading