Skip to content

Commit

Permalink
Merge pull request #540 from eyra/fix-publish
Browse files Browse the repository at this point in the history
Fixed: Unable to publish assignment after disabling consent form #539
  • Loading branch information
mellelieuwes authored Jan 9, 2024
2 parents 1744b3a + 7e79a20 commit 0ff0ca5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
12 changes: 8 additions & 4 deletions core/lib/core/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ defmodule Core.Repo do
import Ecto.Query, warn: false
alias Ecto.Multi

require Logger

def multi_update(multi, name, changeset) do
multi
|> multi_update_guard(name, changeset.data)
|> Multi.update(name, changeset)
end

def multi_update_guard(multi, name, model) do
Multi.run(multi, "#{name}_exists?", fn repo, _ ->
if valid?(repo, model) do
Multi.run(multi, "#{name}_up_to_date?", fn repo, _ ->
if up_to_date?(repo, model) do
{:ok, true}
else
{:error, false}
message = "#{String.capitalize(name)} is not up to date, preventing database change"
Logger.warning(message)
{:error, message}
end
end)
end

defp valid?(repo, %schema{id: id, updated_at: updated_at}) do
defp up_to_date?(repo, %schema{id: id, updated_at: updated_at}) do
from(e in schema, where: e.id == ^id and e.updated_at == ^updated_at)
|> repo.exists?()
end
Expand Down
10 changes: 8 additions & 2 deletions core/systems/assignment/_public.ex
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,14 @@ end
defimpl Core.Persister, for: Systems.Assignment.Model do
def save(_assignment, changeset) do
case Frameworks.Utility.EctoHelper.update_and_dispatch(changeset, :assignment) do
{:ok, %{assignment: assignment}} -> {:ok, assignment}
_ -> {:error, changeset}
{:ok, %{assignment: assignment}} ->
{:ok, assignment}

{:error, new_changeset} ->
{:error, new_changeset}

_ ->
{:error, changeset}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/systems/assignment/content_page_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ defmodule Systems.Assignment.ContentPageBuilder do
end

defp set_status(%{assigns: %{model: assignment}} = socket, status) do
assignment = Assignment.Public.update!(assignment, %{status: status})
{:ok, assignment} = Assignment.Public.update(assignment, %{status: status})
socket |> Phoenix.Component.assign(model: assignment)
end

Expand Down
3 changes: 2 additions & 1 deletion core/systems/observatory/_public.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ defmodule Systems.Observatory.Public do
{
:noreply,
socket
|> Public.update_view_model(__MODULE__, model, @presenter)
|> assign(model: model)
|> update_view_model()
|> handle_view_model_updated()
|> put_updated_info_flash()
}
Expand Down

0 comments on commit 0ff0ca5

Please sign in to comment.