admin donations

This commit is contained in:
byte[] 2019-12-16 22:24:40 -05:00
parent a3e4706597
commit c45a2a16d9
10 changed files with 144 additions and 2 deletions

View file

@ -11,7 +11,7 @@ defmodule Philomena.Donations.Donation do
field :amount, :decimal
field :fee, :decimal
field :txn_id, :string
field :reciept_id, :string
field :receipt_id, :string
field :note, :string
timestamps(inserted_at: :created_at)
@ -20,7 +20,8 @@ defmodule Philomena.Donations.Donation do
@doc false
def changeset(donation, attrs) do
donation
|> cast(attrs, [])
|> cast(attrs, [:email, :amount, :note, :user_id])
|> validate_required([])
|> foreign_key_constraint(:user_id, name: :fk_rails_5470822a00)
end
end

View file

@ -26,6 +26,7 @@ defmodule Philomena.Users.User do
alias Philomena.UserFingerprints.UserFingerprint
alias Philomena.UserIps.UserIp
alias Philomena.Bans.User, as: UserBan
alias Philomena.Donations.Donation
@derive {Phoenix.Param, key: :slug}
@ -41,6 +42,7 @@ defmodule Philomena.Users.User do
has_many :user_ips, UserIp
has_many :user_fingerprints, UserFingerprint
has_many :bans, UserBan
has_many :donations, Donation
has_one :commission, Commission
many_to_many :roles, Role, join_through: "users_roles", on_replace: :delete

View file

@ -0,0 +1,23 @@
defmodule PhilomenaWeb.Admin.Donation.UserController do
use PhilomenaWeb, :controller
alias Philomena.Users.User
alias Philomena.Donations.Donation
alias Philomena.Donations
plug :verify_authorized
plug :load_resource, model: User, id_field: "slug", persisted: true, preload: [donations: :user]
def show(conn, _params) do
user = conn.assigns.user
changeset = Donations.change_donation(%Donation{})
render(conn, "index.html", title: "Donations for User `#{user.name}'", donations: user.donations, changeset: changeset)
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do
true -> conn
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end

View file

@ -0,0 +1,41 @@
defmodule PhilomenaWeb.Admin.DonationController do
use PhilomenaWeb, :controller
alias Philomena.Donations.Donation
alias Philomena.Donations
alias Philomena.Repo
import Ecto.Query
plug :verify_authorized
def index(conn, _params) do
donations =
Donation
|> order_by(desc: :created_at, asc: :user_id)
|> preload(:user)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - Donations", donations: donations)
end
def create(conn, %{"donation" => donation_params}) do
case Donations.create_donation(donation_params) do
{:ok, _donation} ->
conn
|> put_flash(:info, "Donation successfully created.")
|> redirect(to: Routes.admin_donation_path(conn, :index))
_error ->
conn
|> put_flash(:error, "Error creating donation!")
|> redirect(to: Routes.admin_donation_path(conn, :index))
end
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, Donation) do
true -> conn
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end

View file

@ -227,6 +227,8 @@ defmodule PhilomenaWeb.Router do
end
resources "/batch/tags", Batch.TagController, only: [:update], singleton: true
resources "/donations", DonationController, only: [:index, :create]
resources "/donations/user", Donation.UserController, only: [:show]
end
resources "/duplicate_reports", DuplicateReportController, only: [] do

View file

@ -0,0 +1,27 @@
table.table
thead
tr
th User
th Email
th Amount
th Note
th At
tbody
= for donation <- @donations do
tr
td
= if donation.user do
= link donation.user.name, to: Routes.profile_path(@conn, :show, donation.user)
td
= donation.email
td
= donation.amount
td title=donation.note
= String.slice(donation.note, 0, 30)
td
= pretty_time donation.created_at

View file

@ -0,0 +1,14 @@
- route = fn p -> Routes.admin_donation_path(@conn, :index, p) end
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @donations, route: route, conn: @conn
h1 Donations
.block
.block__header
= pagination
.block__content
= render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn
.block__header.block__header--light
= pagination

View file

@ -0,0 +1,26 @@
.walloftext
h1
= @user.name
| 's Donations
= render PhilomenaWeb.Admin.DonationView, "_table.html", donations: @donations, conn: @conn
h1 Add Donation
= form_for @changeset, Routes.admin_donation_path(@conn, :create), fn f ->
.field
=> label f, :user_id, "User ID:"
= number_input f, :user_id, class: "input input--short", value: @user.id
.field
=> label f, :email, "Email:"
= text_input f, :email, class: "input input--wide", value: @user.email
.field
=> label f, :amount, "Amount:"
= number_input f, :amount, class: "input input--wide", min: 0, step: 0.01
.field
=> label f, :note, "Note:"
= textarea f, :note, class: "input input--wide"
= submit "Create Donation", class: "button"

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Admin.Donation.UserView do
use PhilomenaWeb, :view
end

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Admin.DonationView do
use PhilomenaWeb, :view
end