cannery/lib/cannery/activity_log/shot_record.ex

127 lines
3.6 KiB
Elixir
Raw Normal View History

2023-03-30 20:43:30 -04:00
defmodule Cannery.ActivityLog.ShotRecord do
2022-02-15 17:33:45 -05:00
@moduledoc """
2023-03-30 20:43:30 -04:00
A shot record records a group of ammo shot during a range trip
2022-02-15 17:33:45 -05:00
"""
use Ecto.Schema
2022-07-01 21:39:08 -04:00
import CanneryWeb.Gettext
2022-02-15 17:33:45 -05:00
import Ecto.Changeset
2023-03-29 22:54:55 -04:00
alias Cannery.{Accounts.User, Ammo, Ammo.Pack}
2022-02-15 17:33:45 -05:00
alias Ecto.{Changeset, UUID}
2022-11-09 23:33:50 -05:00
@derive {Jason.Encoder,
only: [
:id,
:count,
:date,
:notes,
2023-03-29 22:54:55 -04:00
:pack_id
2022-11-09 23:33:50 -05:00
]}
2022-02-15 17:33:45 -05:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
2023-03-30 20:43:30 -04:00
schema "shot_records" do
2022-02-15 17:33:45 -05:00
field :count, :integer
field :date, :date
field :notes, :string
field :user_id, :binary_id
2023-03-29 22:54:55 -04:00
field :pack_id, :binary_id
2022-02-15 17:33:45 -05:00
timestamps()
end
@type t :: %__MODULE__{
2022-02-15 17:33:45 -05:00
id: id(),
count: integer,
notes: String.t() | nil,
date: Date.t() | nil,
2023-03-29 22:54:55 -04:00
pack_id: Pack.id(),
2022-02-15 17:33:45 -05:00
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
2023-03-30 20:43:30 -04:00
@type new_shot_record :: %__MODULE__{}
2022-02-15 17:33:45 -05:00
@type id :: UUID.t()
2023-03-30 20:43:30 -04:00
@type changeset :: Changeset.t(t() | new_shot_record())
2022-02-15 17:33:45 -05:00
@doc false
2022-07-04 20:06:31 -04:00
@spec create_changeset(
2023-03-30 20:43:30 -04:00
new_shot_record(),
2022-07-04 20:06:31 -04:00
User.t() | any(),
2023-03-29 22:54:55 -04:00
Pack.t() | any(),
2022-07-04 20:06:31 -04:00
attrs :: map()
2022-11-23 20:46:41 -05:00
) :: changeset()
2022-07-01 21:39:08 -04:00
def create_changeset(
2023-03-30 20:43:30 -04:00
shot_record,
2022-07-01 21:39:08 -04:00
%User{id: user_id},
2023-03-29 22:54:55 -04:00
%Pack{id: pack_id, user_id: user_id} = pack,
2022-07-01 21:39:08 -04:00
attrs
) do
2023-03-30 20:43:30 -04:00
shot_record
2022-07-01 21:39:08 -04:00
|> change(user_id: user_id)
2023-03-29 22:54:55 -04:00
|> change(pack_id: pack_id)
2022-07-01 21:39:08 -04:00
|> cast(attrs, [:count, :notes, :date])
2023-03-19 23:46:42 -04:00
|> validate_length(:notes, max: 255)
2023-03-30 20:43:30 -04:00
|> validate_create_shot_record_count(pack)
2023-03-29 22:54:55 -04:00
|> validate_required([:date, :pack_id, :user_id])
2022-02-15 17:33:45 -05:00
end
2023-03-30 20:43:30 -04:00
def create_changeset(shot_record, _invalid_user, _invalid_pack, attrs) do
shot_record
2022-07-04 20:06:31 -04:00
|> cast(attrs, [:count, :notes, :date])
2023-03-19 23:46:42 -04:00
|> validate_length(:notes, max: 255)
2023-03-29 22:54:55 -04:00
|> validate_required([:pack_id, :user_id])
2022-11-19 15:04:56 -05:00
|> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo pack"))
2022-07-04 20:06:31 -04:00
end
2023-03-30 20:43:30 -04:00
defp validate_create_shot_record_count(changeset, %Pack{count: pack_count}) do
case changeset |> Changeset.get_field(:count) do
nil ->
changeset |> Changeset.add_error(:ammo_left, dgettext("errors", "can't be blank"))
2023-03-15 00:45:08 -04:00
2023-03-29 22:54:55 -04:00
count when count > pack_count ->
changeset
|> Changeset.add_error(:ammo_left, dgettext("errors", "Ammo left must be at least 0"))
count when count <= 0 ->
error =
2023-03-29 22:54:55 -04:00
dgettext("errors", "Ammo left can be at most %{count} rounds", count: pack_count - 1)
changeset |> Changeset.add_error(:ammo_left, error)
_valid_count ->
changeset
2022-07-01 21:39:08 -04:00
end
end
2022-02-15 17:33:45 -05:00
@doc false
2023-03-30 20:43:30 -04:00
@spec update_changeset(t() | new_shot_record(), User.t(), attrs :: map()) :: changeset()
def update_changeset(%__MODULE__{} = shot_record, user, attrs) do
shot_record
2022-02-15 17:33:45 -05:00
|> cast(attrs, [:count, :notes, :date])
2023-03-19 23:46:42 -04:00
|> validate_length(:notes, max: 255)
2022-02-15 17:33:45 -05:00
|> validate_number(:count, greater_than: 0)
2022-11-09 21:04:57 -05:00
|> validate_required([:count, :date])
2023-03-30 20:43:30 -04:00
|> validate_update_shot_record_count(shot_record, user)
2022-07-01 21:39:08 -04:00
end
2023-03-30 20:43:30 -04:00
defp validate_update_shot_record_count(
2022-07-01 21:39:08 -04:00
changeset,
2023-03-29 22:54:55 -04:00
%__MODULE__{pack_id: pack_id, count: count},
user
) do
2023-03-29 22:54:55 -04:00
%{count: pack_count} = Ammo.get_pack!(pack_id, user)
2022-07-01 21:39:08 -04:00
2023-03-30 20:43:30 -04:00
new_shot_record_count = changeset |> Changeset.get_field(:count)
shot_diff_to_add = new_shot_record_count - count
2022-07-01 21:39:08 -04:00
2023-03-29 22:54:55 -04:00
if shot_diff_to_add > pack_count do
error = dgettext("errors", "Count can be at most %{count} shots", count: pack_count + count)
2022-07-01 21:39:08 -04:00
2023-03-18 01:05:09 -04:00
changeset |> Changeset.add_error(:count, error)
else
changeset
2022-07-01 21:39:08 -04:00
end
2022-02-15 17:33:45 -05:00
end
end