/
event.ex
138 lines (106 loc) · 4.04 KB
/
event.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
defmodule ValueFlows.EconomicEvent do
use Needle.Pointable,
otp_app: :bonfire_valueflows,
source: "vf_event",
table_id: "2CTVA10BSERVEDF10WS0FVA1VE"
import Bonfire.Common.Repo.Utils, only: [change_public: 1, change_disabled: 1]
alias Ecto.Changeset
alias ValueFlows.Knowledge.Action
alias ValueFlows.Knowledge.ResourceSpecification
alias ValueFlows.EconomicEvent
alias ValueFlows.EconomicResource
alias ValueFlows.Process
alias ValueFlows.ValueCalculation
alias Bonfire.Quantify.Measure
@type t :: %__MODULE__{}
pointable_schema do
field(:note, :string)
# TODO: link to Agreement?
field(:agreed_in, :string)
field(:has_beginning, :utc_datetime_usec)
field(:has_end, :utc_datetime_usec)
field(:has_point_in_time, :utc_datetime_usec)
belongs_to(:action, Action, type: :string)
belongs_to(:input_of, Process)
belongs_to(:output_of, Process)
belongs_to(:provider, ValueFlows.Util.user_or_org_schema())
belongs_to(:receiver, ValueFlows.Util.user_or_org_schema())
belongs_to(:resource_inventoried_as, EconomicResource)
belongs_to(:to_resource_inventoried_as, EconomicResource)
field(:resource_classified_as, {:array, :string}, virtual: true)
belongs_to(:resource_conforms_to, ResourceSpecification)
belongs_to(:resource_quantity, Measure, on_replace: :nilify)
belongs_to(:effort_quantity, Measure, on_replace: :nilify)
belongs_to(:context, Needle.Pointer)
belongs_to(:at_location, Bonfire.Geolocate.Geolocation)
belongs_to(:triggered_by, EconomicEvent)
belongs_to(:calculated_using, ValueCalculation)
# TODO:
# track: [ProductionFlowItem!]
# trace: [ProductionFlowItem!]
# realizationOf: Agreement
# appreciationOf: [Appreciation!]
# appreciatedBy: [Appreciation!]
# fulfills: [Fulfillment!]
# satisfies: [Satisfaction!]
# field(:deletable, :boolean) # TODO - virtual field? how is it calculated?
belongs_to(:creator, ValueFlows.Util.user_schema())
field(:is_public, :boolean, virtual: true)
field(:published_at, :utc_datetime_usec)
field(:is_disabled, :boolean, virtual: true, default: false)
field(:disabled_at, :utc_datetime_usec)
field(:deleted_at, :utc_datetime_usec)
timestamps(inserted_at: false)
end
@required ~w(action_id provider_id receiver_id is_public)a
@cast @required ++
~w(note resource_classified_as agreed_in has_beginning has_end has_point_in_time is_disabled)a ++
~w(input_of_id output_of_id resource_conforms_to_id resource_inventoried_as_id to_resource_inventoried_as_id)a ++
~w(triggered_by_id at_location_id context_id calculated_using_id)a
def create_changeset(
%{} = creator,
attrs
) do
validate_changeset(attrs)
|> Changeset.change(creator_id: creator.id)
end
def validate_changeset(attrs \\ %{}) do
%__MODULE__{}
|> Changeset.cast(attrs, @cast)
|> ValueFlows.Util.change_measures(attrs, measure_fields())
|> validate_create_changeset()
end
def validate_create_changeset(cs) do
cs
|> Changeset.change(is_public: true)
|> Changeset.validate_required(@required)
|> common_changeset()
end
def update_changeset(%EconomicEvent{} = event, attrs) do
event
|> Changeset.cast(attrs, @cast)
|> ValueFlows.Util.change_measures(attrs, measure_fields())
|> common_changeset()
end
def measure_fields do
[:resource_quantity, :effort_quantity]
end
defp common_changeset(changeset) do
changeset
|> Changeset.change(is_public: true)
|> change_public()
|> change_disabled()
|> Changeset.foreign_key_constraint(
:resource_inventoried_as_id,
name: :vf_event_resource_inventoried_as_id_fkey
)
|> Changeset.foreign_key_constraint(
:to_resource_inventoried_as_id,
name: :vf_event_to_resource_inventoried_as_id_fkey
)
end
@behaviour Bonfire.Common.SchemaModule
def context_module, do: ValueFlows.EconomicEvent.EconomicEvents
def query_module, do: ValueFlows.EconomicEvent.Queries
def follow_filters, do: [:default]
end