-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added action for updating state to Delivery resource
TODO: add missing state transitions
- Loading branch information
Showing
4 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |