Skip to content

Commit

Permalink
feat: added action for updating state to Delivery resource
Browse files Browse the repository at this point in the history
TODO: add missing state transitions
  • Loading branch information
Rekkice committed Jan 16, 2025
1 parent 27e5fd0 commit eb3421f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
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
9 changes: 7 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,13 @@ 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

0 comments on commit eb3421f

Please sign in to comment.