@@ -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
343360end
0 commit comments