Skip to content

Commit

Permalink
TO BE REVERTED: Using default on schema does not allow creating a wor…
Browse files Browse the repository at this point in the history
…k order
  • Loading branch information
jyeshe committed Mar 4, 2025
1 parent 9906c96 commit a6debde
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
33 changes: 10 additions & 23 deletions lib/lightning/workorders/manual.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ defmodule Lightning.WorkOrders.Manual do
embeds_one :job, Lightning.Workflows.Job
field :is_persisted, :boolean
field :dataclip_id, Ecto.UUID
field :body, :string
field :body, :string, default: "{}"
end

def new(params, attrs \\ []) do
params = Map.update(params, "body", "{}", fn body ->
if is_nil(body) and (params["dataclip_id"] || "") == "" do
"{}"
else
body
end
end)
# params = Map.update(params, "body", "{}", fn body ->
# if is_nil(body) and (params["dataclip_id"] || "") == "" do
# "{}"
# else
# body
# end
# end)

struct(__MODULE__, attrs)
|> cast(params, [:dataclip_id, :body])
|> validate_required([:project, :job, :created_by, :workflow])
|> remove_body_if_dataclip_present()
|> validate_body_or_dataclip()
|> validate_json(:body)
|> validate_change(:workflow, fn _field, workflow ->
Expand All @@ -54,14 +53,6 @@ defmodule Lightning.WorkOrders.Manual do
end)
end

defp remove_body_if_dataclip_present(changeset) do
if is_nil(get_change(changeset, :dataclip_id)) do
changeset
else
Ecto.Changeset.delete_change(changeset, :body)
end
end

defp validate_json(changeset, field) do
case get_change(changeset, field) do
nil ->
Expand All @@ -77,14 +68,10 @@ defmodule Lightning.WorkOrders.Manual do
end

defp validate_body_or_dataclip(changeset) do
changeset
|> Validators.validate_one_required(
Validators.validate_one_required(
changeset,
[:dataclip_id, :body],
"Either a dataclip or a custom body must be present."
)
|> Validators.validate_exclusive(
[:dataclip_id, :body],
"Dataclip and custom body are mutually exclusive."
)
end
end
6 changes: 5 additions & 1 deletion test/lightning/workorders/manual_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ defmodule Lightning.WorkOrders.ManualTest do
end

test "removes body if dataclip_id is present" do
params = %{"dataclip_id" => Ecto.UUID.generate(), "body" => ~s({"foo": "bar"})}
params = %{
"dataclip_id" => Ecto.UUID.generate(),
"body" => ~s({"foo": "bar"})
}

changeset = Manual.new(params)

assert get_change(changeset, :body) == nil
Expand Down

0 comments on commit a6debde

Please sign in to comment.