Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added action for updating state to Delivery resource #10

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tololo/config/ci.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,3 @@ config :kafka_ex,
# keyfile: File.cwd!() <> "/ssl/key.pem"
# ],
kafka_version: "0.10.1"


3 changes: 1 addition & 2 deletions tololo/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ config :phoenix_live_view,

config :tololo, :kafka_driver, driver: Tololo.Kafka.Noop
config :tololo, Tololo.Prometheus, disabled: true

config :kafka_ex,
brokers: [
{"localhost", 9092},
Expand Down Expand Up @@ -90,5 +91,3 @@ config :kafka_ex,
# keyfile: File.cwd!() <> "/ssl/key.pem"
# ],
kafka_version: "0.10.1"


13 changes: 12 additions & 1 deletion tololo/lib/tololo/deliveries/delivery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ defmodule Tololo.Deliveries.Delivery do
repo Tololo.Repo
end

code_interface do
define :update_state, args: [:state], action: :update_state
end

actions do
defaults [:read]
defaults [:read, :create, :update, :destroy]

update :update_state do
accept [:state]
require_atomic? false

change Tololo.Deliveries.UpdateHistory
end
end

attributes do
Expand Down
11 changes: 9 additions & 2 deletions tololo/lib/tololo/deliveries/delivery_state_changes.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Tololo.Deliveries.DeliveryStateChanges do
@moduledoc """
Stores the state changes of a delivery.
Resource that stores the state changes of a delivery.
"""
use Ash.Resource,
otp_app: :tololo,
Expand All @@ -17,8 +17,15 @@ defmodule Tololo.Deliveries.DeliveryStateChanges do
repo Tololo.Repo
end

code_interface do
define :add_to_state_history,
args: [:delivery_id, :old_state, :new_state, :comment],
action: :create
end

actions do
defaults [:read]
defaults [:read, :create, :destroy]
default_accept [:delivery_id, :old_state, :new_state, :comment]
end

attributes do
Expand Down
23 changes: 23 additions & 0 deletions tololo/lib/tololo/deliveries/transitions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Tololo.Deliveries.Transitions do
@moduledoc """
Functions related to delivery state transitions and comments.
"""

@state_transitions %{
{nil, "In_Preparation"} => "La orden está siendo preparada.",
{"In_Preparation", "In_Delivery"} => "La orden está en tránsito."
# TODO add all possible states
}

@doc """
Generates a comment for a state transition, based on the old and new state.
"""
def generate_comment(old_state, new_state),
do: Map.get(@state_transitions, {old_state, new_state})

@doc """
Checks if a state transition is valid.
"""
def valid?(old_state, new_state),
do: Map.has_key?(@state_transitions, {old_state, new_state})
end
27 changes: 27 additions & 0 deletions tololo/lib/tololo/deliveries/update_history.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Tololo.Deliveries.UpdateHistory do
@moduledoc """
Checks validity of state transition and also adds it to the state history.
"""
alias Tololo.Deliveries.Transitions
alias Tololo.Deliveries.DeliveryStateChanges

use Ash.Resource.Change

@impl true
def change(changeset, _opts, _context) do
%{id: id, state: old_state} = changeset.data
{:ok, new_state} = Ash.Changeset.fetch_change(changeset, :state)

case Transitions.valid?(old_state, new_state) do
true ->
comment = Transitions.generate_comment(old_state, new_state)
DeliveryStateChanges.add_to_state_history!(id, old_state, new_state, comment)

changeset

false ->
changeset
|> Ash.Changeset.add_error(field: :state, message: "invalid state transition")
end
end
end
4 changes: 4 additions & 0 deletions tololo/test/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Tololo.DataCase do
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate

using do
quote do
alias Tololo.Repo
Expand All @@ -21,17 +22,20 @@ defmodule Tololo.DataCase do
import Tololo.DataCase
end
end

setup tags do
Tololo.DataCase.setup_sandbox(tags)
:ok
end

@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Tololo.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
Expand Down
2 changes: 2 additions & 0 deletions tololo/test/tololo_web/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule TololoWeb.ConnCase do
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate

using do
quote do
# The default endpoint for testing
Expand All @@ -24,6 +25,7 @@ defmodule TololoWeb.ConnCase do
import TololoWeb.ConnCase
end
end

setup tags do
Tololo.DataCase.setup_sandbox(tags)
{:ok, conn: Phoenix.ConnTest.build_conn()}
Expand Down
Loading