|
4 | 4 |
|
5 | 5 | defmodule Diffo.Provider.Assigner do |
6 | 6 | @moduledoc """ |
7 | | - Helper to perform Assignment using `Diffo.Provider.AssignedToRelationship`. |
| 7 | + Helper to perform Assignment using `Diffo.Provider.DefinedSimpleRelationship`. |
8 | 8 |
|
9 | | - Assignment state is stored on `AssignedToRelationship` nodes (pool, thing, assigned), |
10 | | - distinct from regular TMF `Diffo.Provider.Relationship` nodes. |
| 9 | + Each assignment is stored as a `DefinedSimpleRelationship` with `type: :assignedTo` |
| 10 | + and a single `NameValuePrimitive` characteristic carrying the thing name and assigned value. |
11 | 11 | """ |
12 | 12 | alias Diffo.Provider.AssignableCharacteristic |
13 | | - alias Diffo.Provider.AssignedToRelationship |
| 13 | + alias Diffo.Provider.DefinedSimpleRelationship |
| 14 | + alias Diffo.Type.NameValuePrimitive |
| 15 | + alias Diffo.Type.Primitive |
14 | 16 |
|
15 | 17 | @doc """ |
16 | 18 | Assign a thing using the pool declared via `pools do` on the instance module. |
@@ -63,13 +65,15 @@ defmodule Diffo.Provider.Assigner do |
63 | 65 | end |
64 | 66 | end |
65 | 67 |
|
66 | | - defp relate_is_assigned(result, pool, thing, value, assignee_id) |
67 | | - when is_struct(result) and is_atom(pool) and is_atom(thing) and is_integer(value) and |
| 68 | + defp relate_is_assigned(result, _pool, thing, value, assignee_id) |
| 69 | + when is_struct(result) and is_atom(thing) and is_integer(value) and |
68 | 70 | is_bitstring(assignee_id) do |
69 | | - case Diffo.Provider.create_assigned_to_relationship(%{ |
70 | | - pool: pool, |
71 | | - thing: thing, |
72 | | - assigned: value, |
| 71 | + case Diffo.Provider.create_defined_simple_relationship(%{ |
| 72 | + type: :assignedTo, |
| 73 | + characteristic: %NameValuePrimitive{ |
| 74 | + name: thing, |
| 75 | + value: Primitive.wrap("integer", value) |
| 76 | + }, |
73 | 77 | source_id: result.id, |
74 | 78 | target_id: assignee_id |
75 | 79 | }) do |
@@ -102,17 +106,22 @@ defmodule Diffo.Provider.Assigner do |
102 | 106 | end |
103 | 107 | end |
104 | 108 |
|
105 | | - defp find_assignment(source_id, target_id, pool, thing, value) do |
106 | | - AssignedToRelationship |
107 | | - |> Ash.Query.new() |
108 | | - |> Ash.Query.filter_input( |
109 | | - source_id: source_id, |
110 | | - target_id: target_id, |
111 | | - pool: pool, |
112 | | - thing: thing, |
113 | | - assigned: value |
114 | | - ) |
115 | | - |> Ash.read_one(domain: Diffo.Provider) |
| 109 | + defp find_assignment(source_id, target_id, _pool, thing, value) do |
| 110 | + case DefinedSimpleRelationship |
| 111 | + |> Ash.Query.new() |
| 112 | + |> Ash.Query.filter_input(source_id: source_id, target_id: target_id, type: :assignedTo) |
| 113 | + |> Ash.read(domain: Diffo.Provider) do |
| 114 | + {:ok, rels} -> |
| 115 | + {:ok, |
| 116 | + Enum.find(rels, fn rel -> |
| 117 | + rel.characteristic && |
| 118 | + rel.characteristic.name == thing && |
| 119 | + Diffo.Unwrap.unwrap(rel.characteristic.value) == value |
| 120 | + end)} |
| 121 | + |
| 122 | + {:error, error} -> |
| 123 | + {:error, error} |
| 124 | + end |
116 | 125 | end |
117 | 126 |
|
118 | 127 | defp next(instance, pool, thing) |
|
0 commit comments