Skip to content

Commit 23e349f

Browse files
authored
change return value schema (#35)
* commit changes * update organisation specs * update contact * pass service specs * fix specs * fix build
1 parent 4e387db commit 23e349f

33 files changed

+268
-222
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module Accountify
22
class ContactController < AccountifyController
33
def create
4-
contact_id, event_id = Contact.create(
4+
contact = Contact.create(
55
user_id: user_id,
66
tenant_id: tenant_id,
77
organisation_id: params[:organisation_id],
88
first_name: params[:first_name],
99
last_name: params[:last_name],
1010
email: params[:email])
1111

12-
render json: { contact_id: contact_id, event_id: event_id }, status: :created
12+
render json: contact, status: :created
1313
end
1414

1515
def show
@@ -22,24 +22,24 @@ def show
2222
end
2323

2424
def update
25-
event_id = Contact.update(
25+
contact = Contact.update(
2626
user_id: user_id,
2727
tenant_id: tenant_id,
2828
id: params[:id],
2929
first_name: params[:first_name],
3030
last_name: params[:last_name],
3131
email: params[:email])
3232

33-
render json: { event_id: event_id }, status: :ok
33+
render json: contact, status: :ok
3434
end
3535

3636
def destroy
37-
event_id = Contact.delete(
37+
contact = Contact.delete(
3838
user_id: user_id,
3939
tenant_id: tenant_id,
4040
id: params[:id])
4141

42-
render json: { event_id: event_id }, status: :ok
42+
render json: contact, status: :ok
4343
end
4444
end
4545
end
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Accountify
22
class InvoiceController < AccountifyController
33
def create
4-
id, event_id = Invoice.draft(
4+
invoice = Invoice.draft(
55
user_id: user_id,
66
tenant_id: tenant_id,
77
organisation_id: params[:organisation_id],
@@ -10,7 +10,7 @@ def create
1010
due_date: params[:due_date],
1111
line_items: params[:line_items])
1212

13-
render json: { id: id, event_id: event_id }, status: :created
13+
render json: invoice, status: :created
1414
end
1515

1616
def show
@@ -23,7 +23,7 @@ def show
2323
end
2424

2525
def update
26-
event_id = Invoice.update(
26+
invoice = Invoice.update(
2727
user_id: user_id,
2828
tenant_id: tenant_id,
2929
id: params[:id],
@@ -32,43 +32,43 @@ def update
3232
due_date: params[:due_date],
3333
line_items: params[:line_items])
3434

35-
render json: { event_id: event_id }, status: :ok
35+
render json: invoice, status: :ok
3636
end
3737

3838
def destroy
39-
event_id = Invoice.delete(
39+
invoice = Invoice.delete(
4040
user_id: user_id,
4141
tenant_id: tenant_id,
4242
id: params[:id])
4343

44-
render json: { event_id: event_id }, status: :ok
44+
render json: invoice, status: :ok
4545
end
4646

4747
def issue
48-
event_id = Invoice.issue(
48+
invoice = Invoice.issue(
4949
user_id: user_id,
5050
tenant_id: tenant_id,
5151
id: params[:id])
5252

53-
render json: { event_id: event_id }, status: :ok
53+
render json: invoice, status: :ok
5454
end
5555

5656
def paid
57-
event_id = Invoice.paid(
57+
invoice = Invoice.paid(
5858
user_id: user_id,
5959
tenant_id: tenant_id,
6060
id: params[:id])
6161

62-
render json: { event_id: event_id }, status: :ok
62+
render json: invoice, status: :ok
6363
end
6464

6565
def void
66-
event_id = Invoice.void(
66+
invoice = Invoice.void(
6767
user_id: user_id,
6868
tenant_id: tenant_id,
6969
id: params[:id])
7070

71-
render json: { event_id: event_id }, status: :ok
71+
render json: invoice, status: :ok
7272
end
7373
end
7474
end
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
module Accountify
22
class OrganisationController < AccountifyController
33
def create
4-
organisation_id, event_id = Organisation.create(
4+
organisation = Organisation.create(
55
user_id: user_id,
66
tenant_id: tenant_id,
77
name: params[:name])
88

9-
render json: { organisation_id: organisation_id, event_id: event_id },
10-
status: :created
9+
render json: organisation, status: :created
1110
end
1211

1312
def show
@@ -20,22 +19,22 @@ def show
2019
end
2120

2221
def update
23-
event_id = Organisation.update(
22+
organisation = Organisation.update(
2423
user_id: user_id,
2524
tenant_id: tenant_id,
2625
id: params[:id],
2726
name: params[:name])
2827

29-
render json: { event_id: event_id }, status: :ok
28+
render json: organisation, status: :ok
3029
end
3130

3231
def destroy
33-
event_id = Organisation.delete(
32+
organisation = Organisation.delete(
3433
user_id: user_id,
3534
tenant_id: tenant_id,
3635
id: params[:id])
3736

38-
render json: { event_id: event_id }, status: :ok
37+
render json: organisation, status: :ok
3938
end
4039
end
4140
end

lib/accountify/contact.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,38 @@ def create(user_id:, tenant_id:,
3838
'type' => event.type,
3939
'organisation_id' => event.body['contact']['organisation_id'] })
4040

41-
[contact.id, event.id]
41+
{ id: contact.id, events: [{ id: event.id, type: event.type }] }
4242
end
4343

4444
def find_by_id(user_id:, tenant_id:, id:)
45-
contact = Models::Contact.where(tenant_id: tenant_id).find_by!(id: id)
45+
contact = Models::Contact
46+
.includes(:events)
47+
.where(tenant_id: tenant_id)
48+
.find_by!(id: id)
4649

4750
{
4851
id: contact.id,
4952
first_name: contact.first_name,
5053
last_name: contact.last_name,
51-
email: contact.email
54+
email: contact.email,
55+
events: contact.events.map do |event|
56+
{
57+
id: event.id,
58+
type: event.type,
59+
eventable_type: event.eventable_type,
60+
eventable_id: event.eventable_id,
61+
body: event.body,
62+
created_at: event.created_at
63+
}
64+
end
5265
}
5366
end
5467

5568
class UpdatedEvent < Event; end
5669

5770
def update(user_id:, tenant_id:, id:,
5871
first_name:, last_name:, email:)
72+
contact = nil
5973
event = nil
6074

6175
ActiveRecord::Base.transaction do
@@ -86,12 +100,13 @@ def update(user_id:, tenant_id:, id:,
86100
'id' => event.id,
87101
'type' => event.type })
88102

89-
event.id
103+
{ id: contact.id, events: [{ id: event.id, type: event.type }] }
90104
end
91105

92106
class DeletedEvent < Event; end
93107

94108
def delete(user_id:, tenant_id:, id:, time: ::Time)
109+
contact = nil
95110
event = nil
96111

97112
ActiveRecord::Base.transaction do
@@ -117,7 +132,7 @@ def delete(user_id:, tenant_id:, id:, time: ::Time)
117132
'id' => event.id,
118133
'type' => event.type })
119134

120-
event.id
135+
{ id: contact.id, events: [{ id: event.id, type: event.type }] }
121136
end
122137
end
123138
end

lib/accountify/invoice.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ def draft(user_id:, tenant_id:,
8585
'occurred_at' => event.created_at.utc.iso8601,
8686
'organisation_id' => event.body['invoice']['organisation_id'] })
8787

88-
[invoice.id, event.id]
88+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
8989
end
9090

9191
def find_by_id(user_id:, tenant_id:, id:)
92-
invoice = Models::Invoice.where(tenant_id: tenant_id).find_by!(id: id)
92+
invoice = Models::Invoice
93+
.includes(:events)
94+
.where(tenant_id: tenant_id)
95+
.find_by!(id: id)
9396

9497
{
9598
id: invoice.id,
@@ -109,7 +112,17 @@ def find_by_id(user_id:, tenant_id:, id:)
109112
end,
110113
sub_total: {
111114
amount: invoice.sub_total_amount,
112-
currency_code: invoice.sub_total_currency_code }
115+
currency_code: invoice.sub_total_currency_code },
116+
events: invoice.events.map do |event|
117+
{
118+
id: event.id,
119+
type: event.type,
120+
eventable_type: event.eventable_type,
121+
eventable_id: event.eventable_id,
122+
body: event.body,
123+
created_at: event.created_at
124+
}
125+
end
113126
}
114127
end
115128

@@ -191,12 +204,13 @@ def update(user_id:, tenant_id:, id:,
191204
'occurred_at' => event.created_at.utc.iso8601,
192205
'organisation_id' => event.body['invoice']['organisation_id'] })
193206

194-
event.id
207+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
195208
end
196209

197210
class DeletedEvent < Event; end
198211

199212
def delete(user_id:, tenant_id:, id:, time: ::Time)
213+
invoice = nil
200214
event = nil
201215

202216
current_utc_time = time.now.utc
@@ -226,12 +240,13 @@ def delete(user_id:, tenant_id:, id:, time: ::Time)
226240
'occurred_at' => event.created_at.utc.iso8601,
227241
'organisation_id' => event.body['invoice']['organisation_id'] })
228242

229-
event.id
243+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
230244
end
231245

232246
class IssuedEvent < Event; end
233247

234248
def issue(user_id:, tenant_id:, id:, time: ::Time)
249+
invoice = nil
235250
event = nil
236251

237252
current_utc_time = time.now.utc
@@ -265,12 +280,13 @@ def issue(user_id:, tenant_id:, id:, time: ::Time)
265280
'occurred_at' => event.created_at.utc.iso8601,
266281
'organisation_id' => event.body['invoice']['organisation_id'] })
267282

268-
event.id
283+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
269284
end
270285

271286
class PaidEvent < Event; end
272287

273288
def paid(user_id:, tenant_id:, id:, time: ::Time)
289+
invoice = nil
274290
event = nil
275291

276292
current_utc_time = time.now.utc
@@ -305,12 +321,13 @@ def paid(user_id:, tenant_id:, id:, time: ::Time)
305321
'occurred_at' => event.created_at.utc.iso8601,
306322
'organisation_id' => event.body['invoice']['organisation_id'] })
307323

308-
event.id
324+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
309325
end
310326

311327
class VoidedEvent < Event; end
312328

313329
def void(user_id:, tenant_id:, id:, time: ::Time)
330+
invoice = nil
314331
event = nil
315332

316333
ActiveRecord::Base.transaction do
@@ -337,7 +354,7 @@ def void(user_id:, tenant_id:, id:, time: ::Time)
337354
'occurred_at' => event.created_at.utc.iso8601,
338355
'organisation_id' => event.body['invoice']['organisation_id'] })
339356

340-
event.id
357+
{ id: invoice.id, events: [{ id: event.id, type: event.type }] }
341358
end
342359
end
343360
end

lib/accountify/models/contact.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Models
33
class Contact < ActiveRecord::Base
44
self.table_name = 'accountify_contacts'
55

6+
validates :organisation_id, presence: true
7+
68
has_many :events, -> { order(created_at: :asc) }, as: :eventable
79
end
810
end

lib/accountify/models/invoice.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ module Models
33
class Invoice < ActiveRecord::Base
44
self.table_name = 'accountify_invoices'
55

6-
class LineItem < ActiveRecord::Base; end
6+
validates :organisation_id, presence: true
77

88
has_many :line_items, -> { order(id: :asc) }
99

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

1313
has_one :invoice_status_summary
14+
15+
class LineItem < ActiveRecord::Base; end
1416
end
1517
end
1618
end

0 commit comments

Comments
 (0)