Current section
Files
Jump to
Current section
Files
lib/diffo/provider/components/instance/extension/party.ex
# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors>
#
# SPDX-License-Identifier: MIT
defmodule Diffo.Provider.Instance.Party do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference
Party for Instance Extension
"""
alias Diffo.Provider
@doc """
Struct for a Party
"""
defstruct [:id, :role]
@doc """
Relates the parties in the changeset with the Extended Instance by creating party_ref
"""
def relate_instance(result, changeset)
when is_struct(result) and is_struct(changeset, Ash.Changeset) do
parties = Ash.Changeset.get_argument(changeset, :parties)
case parties do
nil ->
{:ok, result}
[] ->
{:ok, result}
_ ->
party_refs =
Enum.reduce_while(parties, [], fn %{id: id, role: role}, acc ->
case Provider.create_party_ref(%{instance_id: result.id, party_id: id, role: role}) do
{:ok, party_ref} ->
{:cont, [party_ref | acc]}
{:error, _error} ->
{:halt, []}
end
end)
case party_refs do
[] ->
{:error, "couldn't relate parties"}
_ ->
# sorted = Ash.Sort.runtime_sort(party_refs, [role: :asc, inserted_at: :desc])
{:ok, result |> Map.put(:parties, party_refs)}
end
end
end
end