Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
adding dialyzer and addressing some of the issues (#202)
Browse files Browse the repository at this point in the history
* adding dialyzer and addressing some of the issues
* fixed formatting
* unmatched expressions
* overlaping domains and return specs
* fixing specs and removing dead code
* addresing comments
* get by
  • Loading branch information
Ino Murko authored and unnawut committed Jun 12, 2018
1 parent 5aa6f74 commit 1ff3915
Show file tree
Hide file tree
Showing 34 changed files with 132 additions and 111 deletions.
18 changes: 18 additions & 0 deletions .dialyzer_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Unknown function 'Elixir.IEx':'started?'/0
Unknown function 'Elixir.Mix':env/0
Unknown function 'Elixir.Mix':shell/0
Unknown function 'Elixir.Mix.Task':run/2
Unknown function 'Elixir.Mix.Tasks.Run':run/1
Unknown type 'Elixir.Account':t/0
Unknown type 'Elixir.Atom':t/0
Unknown type 'Elixir.Boolean':t/0
Unknown type 'Elixir.Conn':t/0
Unknown type 'Elixir.Ecto.UUID':t/0
Unknown type 'Elixir.Integer':t/0
Unknown type 'Elixir.List':t/0
Unknown type 'Elixir.Map':t/0
Function 'iex_running?'/0 will never be called
Function run/1 has no local return
Function configure_no_halt/1 will never be called
Callback info about the 'Elixir.Mix.Task' behaviour is not available
Function run/1 has no local return
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elixir 1.6.5
2 changes: 1 addition & 1 deletion apps/admin_api/lib/admin_api/invites/inviter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule AdminAPI.Inviter do

{:ok, _membership} = Membership.assign(invite.user, account, role)

send_email(invite, redirect_url)
_ = send_email(invite, redirect_url)
{:ok, invite}
catch
error when is_atom(error) -> {:error, error}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ defmodule AdminAPI.V1.AccountMembershipController do
"redirect_url" => redirect_url
} = attrs
) do
with user when not is_tuple(user) <- get_user_or_email(attrs) || {:error, :user_id_not_found},
with user <- get_user_or_email(attrs),
{false, :user_id_not_found} <- {is_tuple(user), :user_id_not_found},
%Account{} = account <- Account.get(account_id) || {:error, :account_id_not_found},
%Role{} = role <- Role.get_by_name(role_name) || {:error, :role_name_not_found},
{:ok, _} <- assign_or_invite(user, account, role, redirect_url) do
render(conn, :empty, %{success: true})
else
{true, :user_id_not_found} ->
handle_error(conn, :user_id_not_found)

{:error, error} when is_atom(error) ->
handle_error(conn, error)

Expand Down Expand Up @@ -70,7 +74,14 @@ defmodule AdminAPI.V1.AccountMembershipController do
end
end

defp assign_or_invite(%User{} = user, account, role, redirect_url) do
defp assign_or_invite(email, account, role, redirect_url) when is_binary(email) do
case Inviter.invite(email, account, role, redirect_url) do
{:ok, invite} -> {:ok, invite.user}
{:error, _} = error -> error
end
end

defp assign_or_invite(user, account, role, redirect_url) do
case User.get_status(user) do
:pending_confirmation ->
invite =
Expand All @@ -85,13 +96,6 @@ defmodule AdminAPI.V1.AccountMembershipController do
end
end

defp assign_or_invite(email, account, role, redirect_url) when is_binary(email) do
case Inviter.invite(email, account, role, redirect_url) do
{:ok, invite} -> {:ok, invite.user}
{:error, _} = error -> error
end
end

@doc """
Unassigns the user from the given account.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ defmodule AdminAPI.V1.ResetPasswordController do
handle_error(conn, :user_email_not_found)

user ->
user
|> ForgetPasswordRequest.delete_all()
|> ForgetPasswordRequest.generate()
|> ForgetPasswordEmail.create(redirect_url)
|> Mailer.deliver_now()
_ =
user
|> ForgetPasswordRequest.delete_all()
|> ForgetPasswordRequest.generate()
|> ForgetPasswordEmail.create(redirect_url)
|> Mailer.deliver_now()

render(conn, :empty, %{success: true})
end
Expand All @@ -34,7 +35,7 @@ defmodule AdminAPI.V1.ResetPasswordController do
with %User{} = user <- get_user(email),
%ForgetPasswordRequest{} = request <- get_request(user, token),
{:ok, %User{} = user} <- update_password(request, attrs) do
ForgetPasswordRequest.delete_all(user)
_ = ForgetPasswordRequest.delete_all(user)
render(conn, :empty, %{success: true})
else
error when is_atom(error) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ defmodule AdminAPI.V1.UserController do

# Respond with a single user
defp respond_single(%User{} = user, conn), do: render(conn, :user, %{user: user})
# Responds when the given params were invalid
defp respond_single({:error, changeset}, conn) do
handle_error(conn, :invalid_parameter, changeset)
end

# Responds when the user is not found
defp respond_single(nil, conn), do: handle_error(conn, :user_id_not_found)
Expand Down
4 changes: 0 additions & 4 deletions apps/ewallet/lib/ewallet/fetchers/uuid_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ defmodule EWallet.UUIDFetcher do
|> Enum.into(%{})
end

defp replace_external_id(key, value, external_id: true) when is_atom(key) do
replace_external_id(Atom.to_string(key), value, external_id: true)
end

defp replace_external_id(key, value, external_id: true) do
schema = @mappings[key]
uuid_key = String.replace(key, "_id", "_uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ defmodule EWallet.TransactionConsumptionConsumerGate do

def consume(_attrs), do: {:error, :invalid_parameter}

@spec consume(User.t(), Map.t()) :: {:ok, TransactionConsumption.t()} | {:error, Atom.t()}
@spec consume(User.t() | Balance.t(), Map.t()) ::
{:ok, TransactionConsumption.t()} | {:error, Atom.t()}
def consume(
%User{} = user,
%{
Expand All @@ -109,7 +110,6 @@ defmodule EWallet.TransactionConsumptionConsumerGate do
end
end

@spec consume(Balance.t(), Map.t()) :: {:ok, TransactionConsumption.t()} | {:error, Atom.t()}
def consume(
%Wallet{} = wallet,
%{
Expand Down
16 changes: 7 additions & 9 deletions apps/ewallet/lib/ewallet/gates/transaction_request_gate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ defmodule EWallet.TransactionRequestGate do
alias EWalletDB.{TransactionRequest, User, Wallet, Token, Account}

@spec create(Map.t()) :: {:ok, TransactionRequest.t()} | {:error, Atom.t()}

def create(
%{
"account_id" => account_id,
"provider_user_id" => provider_user_id,
"address" => address
} = attrs
) do
with %Account{} = account <- Account.get(account_id) || :account_id_not_found,
with %Account{} = account <- Account.get(account_id) || {:error, :account_id_not_found},
%User{} = user <-
User.get_by_provider_user_id(provider_user_id) || :provider_user_id_not_found,
User.get_by_provider_user_id(provider_user_id) || {:error, :provider_user_id_not_found},
{:ok, wallet} <- WalletFetcher.get(user, address),
wallet <- Map.put(wallet, :account_uuid, account.uuid),
{:ok, transaction_request} <- create(wallet, attrs) do
TransactionRequestFetcher.get(transaction_request.id)
else
error when is_atom(error) -> {:error, error}
error -> error
end
end
Expand All @@ -37,7 +35,7 @@ defmodule EWallet.TransactionRequestGate do
"address" => address
} = attrs
) do
with %Account{} = account <- Account.get(account_id) || :account_id_not_found,
with %Account{} = account <- Account.get(account_id) || {:error, :account_id_not_found},
{:ok, wallet} <- WalletFetcher.get(account, address),
{:ok, transaction_request} <- create(wallet, attrs) do
TransactionRequestFetcher.get(transaction_request.id)
Expand All @@ -60,7 +58,7 @@ defmodule EWallet.TransactionRequestGate do
} = attrs
) do
with %User{} = user <-
User.get_by_provider_user_id(provider_user_id) || :provider_user_id_not_found,
User.get_by_provider_user_id(provider_user_id) || {:error, :provider_user_id_not_found},
{:ok, wallet} <- WalletFetcher.get(user, address),
{:ok, transaction_request} <- create(wallet, attrs) do
TransactionRequestFetcher.get(transaction_request.id)
Expand Down Expand Up @@ -92,7 +90,8 @@ defmodule EWallet.TransactionRequestGate do

def create(_), do: {:error, :invalid_parameter}

@spec create(User.t(), Map.t()) :: {:ok, TransactionRequest.t()} | {:error, Atom.t()}
@spec create(User.t() | Wallet.t(), Map.t()) ::
{:ok, TransactionRequest.t()} | {:error, Atom.t()}
def create(
%User{} = user,
%{
Expand All @@ -106,7 +105,6 @@ defmodule EWallet.TransactionRequestGate do
end
end

@spec create(Wallet.t(), Map.t()) :: {:ok, TransactionRequest.t()} | {:error, Atom.t()}
def create(
%Wallet{} = wallet,
%{
Expand All @@ -116,7 +114,7 @@ defmodule EWallet.TransactionRequestGate do
"token_id" => token_id
} = attrs
) do
with %Token{} = token <- Token.get(token_id) || :token_not_found,
with %Token{} = token <- Token.get(token_id) || {:error, :token_not_found},
{:ok, transaction_request} <- insert(token, wallet, attrs) do
TransactionRequestFetcher.get(transaction_request.id)
else
Expand Down
4 changes: 2 additions & 2 deletions apps/ewallet/lib/ewallet/seeders/cli_seeder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ defmodule EWallet.Seeder.CLI do
|> Seeder.argsline_for()
|> process_argsline()

IO.puts("\n-----\n")
IO.gets(@confirm_message)
_ = IO.puts("\n-----\n")
_ = IO.gets(@confirm_message)

args = run_seeds(mods, args)
run_reporters(reporters, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule EWallet.TransactionConsumptionValidator do
expiration.
"""
alias EWallet.Web.V1.Event
alias EWalletDB.{Repo, Balance, TransactionRequest, TransactionConsumption, Token}
alias EWalletDB.{Repo, TransactionRequest, TransactionConsumption, Token}

@spec validate_before_consumption(TransactionRequest.t(), Balance.t(), Integer.t()) ::
{:ok, TransactionRequest.t(), Integer.t()}
@spec validate_before_consumption(TransactionRequest.t(), any(), nil | keyword() | map()) ::
{:ok, TransactionRequest.t(), Token.t(), integer()}
| {:error, Atom.t()}
def validate_before_consumption(request, wallet, attrs) do
with amount <- attrs["amount"],
Expand Down
2 changes: 1 addition & 1 deletion apps/ewallet/lib/ewallet/web/email_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule EWallet.EmailValidator do
Checks whether the email address looks correct.
Returns the email string if valid, returns false if invalid.
"""
@spec valid?(String.t()) :: String.t() | false
@spec validate(String.t()) :: String.t() | false
def validate(email) do
if Regex.match?(@email_regex, email), do: email, else: false
end
Expand Down
2 changes: 2 additions & 0 deletions apps/ewallet/lib/ewallet/web/paginator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule EWallet.Web.Paginator do
(so long as the attribute keys don't conflict). Therefore this function
expects attribute keys to be strings, not atoms.
"""
@spec paginate_attrs(Ecto.Query.t() | Ecto.Queryable.t(), map()) ::
map() | {:error, :invalid_parameter, String.t()}
def paginate_attrs(queryable, %{"page" => page} = attrs) when not is_integer(page) do
parse_string_param(queryable, attrs, "page", page)
end
Expand Down
2 changes: 1 addition & 1 deletion apps/ewallet/lib/ewallet/web/preloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule EWallet.Web.Preloader do
@doc """
Preload the given list of associations.
"""
@spec to_query(Ecto.Query.t(), List.t()) :: {Ecto.Query.t()}
@spec to_query(Ecto.Queryable.t(), List.t()) :: {Ecto.Query.t()}
def to_query(queryable, preload_fields) when is_list(preload_fields) do
import Ecto.Query
from(q in queryable, preload: ^preload_fields)
Expand Down
13 changes: 8 additions & 5 deletions apps/ewallet/lib/ewallet/web/v1/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ defmodule EWallet.Web.V1.ErrorHandler do
@doc """
Returns a map of all the error atoms along with their code and description.
"""
@spec errors() :: %{required(atom()) => %{code: String.t(), description: String.t()}}
@spec errors() :: %{
required(Atom.t()) => %{
required(Atom.t()) => String.t(),
required(Atom.t()) => String.t()
}
}
def errors, do: @errors

