From 5dc532c794c965d64e257052bbb2341352ca48e2 Mon Sep 17 00:00:00 2001 From: Neylix Date: Mon, 2 Jan 2023 19:48:53 +0100 Subject: [PATCH] Refactor format and comment sort function --- lib/archethic/transaction_chain.ex | 4 ++++ lib/archethic_web/controllers/api/transaction_payload.ex | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/archethic/transaction_chain.ex b/lib/archethic/transaction_chain.ex index 2edfb5d22..022d61689 100644 --- a/lib/archethic/transaction_chain.ex +++ b/lib/archethic/transaction_chain.ex @@ -798,6 +798,10 @@ defmodule Archethic.TransactionChain do conflict_resolver = fn results -> results |> Enum.sort( + # Prioritize more? at true + # then length of transaction list + # then regarding order, the oldest or newest transaction timestamp + # of the first element of the list &with false <- &1.more? and !&2.more?, false <- length(&1.transactions) > length(&2.transactions) do if Enum.empty?(&1.transactions) do diff --git a/lib/archethic_web/controllers/api/transaction_payload.ex b/lib/archethic_web/controllers/api/transaction_payload.ex index ac82dde11..a939b7f14 100644 --- a/lib/archethic_web/controllers/api/transaction_payload.ex +++ b/lib/archethic_web/controllers/api/transaction_payload.ex @@ -49,7 +49,7 @@ defmodule ArchethicWeb.API.TransactionPayload do def to_map(%{changes: changes}, acc) do Enum.reduce(changes, acc, fn {key, value}, acc -> - value = if key == :authorizedKeys, do: format_authorized_keys(value), else: value + value = format_change(key, value) key = Macro.underscore(Atom.to_string(key)) @@ -69,7 +69,7 @@ defmodule ArchethicWeb.API.TransactionPayload do def to_map(value, _), do: value - defp format_authorized_keys(authorized_keys) do + defp format_change(:authorizedKeys, authorized_keys) do Enum.reduce(authorized_keys, %{}, fn %Ecto.Changeset{ changes: %{ publicKey: public_key, @@ -81,6 +81,8 @@ defmodule ArchethicWeb.API.TransactionPayload do end) end + defp format_change(_, value), do: value + defp validate_data(changeset = %Ecto.Changeset{}) do validate_change(changeset, :data, fn _, data_changeset -> case data_changeset.valid? do