@@ -2,12 +2,13 @@ defmodule LiquidVoting.Voting do
22 @ moduledoc """
33 The Voting context.
44 """
5+
56 require OpenTelemetry.Tracer , as: Tracer
67
78 import Ecto.Query , warn: false
89
9- alias __MODULE__ . { Vote , Participant }
10- alias LiquidVoting . { Repo , Delegations }
10+ alias __MODULE__ . { Participant , Vote }
11+ alias LiquidVoting . { Delegations , Repo }
1112 alias LiquidVoting.Delegations.Delegation
1213
1314 @ doc """
@@ -24,31 +25,42 @@ defmodule LiquidVoting.Voting do
2425
2526 """
2627 def create_vote ( attrs \\ % { } ) do
27- Repo . transaction ( fn ->
28- # TODO: refactor case statements into small functions.
28+ Tracer . with_span "#{ __MODULE__ } #{ inspect ( __ENV__ . function ) } " do
29+ Tracer . set_attributes ( [
30+ { :request_id , Logger . metadata ( ) [ :request_id ] } ,
31+ { :params ,
32+ [
33+ { :organization_id , attrs [ :organization_id ] } ,
34+ { :participant_email , attrs [ :participant_email ] } ,
35+ { :proposal_url , attrs [ :proposal_url ] } ,
36+ { :yes , attrs [ :yes ] }
37+ ] }
38+ ] )
2939
30- % Vote { }
31- |> Vote . changeset ( attrs )
32- |> Repo . insert ( )
33- |> case do
34- { :ok , vote } ->
35- if delegation =
36- Repo . get_by ( Delegation ,
37- delegator_id: attrs [ :participant_id ] ,
38- organization_id: attrs [ :organization_id ]
39- ) do
40- case Delegations . delete_delegation ( delegation ) do
41- { :ok , _delegation } -> vote
42- { :error , changeset } -> Repo . rollback ( changeset )
40+ Repo . transaction ( fn ->
41+ % Vote { }
42+ |> Vote . changeset ( attrs )
43+ |> Repo . insert ( )
44+ |> case do
45+ { :ok , vote } ->
46+ if delegation =
47+ Repo . get_by ( Delegation ,
48+ delegator_id: attrs [ :participant_id ] ,
49+ organization_id: attrs [ :organization_id ]
50+ ) do
51+ case Delegations . delete_delegation ( delegation ) do
52+ { :ok , _delegation } -> vote
53+ { :error , changeset } -> Repo . rollback ( changeset )
54+ end
55+ else
56+ vote
4357 end
44- else
45- vote
46- end
47-
48- { :error , changeset } ->
49- Repo . rollback ( changeset )
50- end
51- end )
58+
59+ { :error , changeset } ->
60+ Repo . rollback ( changeset )
61+ end
62+ end )
63+ end
5264 end
5365
5466 @ doc """
@@ -61,11 +73,10 @@ defmodule LiquidVoting.Voting do
6173
6274 """
6375 def list_votes ( organization_id ) do
64- Tracer . with_span "LV/voting " do
76+ Tracer . with_span "#{ __MODULE__ } #{ inspect ( __ENV__ . function ) } " do
6577 Tracer . set_attributes ( [
66- { :action , "list_votes" } ,
6778 { :request_id , Logger . metadata ( ) [ :request_id ] } ,
68- { :organization_id , organization_id }
79+ { :params , [ { : organization_id, organization_id } ] }
6980 ] )
7081
7182 Vote
0 commit comments