# ---- WITH CHANGESET ----
@doc """
Handles response of invalid parameter error with error details provided.
"""
@spec build_error(String.t(), Ecto.Changeset.t(), Map.t()) :: Map.t()
@spec build_error(String.t() | Atom.t(), Map.t() | Ecto.Changeset.t() | String.t(), Map.t()) ::
Map.t()
def build_error(code, %Changeset{} = changeset, supported_errors) do
run_if_valid_error(code, supported_errors, fn error ->
build(
Expand All @@ -174,7 +180,6 @@ defmodule EWallet.Web.V1.ErrorHandler do
@doc """
Handles response with custom error code and description.
"""
@spec build_error(Atom.t(), String.t(), Map.t()) :: Map.t()
def build_error(code, description, supported_errors)
when is_binary(description)
when is_atom(description) do
Expand All @@ -187,7 +192,6 @@ defmodule EWallet.Web.V1.ErrorHandler do
@doc """
Handles response of insufficient_funds.
"""
@spec build_error(Atom.t(), Map.t(), Map.t()) :: Map.t()
def build_error(
code,
%{
Expand Down Expand Up @@ -215,7 +219,6 @@ defmodule EWallet.Web.V1.ErrorHandler do
@doc """
Handles response with template description to build.
"""
@spec build_error(Atom.t(), Map.t(), Map.t()) :: Map.t()
def build_error(code, data, supported_errors) when is_map(data) do
run_if_valid_error(code, supported_errors, fn error ->
build(code: error.code, desc: build_template(data, error.template))
Expand Down
27 changes: 14 additions & 13 deletions apps/ewallet/lib/ewallet/web/v1/event_handlers/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule EWallet.Web.V1.Event do
topics: topics,
payload: payload
) do
log(event, topics, payload)
_ = log(event, topics, payload)

Enum.each(topics, fn topic ->
Enum.each(endpoints(), fn endpoint ->
Expand All @@ -31,30 +31,31 @@ defmodule EWallet.Web.V1.Event do
end

def log(event, topics, payload) do
Logger.info("")
Logger.info("WEBSOCKET EVENT: Dispatching event '#{event}' to:")
Logger.info("-- Endpoints:")
_ = Logger.info("")
_ = Logger.info("WEBSOCKET EVENT: Dispatching event '#{event}' to:")
_ = Logger.info("-- Endpoints:")

Enum.each(endpoints(), fn endpoint ->
Logger.info("---- #{endpoint}")
end)

Logger.info("-- Channels:")
_ = Logger.info("-- Channels:")

Enum.each(topics, fn topic ->
Logger.info("---- #{topic}")
end)

case payload[:error] do
nil ->
Logger.info("With no errors.")
_ =
case payload[:error] do
nil ->
Logger.info("With no errors.")

error ->
Logger.info("With error:")
error |> inspect() |> Logger.info()
end
error ->
_ = Logger.info("With error:")
error |> inspect() |> Logger.info()
end

Logger.info("Ending event dispatch...")
_ = Logger.info("Ending event dispatch...")
Logger.info("")
end

Expand Down
2 changes: 1 addition & 1 deletion apps/ewallet_api/lib/ewallet_api/websocket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule EWalletAPI.WebSocket do

defp code_reload(conn, opts, endpoint) do
reload? = Keyword.get(opts, :code_reloader, endpoint.config(:code_reloader))
if reload?, do: CodeReloader.reload!(endpoint)
_ = if reload?, do: CodeReloader.reload!(endpoint)

conn
end
Expand Down
Loading

0 comments on commit 1ff3915

Please sign in to comment.