fix: flatten nested case blocks with with chains (CSM-020)

Replace deeply nested case expressions with flat with chains in
import_definitions, publishing, and templates modules. Also replaced
Repo.update!() with Repo.update() in the publishing update_job handler.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 12:36:42 +02:00
parent b6f9cf58e1
commit 7c00279b9d
5 changed files with 171 additions and 100 deletions

View File

@@ -55,16 +55,12 @@ defmodule BDS.ImportDefinitions do
end
def delete_definition(definition_id) when is_binary(definition_id) do
case Repo.get(ImportDefinition, definition_id) do
nil ->
{:error, :not_found}
%ImportDefinition{} = definition ->
Repo.delete(definition)
|> case do
{:ok, _deleted} -> {:ok, :deleted}
error -> error
end
with %ImportDefinition{} = definition <- Repo.get(ImportDefinition, definition_id),
{:ok, _deleted} <- Repo.delete(definition) do
{:ok, :deleted}
else
nil -> {:error, :not_found}
{:error, _} = error -> error
end
end

View File

@@ -47,18 +47,12 @@ defmodule BDS.Publishing do
end
def handle_call({:update_job, job_id, attrs}, _from, state) do
reply =
case Repo.get(PublishJob, job_id) do
nil ->
:ok
with %PublishJob{} = job <- Repo.get(PublishJob, job_id) do
attrs = Map.put(attrs, :updated_at, Persistence.now_ms())
job |> PublishJob.changeset(attrs) |> Repo.update()
end
job ->
attrs = Map.put(attrs, :updated_at, Persistence.now_ms())
job |> PublishJob.changeset(attrs) |> Repo.update!()
:ok
end
{:reply, reply, state}
{:reply, :ok, state}
end
def handle_call({:should_upload_scp_file, upload_key, local_mtime}, _from, state) do

View File

@@ -87,82 +87,77 @@ defmodule BDS.Templates do
@spec update_template(String.t(), attrs()) :: template_result() | {:error, :not_found}
def update_template(template_id, attrs) do
case Repo.get(Template, template_id) do
nil ->
{:error, :not_found}
with %Template{} = template <- Repo.get(Template, template_id) do
do_update_template(template, attrs)
else
nil -> {:error, :not_found}
end
end
template ->
next_slug =
if has_attr?(attrs, :slug) do
unique_slug(
template.project_id,
Slug.slugify(attr(attrs, :slug)),
"template",
template.id
)
else
template.slug
end
defp do_update_template(template, attrs) do
next_slug =
if has_attr?(attrs, :slug) do
unique_slug(
template.project_id,
Slug.slugify(attr(attrs, :slug)),
"template",
template.id
)
else
template.slug
end
content_changed? =
has_attr?(attrs, :content) and
attr(attrs, :content) != effective_template_content(template)
content_changed? =
has_attr?(attrs, :content) and
attr(attrs, :content) != effective_template_content(template)
slug_changed? = next_slug != template.slug
now = Persistence.now_ms()
slug_changed? = next_slug != template.slug
now = Persistence.now_ms()
next_status =
if(template.status == :published and content_changed?,
do: :draft,
else: template.status
)
next_status =
if(template.status == :published and content_changed?,
do: :draft,
else: template.status
)
next_file_path = next_template_file_path(template, next_slug)
next_file_path = next_template_file_path(template, next_slug)
updates =
%{}
|> maybe_put(:title, attr(attrs, :title))
|> maybe_put(:kind, attr(attrs, :kind))
|> maybe_put(:enabled, attr(attrs, :enabled))
|> maybe_put(:content, attr(attrs, :content))
|> Map.put(:file_path, next_file_path)
|> Map.put(:slug, next_slug)
|> Map.put(:version, template.version + 1)
|> Map.put(:updated_at, now)
|> Map.put(:status, next_status)
updates =
%{}
|> maybe_put(:title, attr(attrs, :title))
|> maybe_put(:kind, attr(attrs, :kind))
|> maybe_put(:enabled, attr(attrs, :enabled))
|> maybe_put(:content, attr(attrs, :content))
|> Map.put(:file_path, next_file_path)
|> Map.put(:slug, next_slug)
|> Map.put(:version, template.version + 1)
|> Map.put(:updated_at, now)
|> Map.put(:status, next_status)
transaction_result =
Repo.transaction(fn ->
updated_template =
template
|> Template.changeset(updates)
|> Repo.update!()
with {:ok, {updated_template, affected_posts}} <-
Repo.transaction(fn ->
updated_template =
template
|> Template.changeset(updates)
|> Repo.update!()
affected_posts =
if slug_changed? do
cascade_template_slug_change(template, updated_template, now)
else
[]
end
affected_posts =
if slug_changed? do
cascade_template_slug_change(template, updated_template, now)
else
[]
end
{updated_template, affected_posts}
end)
case transaction_result do
{:ok, {updated_template, affected_posts}} ->
case sync_template_update_side_effects(
template,
updated_template,
affected_posts,
slug_changed?
) do
:ok -> {:ok, updated_template}
{:error, reason} -> {:error, reason}
end
{:error, reason} ->
{:error, reason}
end
{updated_template, affected_posts}
end),
:ok <-
sync_template_update_side_effects(
template,
updated_template,
affected_posts,
slug_changed?
) do
{:ok, updated_template}
end
end