meta/lib/recycledcloud_web/controllers/billing_controller.ex
2021-01-20 18:17:16 +01:00

47 lines
1.1 KiB
Elixir

defmodule RecycledCloudWeb.BillingController do
use RecycledCloudWeb, :controller
alias RecycledCloud.Billing.{Partner, Invoice}
alias RecycledCloud.Repo
def current_partner(conn) do
user = conn.assigns.current_user
Partner.get_or_create(user)
end
def index(conn, _params) do
partner = conn |> current_partner
changeset = partner |> Partner.changeset(%{})
invoices = partner |> Invoice.get_all_for
render(
conn,
"index.html",
partner: partner,
partner_changeset: changeset,
invoices: invoices
)
end
def update(conn, %{"partner" => changes} = params) do
partner = conn |> current_partner
changeset = partner |> Partner.changeset(changes)
case Partner.apply(changeset) do
{:ok, _} ->
conn
|> put_flash(:info, "Billing Partner has been updated")
|> redirect(to: Routes.billing_path(conn, :index))
{:error, changeset} ->
invoices = partner |> Invoice.get_all_for
render(
conn,
"index.html",
partner: partner,
partner_changeset: changeset,
invoices: invoices
)
end
end
end