Hello all,
So I have a Multi.new() function that works perfectly.
Then I added another |> Multi.run() pipe, which returns an {:ok, _} with the field I wanted updated correctly. Repo.transaction() will also return {:ok, _} and commit all changes from all Multi.run() lines except the one I added last.
I started to debug, and I realized there is two Multi.run() lines that work with the same struct, and only the first one in the pipe will be correctly commited to the database. I looked up on the documentation and didn’t find anything about it.
Therefore my question is, can I commit changes to the same struct twice (differente field each time), in the same Multi.new() but in differente Multi.run() lines?
Thanks in advance for any help. And just in case anyone wants to know I’ll post the function bellow, and the Multi.run() that I added, and is returning {:ok, _} from Repo.update but not being commited to the database is the last one before Repo.transaction().
defp complete_signature_and_document(signature) do
Multi.new()
|> Multi.run(:signature, fn _repo, _changes ->
update_signature_completed(signature)
end)
|> Multi.run(:document, fn _repo, %{signature: signature} ->
Documents.update_document_status(signature.document, "current_signature")
end)
|> Multi.run(:audit, fn _repo, %{signature: signature} ->
{:ok, signature} = get_signature_with_document(signature.id)
audit(nil, "signature.completed", {signature})
end)
|> Multi.run(:receipt_file, fn _repo, %{signature: signature} ->
signature = Repo.preload(signature, [:original_file, {:signees, [:individual, :company]}])
create_receipt_file(signature)
end)
|> Multi.run(:merge_file, fn _repo, %{signature: signature, receipt_file: receipt_file} ->
signature = Repo.preload(signature, [:signing_file])
create_merged_file(signature, receipt_file)
end)
|> Multi.run(:complete_signature, fn _repo,
%{
signature: signature,
receipt_file: receipt_file,
merge_file: merge_file
} ->
update_signature_complete_files(signature, receipt_file.id, merge_file.id)
end)
|> Multi.run(:notify_signees, fn _repo, %{signature: signature} ->
notify_signees_completed(signature.document)
end)
|> Multi.run(:notify_assignee, fn _repo, %{signature: signature} ->
notify_assignee_completed(signature.document)
end)
|> Multi.run(:completed_at, fn _repo, %{signature: signature} ->
signed_date =
Documents.get_current_document_version!(signature.document.id)
|> Map.get(:id)
|> AuditLogs.get_by_document_version_and_signature_completed()
|> Map.get(:inserted_at)
|> DateTime.truncate(:second)
DocumentModel.update_completed_at(signature.document, signed_date)
end)
|> Repo.transaction(timeout: 45_000)
|> case do
{:error, :signature, _, _} -> {:error, :signature_not_completed}
{:error, :document, _, _} -> {:error, :document_not_completed}
{:error, :audit, _, _} -> {:error, :signature_event_not_inserted}
{:error, :notify_signees, _, _} -> {:error, :email_not_sent}
{:error, :completed_at, _, _} -> {:error, :completed_at_field_not_saved}
{:ok, result} -> {:ok, result}
end
end