@@ -10,6 +10,7 @@ defmodule Diffo.Provider.Assigner do
1010 """
1111
1212 alias Diffo.Provider.AssignableValue
13+ alias Diffo.Type.Value
1314
1415 @ doc """
1516 Assign a thing using the instance changeset assignment
@@ -53,7 +54,11 @@ defmodule Diffo.Provider.Assigner do
5354 defp relate_is_assigned ( result , things , thing , value , assignee_id )
5455 when is_struct ( result ) and is_atom ( things ) and is_atom ( thing ) and is_integer ( value ) and
5556 is_bitstring ( assignee_id ) do
56- case Diffo.Provider . create_characteristic ( % { name: thing , value: value , type: :relationship } ) do
57+ case Diffo.Provider . create_characteristic ( % {
58+ name: thing ,
59+ value: Value . primitive ( "integer" , value ) ,
60+ type: :relationship
61+ } ) do
5762 { :ok , characteristic } ->
5863 case Diffo.Provider . create_relationship ( % {
5964 type: :assignedTo ,
@@ -62,7 +67,6 @@ defmodule Diffo.Provider.Assigner do
6267 characteristics: [ characteristic . id ]
6368 } ) do
6469 { :ok , _relationship } ->
65- # we haven't refreshed the result there will be a new forward_relationship and an updated things characteristic
6670 case decrement_free ( result , things ) do
6771 :ok ->
6872 { :ok , result }
@@ -91,7 +95,7 @@ defmodule Diffo.Provider.Assigner do
9195 } ->
9296 type == :assignedTo and target_id == assignee_id and
9397 Enum . any? ( characteristics , fn % { name: name , value: v } ->
94- name == thing and v == value
98+ name == thing and Diffo.Unwrap . unwrap ( v ) == value
9599 end )
96100 end )
97101
@@ -105,16 +109,13 @@ defmodule Diffo.Provider.Assigner do
105109 characteristic =
106110 Enum . find ( relationship . characteristics , fn % { name: n } -> n == thing end )
107111
108- # unrelate the relationship characterisitic
109112 relationship =
110113 Diffo.Provider . unrelate_relationship_characteristics! ( relationship , % {
111114 characteristics: [ characteristic . id ]
112115 } )
113116
114- # delete the relationship characteristic
115117 Diffo.Provider . delete_characteristic ( characteristic . id )
116118
117- # delete the relationship
118119 case Diffo.Provider . delete_relationship ( relationship . id ) do
119120 :ok ->
120121 case increment_free ( result , things ) do
@@ -148,8 +149,8 @@ defmodule Diffo.Provider.Assigner do
148149 if characteristic do
149150 assignment =
150151 struct ( Diffo.Provider.Assignment , % {
151- id: characteristic . value ,
152- type : thing ,
152+ id: Diffo.Unwrap . unwrap ( characteristic . value ) ,
153+ assignable_type : thing ,
153154 assignee_id: target_id
154155 } )
155156
@@ -168,9 +169,10 @@ defmodule Diffo.Provider.Assigner do
168169 defp next ( instance , things , thing )
169170 when is_struct ( instance ) and is_atom ( things ) and is_atom ( thing ) do
170171 characteristic = Enum . find ( instance . characteristics , fn % { name: name } -> name == things end )
171- algorithm = Map . get ( characteristic . value , :algorithm )
172+ assignable_value = Diffo.Unwrap . unwrap ( characteristic . value )
173+ algorithm = Map . get ( assignable_value , :algorithm )
172174
173- case free = free ( instance , thing , characteristic . value ) do
175+ case free = free ( instance , thing , assignable_value ) do
174176 [ ] ->
175177 { :error , "all things are assigned" }
176178
@@ -191,7 +193,8 @@ defmodule Diffo.Provider.Assigner do
191193 defp assignable? ( instance , things , thing , value )
192194 when is_struct ( instance ) and is_atom ( things ) and is_atom ( thing ) and is_integer ( value ) do
193195 characteristic = Enum . find ( instance . characteristics , fn % { name: name } -> name == things end )
194- free = free ( instance , thing , characteristic . value )
196+ assignable_value = Diffo.Unwrap . unwrap ( characteristic . value )
197+ free = free ( instance , thing , assignable_value )
195198
196199 value in free
197200 end
@@ -200,10 +203,15 @@ defmodule Diffo.Provider.Assigner do
200203 characteristic =
201204 Enum . find ( instance . characteristics , fn % { name: name } -> name == things end )
202205
203- { _free , assignable_value } =
204- Map . get_and_update ( characteristic . value , :free , fn free -> { free - 1 , free - 1 } end )
206+ assignable_value = Diffo.Unwrap . unwrap ( characteristic . value )
205207
206- case Diffo.Provider . update_characteristic ( characteristic , % { value: assignable_value } ) do
208+ { _free , updated } =
209+ Map . get_and_update ( assignable_value , :free , fn free -> { free - 1 , free - 1 } end )
210+
211+ { :ok , new_struct } = Ash.Type . cast_input ( AssignableValue , Map . from_struct ( updated ) , AssignableValue . subtype_constraints ( ) )
212+ new_value = Value . dynamic ( AssignableValue , new_struct )
213+
214+ case Diffo.Provider . update_characteristic ( characteristic , % { value: new_value } ) do
207215 { :ok , _characteristic } ->
208216 :ok
209217
@@ -216,10 +224,15 @@ defmodule Diffo.Provider.Assigner do
216224 characteristic =
217225 Enum . find ( instance . characteristics , fn % { name: name } -> name == things end )
218226
219- { _free , assignable_value } =
220- Map . get_and_update ( characteristic . value , :free , fn free -> { free + 1 , free + 1 } end )
227+ assignable_value = Diffo.Unwrap . unwrap ( characteristic . value )
228+
229+ { _free , updated } =
230+ Map . get_and_update ( assignable_value , :free , fn free -> { free + 1 , free + 1 } end )
231+
232+ { :ok , new_struct } = Ash.Type . cast_input ( AssignableValue , Map . from_struct ( updated ) , AssignableValue . subtype_constraints ( ) )
233+ new_value = Value . dynamic ( AssignableValue , new_struct )
221234
222- case Diffo.Provider . update_characteristic ( characteristic , % { value: assignable_value } ) do
235+ case Diffo.Provider . update_characteristic ( characteristic , % { value: new_value } ) do
223236 { :ok , _characteristic } ->
224237 :ok
225238
0 commit comments