1
1
defmodule Algora.Contracts do
2
2
@ moduledoc false
3
+ import Algora.Activities.Activity , only: [ put_activity: 2 , put_activity: 3 ]
3
4
import Ecto.Changeset
4
5
import Ecto.Query
5
6
7
+ alias Algora.Activities
6
8
alias Algora.Contracts.Contract
7
9
alias Algora.Contracts.Timesheet
8
10
alias Algora.FeeTier
@@ -170,6 +172,12 @@ defmodule Algora.Contracts do
170
172
total_fee: total_fee ,
171
173
line_items: line_items
172
174
} )
175
+ |> put_assoc (
176
+ :contract ,
177
+ put_activity ( contract , % {
178
+ type: :transaction_created
179
+ } )
180
+ )
173
181
|> Algora.Validations . validate_positive ( :gross_amount )
174
182
|> Algora.Validations . validate_positive ( :net_amount )
175
183
|> Algora.Validations . validate_positive ( :total_fee )
@@ -196,6 +204,12 @@ defmodule Algora.Contracts do
196
204
total_fee: Money . zero ( :USD ) ,
197
205
linked_transaction_id: linked_transaction_id
198
206
} )
207
+ |> put_assoc (
208
+ :contract ,
209
+ put_activity ( contract , % {
210
+ type: :transaction_created
211
+ } )
212
+ )
199
213
|> Algora.Validations . validate_positive ( :gross_amount )
200
214
|> Algora.Validations . validate_positive ( :net_amount )
201
215
|> foreign_key_constraint ( :original_contract_id )
@@ -221,6 +235,12 @@ defmodule Algora.Contracts do
221
235
user_id: contract . contractor_id ,
222
236
linked_transaction_id: linked_transaction_id
223
237
} )
238
+ |> put_assoc (
239
+ :contract ,
240
+ put_activity ( contract , % {
241
+ type: :transaction_created
242
+ } )
243
+ )
224
244
|> Algora.Validations . validate_positive ( :gross_amount )
225
245
|> Algora.Validations . validate_positive ( :net_amount )
226
246
|> foreign_key_constraint ( :original_contract_id )
@@ -245,6 +265,12 @@ defmodule Algora.Contracts do
245
265
timesheet_id: contract . timesheet . id ,
246
266
user_id: contract . contractor_id
247
267
} )
268
+ |> put_assoc (
269
+ :contract ,
270
+ put_activity ( contract , % {
271
+ type: :transaction_created
272
+ } )
273
+ )
248
274
|> Algora.Validations . validate_positive ( :gross_amount )
249
275
|> Algora.Validations . validate_positive ( :net_amount )
250
276
|> foreign_key_constraint ( :original_contract_id )
@@ -377,7 +403,16 @@ defmodule Algora.Contracts do
377
403
with { :ok , txs } <- initialize_prepayment_transaction ( contract ) ,
378
404
{ :ok , invoice } <- maybe_generate_invoice ( contract , txs . charge ) ,
379
405
{ :ok , _invoice } <- maybe_pay_invoice ( contract , invoice , txs ) do
406
+ Activities . insert ( contract , % { type: :contract_prepaid } )
407
+
380
408
{ :ok , txs }
409
+ else
410
+ error ->
411
+ Activities . insert ( contract , % {
412
+ type: :contract_prepayment_failed
413
+ } )
414
+
415
+ error
381
416
end
382
417
end
383
418
@@ -483,6 +518,7 @@ defmodule Algora.Contracts do
483
518
484
519
{ :error , error } ->
485
520
update_transaction_status ( transaction , { :error , error } )
521
+ Activities . insert ( contract , % { type: :contract_prepayment_failed } )
486
522
{ :error , error }
487
523
end
488
524
end
@@ -493,6 +529,13 @@ defmodule Algora.Contracts do
493
529
provider_meta: Util . normalize_struct ( % { error: error } ) ,
494
530
status: :failed
495
531
} )
532
+ |> put_assoc (
533
+ :contract ,
534
+ put_activity ( transaction . contract , % {
535
+ type: :transaction_status_change ,
536
+ meta: % { status: :failed , transaction_id: transaction . id }
537
+ } )
538
+ )
496
539
|> Repo . update ( )
497
540
end
498
541
@@ -504,12 +547,26 @@ defmodule Algora.Contracts do
504
547
status: status ,
505
548
succeeded_at: if ( status == :succeeded , do: DateTime . utc_now ( ) )
506
549
} )
550
+ |> put_assoc (
551
+ :contract ,
552
+ put_activity ( transaction . contract , % {
553
+ type: :transaction_status_change ,
554
+ meta: % { status: status , transaction_id: transaction . id }
555
+ } )
556
+ )
507
557
|> Repo . update ( )
508
558
end
509
559
510
560
defp mark_contract_as_paid ( contract ) do
511
561
contract
512
562
|> change ( % { status: :paid } )
563
+ |> put_activity ( contract , % {
564
+ type: :contract_paid ,
565
+ meta: % { } ,
566
+ template: "" ,
567
+ trace_id: Nanoid . generate ( ) ,
568
+ notify_users: [ ]
569
+ } )
513
570
|> Repo . update ( )
514
571
end
515
572
@@ -527,6 +584,10 @@ defmodule Algora.Contracts do
527
584
hourly_rate: contract . hourly_rate ,
528
585
hours_per_week: contract . hours_per_week
529
586
} )
587
+ |> put_activity ( contract , % {
588
+ type: :contract_renewed ,
589
+ trace_id: Nanoid . generate ( )
590
+ } )
530
591
|> Repo . insert ( )
531
592
end
532
593
@@ -601,6 +662,7 @@ defmodule Algora.Contracts do
601
662
on: tt . original_contract_id == c . original_contract_id ,
602
663
as: :tt
603
664
)
665
+ |> join ( :left , [ c ] , act in assoc ( c , :activities ) , as: :act )
604
666
|> select_merge ( [ ta: ta , tt: tt ] , % {
605
667
amount_credited: Algora.SQL . money_or_zero ( ta . amount_credited ) ,
606
668
amount_debited: Algora.SQL . money_or_zero ( ta . amount_debited ) ,
@@ -611,11 +673,12 @@ defmodule Algora.Contracts do
611
673
total_transferred: Algora.SQL . money_or_zero ( tt . total_transferred ) ,
612
674
total_withdrawn: Algora.SQL . money_or_zero ( tt . total_withdrawn )
613
675
} )
614
- |> preload ( [ ts: ts , txs: txs , cl: cl , ct: ct , cu: cu , dpm: dpm ] ,
676
+ |> preload ( [ ts: ts , txs: txs , cl: cl , ct: ct , cu: cu , dpm: dpm , act: act ] ,
615
677
timesheet: ts ,
616
678
transactions: txs ,
617
679
client: { cl , customer: { cu , default_payment_method: dpm } } ,
618
- contractor: ct
680
+ contractor: ct ,
681
+ activities: act
619
682
)
620
683
|> Repo . all ( )
621
684
|> Enum . map ( & Contract . after_load / 1 )
0 commit comments