Skip to content

Commit

Permalink
Run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaslong authored and Oliver Azevedo Barnes committed Jun 26, 2020
1 parent 3029f93 commit ab49f0a
Show file tree
Hide file tree
Showing 34 changed files with 428 additions and 229 deletions.
4 changes: 1 addition & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use Mix.Config

config :liquid_voting, LiquidVotingWeb.Endpoint,
url: [
host: System.get_env("APP_HOSTNAME") || "localhost" ,
host: System.get_env("APP_HOSTNAME") || "localhost",
port: String.to_integer(System.get_env("APP_PORT") || "4000")
],
server: true

config :logger, level: :info

config :liquid_voting, LiquidVotingWeb.Endpoint, server: true


4 changes: 2 additions & 2 deletions config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Config

db_user = System.fetch_env!("DB_USERNAME")
db_password = System.fetch_env!("DB_PASSWORD")
db_name= System.fetch_env!("DB_NAME")
db_host= System.fetch_env!("DB_HOST")
db_name = System.fetch_env!("DB_NAME")
db_host = System.fetch_env!("DB_HOST")
db_pool_size = System.get_env("DB_POOL_SIZE") || "10"

port = System.get_env("APP_PORT") || "4000"
Expand Down
22 changes: 16 additions & 6 deletions lib/liquid_voting/delegations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule LiquidVoting.Delegations do

alias LiquidVoting.Delegations.Delegation
alias LiquidVoting.Voting

@doc """
Returns the list of delegations for an organization uuid
Expand All @@ -20,8 +21,8 @@ defmodule LiquidVoting.Delegations do
def list_delegations(organization_uuid) do
Delegation
|> where(organization_uuid: ^organization_uuid)
|> Repo.all
|> Repo.preload([:delegator,:delegate])
|> Repo.all()
|> Repo.preload([:delegator, :delegate])
end

@doc """
Expand All @@ -41,7 +42,7 @@ defmodule LiquidVoting.Delegations do
def get_delegation!(id, organization_uuid) do
Delegation
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
|> Repo.preload([:delegator,:delegate])
|> Repo.preload([:delegator, :delegate])
end

@doc """
Expand All @@ -63,7 +64,12 @@ defmodule LiquidVoting.Delegations do
delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid)

Delegation
|> Repo.get_by!([delegator_id: delegator.id, delegate_id: delegate.id, proposal_url: proposal_url, organization_uuid: organization_uuid])
|> Repo.get_by!(
delegator_id: delegator.id,
delegate_id: delegate.id,
proposal_url: proposal_url,
organization_uuid: organization_uuid
)
end

@doc """
Expand All @@ -85,7 +91,11 @@ defmodule LiquidVoting.Delegations do
delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid)

Delegation
|> Repo.get_by!([delegator_id: delegator.id, delegate_id: delegate.id, organization_uuid: organization_uuid])
|> Repo.get_by!(
delegator_id: delegator.id,
delegate_id: delegate.id,
organization_uuid: organization_uuid
)
end

