Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Created February 2, 2019 08:56
Show Gist options
  • Save zacck-zz/e282dbf34db587d0b882134076a06d01 to your computer and use it in GitHub Desktop.
Save zacck-zz/e282dbf34db587d0b882134076a06d01 to your computer and use it in GitHub Desktop.
@spec create_merchant(map()) :: {:ok, Merchant.t()} | {:error, Ecto.Changeset.t()}
def create_merchant(attrs) do
temp_password = generate_password(8)
account_params = %{email: attrs.email, password: temp_password, status: 0, group: "merchant"}
Multi.new
|> Multi.insert(:account, Account.create_changeset(account_params))
|> Multi.run(:merchant, fn repo, %{account: acc} ->
repo.insert(Merchant.create_changeset(Map.put(attrs, :account, acc)))
end)
|> Repo.transaction()
|> case do
{:ok, %{merchant: mct}} ->
Notifications.new_lead_email(mct) |> @mail_api.deliver_now()
Notifications.password_email(%{mct | password: temp_password})
{:ok, mct}
{:error, :account, acc_res, _} ->
{:error, acc_res}
{:error, :merchant, mct_res, _} ->
{:error, mct_res}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment