meta/lib/meta_web/controllers/billing_controller.ex
2021-04-12 15:44:12 +02:00

47 lines
1.1 KiB
Elixir

defmodule MetaWeb.BillingController do
use MetaWeb, :controller
alias Meta.Billing.{Partner, Invoice}
alias Meta.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