@doc """
Expand All @@ -109,7 +119,7 @@ defmodule LiquidVoting.Delegations do
def create_delegation!(attrs \\ %{}) do
%Delegation{}
|> Delegation.changeset(attrs)
|> Repo.insert
|> Repo.insert()
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid_voting/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ end

defmodule LiquidVoting.Metrics.PrometheusExporter do
use Prometheus.PlugExporter
end
end
2 changes: 1 addition & 1 deletion lib/liquid_voting/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ defmodule LiquidVoting.Release do
Application.load(@app)
Application.fetch_env!(@app, :ecto_repos)
end
end
end
55 changes: 32 additions & 23 deletions lib/liquid_voting/voting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule LiquidVoting.Voting do
import Ecto.Query, warn: false
alias LiquidVoting.Repo

alias LiquidVoting.Voting.{Vote,Participant}
alias LiquidVoting.Voting.{Vote, Participant}
alias LiquidVoting.Delegations
alias LiquidVoting.Delegations.Delegation

Expand All @@ -24,22 +24,26 @@ defmodule LiquidVoting.Voting do
"""
def create_vote(attrs \\ %{}) do
Repo.transaction(
fn ->
case %Vote{} |> Vote.changeset(attrs) |> Repo.insert() do
{:ok, vote} ->
if delegation = Repo.get_by(Delegation, [delegator_id: attrs[:participant_id], organization_uuid: attrs[:organization_uuid]]) do
case Delegations.delete_delegation(delegation) do
{:ok, _delegation} -> vote
{:error, changeset} -> Repo.rollback(changeset)
end
else
vote
Repo.transaction(fn ->
case %Vote{} |> Vote.changeset(attrs) |> Repo.insert() do
{:ok, vote} ->
if delegation =
Repo.get_by(Delegation,
delegator_id: attrs[:participant_id],
organization_uuid: attrs[:organization_uuid]
) do
case Delegations.delete_delegation(delegation) do
{:ok, _delegation} -> vote
{:error, changeset} -> Repo.rollback(changeset)
end
{:error, changeset} -> Repo.rollback(changeset)
end
else
vote
end

{:error, changeset} ->
Repo.rollback(changeset)
end
)
end)
end

@doc """
Expand Down Expand Up @@ -69,7 +73,7 @@ defmodule LiquidVoting.Voting do
"""
def list_votes(proposal_url, organization_uuid) do
Vote
|> where([proposal_url: ^proposal_url, organization_uuid: ^organization_uuid])
|> where(proposal_url: ^proposal_url, organization_uuid: ^organization_uuid)
|> Repo.all()
|> Repo.preload([:participant])
end
Expand All @@ -90,7 +94,7 @@ defmodule LiquidVoting.Voting do
"""
def get_vote!(id, organization_uuid) do
Vote
|> Repo.get_by!([id: id, organization_uuid: organization_uuid])
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
|> Repo.preload([:participant])
end

Expand All @@ -109,9 +113,14 @@ defmodule LiquidVoting.Voting do
"""
def get_vote!(email, proposal_url, organization_uuid) do
participant = get_participant_by_email!(email, organization_uuid)

Vote
|> Repo.get_by!([participant_id: participant.id, proposal_url: proposal_url, organization_uuid: organization_uuid])
|> Repo.preload([:participant])
|> Repo.get_by!(
participant_id: participant.id,
proposal_url: proposal_url,
organization_uuid: organization_uuid
)
|> Repo.preload([:participant])
end

# Just for seeding
Expand Down Expand Up @@ -215,7 +224,7 @@ defmodule LiquidVoting.Voting do
"""
def get_participant!(id, organization_uuid) do
Participant
|> Repo.get_by!([id: id, organization_uuid: organization_uuid])
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
end

@doc """
Expand All @@ -234,7 +243,7 @@ defmodule LiquidVoting.Voting do
"""
def get_participant_by_email(email, organization_uuid) do
Participant
|> Repo.get_by([email: email, organization_uuid: organization_uuid])
|> Repo.get_by(email: email, organization_uuid: organization_uuid)
end

@doc """
Expand All @@ -253,7 +262,7 @@ defmodule LiquidVoting.Voting do
"""
def get_participant_by_email!(email, organization_uuid) do
Participant
|> Repo.get_by!([email: email, organization_uuid: organization_uuid])
|> Repo.get_by!(email: email, organization_uuid: organization_uuid)
end

@doc """
Expand Down Expand Up @@ -286,7 +295,7 @@ defmodule LiquidVoting.Voting do
|> Repo.insert(
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:organization_uuid, :email]
)
)
end

@doc """
Expand Down
12 changes: 6 additions & 6 deletions lib/liquid_voting/voting_results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ defmodule LiquidVoting.VotingResults do
}

attrs =
Enum.reduce votes, attrs, fn (vote, attrs) ->
Enum.reduce(votes, attrs, fn vote, attrs ->
{:ok, vote} = VotingWeight.update_vote_weight(vote)

if vote.yes do
Map.update!(attrs, :in_favor, &(&1 + vote.weight))
else
Map.update!(attrs, :against, &(&1 + vote.weight))
end
end
end)

%Result{}
|> Result.changeset(attrs)
|> Repo.insert!(
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:organization_uuid, :proposal_url]
)
)
end

@doc """
Expand Down Expand Up @@ -98,7 +98,7 @@ defmodule LiquidVoting.VotingResults do
"""
def get_result!(id, organization_uuid) do
Result
|> Repo.get_by!([id: id, organization_uuid: organization_uuid])
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
end

@doc """
Expand All @@ -117,7 +117,7 @@ defmodule LiquidVoting.VotingResults do
"""
def get_result_by_proposal_url(proposal_url, organization_uuid) do
Result
|> Repo.get_by([proposal_url: proposal_url, organization_uuid: organization_uuid])
|> Repo.get_by(proposal_url: proposal_url, organization_uuid: organization_uuid)
end

@doc """
Expand All @@ -141,6 +141,6 @@ defmodule LiquidVoting.VotingResults do
def create_result!(attrs \\ %{}) do
%Result{}
|> Result.changeset(attrs)
|> Repo.insert!
|> Repo.insert!()
end
end
4 changes: 3 additions & 1 deletion lib/liquid_voting/voting_results/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defmodule LiquidVoting.VotingResults.Result do
result
|> cast(attrs, all_fields)
|> validate_required(required_fields)
|> unique_constraint(:organization_uuid_proposal_url, name: :uniq_index_organization_uuid_proposal_url)
|> unique_constraint(:organization_uuid_proposal_url,
name: :uniq_index_organization_uuid_proposal_url
)
end
end
16 changes: 8 additions & 8 deletions lib/liquid_voting/voting_weight.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ defmodule LiquidVoting.VotingWeight do
"""
def update_vote_weight(vote) do
# :force it because sometimes votes come in with stale associations
vote = Repo.preload(vote, [participant: :delegations_received], force: true)
vote = Repo.preload(vote, [participant: :delegations_received], force: true)
voter = vote.participant

weight = 1 + delegation_weight(voter.delegations_received, vote.proposal_url)

Voting.update_vote(vote, %{weight: weight})
Expand All @@ -38,7 +38,7 @@ defmodule LiquidVoting.VotingWeight do
# Traverse up delegation tree and add up the accumulated weight.
# If you're wondering, [_|_] matches a non-empty array. Equivalent to
# matching [head|tail] but ignoring both head and tail variables
defp delegation_weight(delegations = [_|_], proposal_url, weight) do
defp delegation_weight(delegations = [_ | _], proposal_url, weight) do
# TODO: Do this in SQL
#
# Not sure yet which tree/hierarchy handling pattern would fit here,
Expand All @@ -51,22 +51,22 @@ defmodule LiquidVoting.VotingWeight do
#
# Add-up 1 unit of weight for each delegation,
# then recurse on each delegators' own delegations
Enum.reduce delegations, weight, fn (delegation, weight) ->
Enum.reduce(delegations, weight, fn delegation, weight ->
# Only proceed if delegation is global or is meant for the
# proposal being voted on:
if delegation.proposal_url == proposal_url || delegation.proposal_url == nil do
delegation = Repo.preload(delegation, [delegator: :delegations_received])
delegation = Repo.preload(delegation, delegator: :delegations_received)
delegator = delegation.delegator

weight = weight + 1

delegation_weight(delegator.delegations_received, proposal_url, weight)

# If delegation is for a different proposal, just return the unchanged weight
# If delegation is for a different proposal, just return the unchanged weight
else
weight
end
end
end)
end

# Base case for the above recursion:
Expand All @@ -75,4 +75,4 @@ defmodule LiquidVoting.VotingWeight do
defp delegation_weight(_ = [], _, weight) do
weight
end
end
end
2 changes: 1 addition & 1 deletion lib/liquid_voting_web/plugs/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ defmodule LiquidVotingWeb.Plugs.Context do
_ -> %{}
end
end
end
end
Loading

0 comments on commit ab49f0a

Please sign in to comment.