<commit_before>defmodule HELM.Account.Controller do
  import Ecto.Changeset
  import Ecto.Query

  alias HeBroker.Publisher

  alias HELF.{Broker, Error}
  alias HELM.Account.{Repo, Model}

  def find_account(account_id) do
    Model
    |> where([a], a.account_id == ^account_id)
    |> select([a], map(a, [:account_id, :confirmed, :email]))
    |> Repo.one()
    |> case do
      nil -> {:error, :notfound}
      account -> {:ok, account}
    end
  end

  def new_account(account) do
    Model.create_changeset(account)
    |> do_new_account()
  end

  def login_with(account = %{"email" => email, "password" => <PASSWORD>}) do
    do_find_account(email: email, password: <PASSWORD>)
    |> do_login
  end

  def find(email) do
    case Repo.get_by(Account, email: email) do
      nil -> {:reply, {:error, Error.format_reply(:not_found, "Account with given email not found")}}
      res -> {:reply, {:ok, res}}
    end
  end

  def get(request) do
    case Broker.call("jwt:account:verify", request.args["jwt"]) do
      :ok -> find(request.args["email"])
      {:error, reason} -> {:reply, {:error, reason}}
    end
  end

  defp do_find_account(email: email, password: password) do
    Model
    |> where([a], a.email == ^email and a.password == ^password)
    |> select([a], map(a, [:account_id, :confirmed, :email]))
    |> Repo.one()
    |> case do
      nil -> {:error, :notfound}
      account -> {:ok, account}
    end
  end

  defp do_new_account(changeset) do
    Repo.insert(changeset)
  end

  defp do_login({:ok, account}) do
    # TODO Call `jwt:account:create` hebroker topic
    account["account_id"]
  end

  defp do_login(err), do: err

end<commit_msg>Changed jwt:account:create to jwt:create and added login failure handling.<commit_after>defmodule HELM.Account.Controller do
  import Ecto.Changeset
  import Ecto.Query

  alias HeBroker.Publisher

  alias HELF.{Broker, Error}
  alias HELM.Account.{Repo, Model}

  def find_account(account_id) do
    Model
    |> where([a], a.account_id == ^account_id)
    |> select([a], map(a, [:account_id, :confirmed, :email]))
    |> Repo.one()
    |> case do
      nil -> {:error, :notfound}
      account -> {:ok, account}
    end
  end

  def new_account(account) do
    Model.create_changeset(account)
    |> do_new_account()
  end

  def login_with(account = %{"email" => email, "password" => <PASSWORD>}) do
    do_find_account(email: email, password: <PASSWORD>)
    |> do_login
  end

  def find(email) do
    case Repo.get_by(Account, email: email) do
      nil -> {:reply, {:error, Error.format_reply(:not_found, "Account with given email not found")}}
      res -> {:reply, {:ok, res}}
    end
  end

  def get(request) do
    case Broker.call("jwt:account:verify", request.args["jwt"]) do
      :ok -> find(request.args["email"])
      {:error, reason} -> {:reply, {:error, reason}}
    end
  end

  defp do_find_account(email: email, password: password) do
    Model
    |> where([a], a.email == ^email and a.password == ^password)
    |> select([a], map(a, [:account_id, :confirmed, :email]))
    |> Repo.one()
    |> case do
      nil -> {:error, :notfound}
      account -> {:ok, account}
    end
  end

  defp do_new_account(changeset) do
    Repo.insert(changeset)
  end

  defp do_login({:ok, account}) do
    Broker.call("jwt:create", account.account_id)
  end

  defp do_login({:error, err}) do
    case err do
      :notfound ->
        {:error, Error.format_reply(:unauthorized, "Account not found.")}
      _ ->
        {:error, Error.format_reply(:unspecified, "oh god")}
    end
  end
end
{'fraction_non_alphanumeric': 0.15244755244755245, 'fraction_numerical': 0.0, 'mean_word_length': 3.1922626025791323, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208414', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1227, 'n_words': 312}