cannery/lib/cannery/ammo/pack.ex

144 lines
4.1 KiB
Elixir
Raw Normal View History

2023-03-29 22:54:55 -04:00
defmodule Cannery.Ammo.Pack do
2022-01-22 21:40:29 -05:00
@moduledoc """
A group of a certain ammunition type.
Can be placed in a container, and contains auxiliary information such as the
amount paid for that ammunition, or what condition it is in
"""
2021-09-02 23:31:14 -04:00
use Ecto.Schema
2022-07-04 20:06:41 -04:00
import CanneryWeb.Gettext
2021-09-02 23:31:14 -04:00
import Ecto.Changeset
2023-03-30 21:53:52 -04:00
alias Cannery.Ammo.Type
alias Cannery.{Accounts.User, Containers, Containers.Container}
2022-01-31 20:08:01 -05:00
alias Ecto.{Changeset, UUID}
2021-09-02 23:31:14 -04:00
2022-11-09 23:33:50 -05:00
@derive {Jason.Encoder,
only: [
:id,
:count,
:notes,
:price_paid,
2023-03-30 23:35:01 -04:00
:lot_number,
2022-11-09 23:33:50 -05:00
:staged,
2023-03-30 21:53:52 -04:00
:type_id,
2022-11-09 23:33:50 -05:00
:container_id
]}
2021-09-02 23:31:14 -04:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
2023-03-29 22:54:55 -04:00
schema "packs" do
2021-09-02 23:31:14 -04:00
field :count, :integer
field :notes, :string
field :price_paid, :float
2022-02-15 17:33:45 -05:00
field :staged, :boolean, default: false
2023-03-30 23:35:01 -04:00
field :lot_number, :string
field :purchased_on, :date
2023-03-30 21:53:52 -04:00
belongs_to :type, Type
field :container_id, :binary_id
field :user_id, :binary_id
2022-02-15 21:56:01 -05:00
2021-09-02 23:31:14 -04:00
timestamps()
end
@type t :: %__MODULE__{
2022-01-31 20:08:01 -05:00
id: id(),
count: integer,
2022-02-09 23:45:10 -05:00
notes: String.t() | nil,
price_paid: float() | nil,
2022-02-15 17:33:45 -05:00
staged: boolean(),
2023-03-30 23:35:01 -04:00
lot_number: String.t() | nil,
purchased_on: Date.t(),
2023-03-30 21:53:52 -04:00
type: Type.t() | nil,
type_id: Type.id(),
2022-01-31 20:08:01 -05:00
container_id: Container.id(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
2023-03-29 22:54:55 -04:00
@type new_pack :: %__MODULE__{}
2022-01-31 20:08:01 -05:00
@type id :: UUID.t()
2023-03-29 22:54:55 -04:00
@type changeset :: Changeset.t(t() | new_pack())
2022-01-31 20:08:01 -05:00
2021-09-02 23:31:14 -04:00
@doc false
2022-07-04 20:06:41 -04:00
@spec create_changeset(
2023-03-29 22:54:55 -04:00
new_pack(),
2023-03-30 21:53:52 -04:00
Type.t() | nil,
2022-07-04 20:06:41 -04:00
Container.t() | nil,
User.t(),
attrs :: map()
2022-11-23 20:46:41 -05:00
) :: changeset()
2022-07-04 20:06:41 -04:00
def create_changeset(
2023-03-29 22:54:55 -04:00
pack,
2023-03-30 21:53:52 -04:00
%Type{id: type_id},
2022-07-04 20:06:41 -04:00
%Container{id: container_id, user_id: user_id},
%User{id: user_id},
attrs
)
2023-03-30 21:53:52 -04:00
when is_binary(type_id) and is_binary(container_id) and is_binary(user_id) do
2023-03-29 22:54:55 -04:00
pack
2023-03-30 21:53:52 -04:00
|> change(type_id: type_id)
2022-07-04 20:06:41 -04:00
|> change(user_id: user_id)
|> change(container_id: container_id)
2023-03-30 23:35:01 -04:00
|> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on, :lot_number])
2022-02-14 20:51:09 -05:00
|> validate_number(:count, greater_than: 0)
2023-03-30 23:35:01 -04:00
|> validate_number(:price_paid, greater_than_or_equal_to: 0)
|> validate_length(:lot_number, max: 255)
2023-03-30 21:53:52 -04:00
|> validate_required([:count, :staged, :purchased_on, :type_id, :container_id, :user_id])
2021-09-02 23:31:14 -04:00
end
2022-07-04 20:06:41 -04:00
@doc """
2023-03-30 21:53:52 -04:00
Invalid changeset, used to prompt user to select type and container
2022-07-04 20:06:41 -04:00
"""
2023-03-30 21:53:52 -04:00
def create_changeset(pack, _invalid_type, _invalid_container, _invalid_user, attrs) do
2023-03-29 22:54:55 -04:00
pack
2023-03-30 21:53:52 -04:00
|> cast(attrs, [:type_id, :container_id])
|> validate_required([:type_id, :container_id])
2023-03-30 23:35:01 -04:00
|> validate_number(:count, greater_than: 0)
|> validate_number(:price_paid, greater_than_or_equal_to: 0)
|> validate_length(:lot_number, max: 255)
2023-03-30 21:53:52 -04:00
|> add_error(:invalid, dgettext("errors", "Please select a type and container"))
2022-07-04 20:06:41 -04:00
end
@doc false
2023-03-29 22:54:55 -04:00
@spec update_changeset(t() | new_pack(), attrs :: map(), User.t()) :: changeset()
def update_changeset(pack, attrs, user) do
pack
2023-03-30 23:35:01 -04:00
|> cast(attrs, [
:count,
:price_paid,
:notes,
:staged,
:purchased_on,
:lot_number,
:container_id
])
|> validate_number(:count, greater_than_or_equal_to: 0)
2023-03-30 23:35:01 -04:00
|> validate_number(:price_paid, greater_than_or_equal_to: 0)
2022-11-10 21:45:50 -05:00
|> validate_container_id(user)
2023-03-30 23:35:01 -04:00
|> validate_length(:lot_number, max: 255)
|> validate_required([:count, :staged, :purchased_on, :container_id])
2022-11-10 21:45:50 -05:00
end
defp validate_container_id(changeset, user) do
container_id = changeset |> Changeset.get_field(:container_id)
if container_id do
Containers.get_container!(container_id, user)
end
changeset
2022-02-14 20:51:09 -05:00
end
@doc """
2023-03-29 23:49:45 -04:00
This range changeset is used when "using up" packs, and allows for
2022-02-14 20:51:09 -05:00
updating the count to 0
"""
2023-03-29 22:54:55 -04:00
@spec range_changeset(t() | new_pack(), attrs :: map()) :: changeset()
def range_changeset(pack, attrs) do
pack
2022-02-15 17:33:45 -05:00
|> cast(attrs, [:count, :staged])
2022-07-01 19:53:44 -04:00
|> validate_required([:count, :staged])
end
2021-09-02 23:31:14 -04:00
end