Commit b80770c5 authored by Antonis Kalou's avatar Antonis Kalou

Add provider/receiver for claim

parent e8f65fdb
......@@ -10,6 +10,8 @@ defmodule ValueFlows.Claim do
alias Ecto.Changeset
alias CommonsPub.Users.User
alias Measurement.Measure
alias ValueFlows.Knowledge.Action
alias ValueFlows.Knowledge.ResourceSpecification
alias ValueFlows.Observation.EconomicEvent
......@@ -52,12 +54,19 @@ defmodule ValueFlows.Claim do
@cast @required ++
~w(note finished agreed_in created due resource_classified_as is_disabled)a
def create_changeset(%User{} = creator, attrs) do
def create_changeset(%User{} = creator, %{id: _} = provider, %{id: _} = receiver, attrs) do
%__MODULE__{}
|> Changeset.cast(attrs, @cast)
|> Changeset.validate_required(@required)
|> Changeset.change(
creator_id: creator.id,
is_public: true
provider_id: provider.id,
receiver_id: receiver.id,
is_public: true,
# preload
provider: provider,
receiver: receiver,
creator: creator.id
)
|> common_changeset()
end
......
......@@ -6,14 +6,17 @@ defmodule ValueFlows.Claim.Claims do
alias ValueFlows.Claim
alias ValueFlows.Claim.Queries
alias CommonsPub.Meta.Pointers
def one(filters), do: Repo.single(Queries.query(Claim, filters))
def many(filters \\ []), do: {:ok, Repo.all(Queries.query(Claim, filters))}
def create(%User{} = creator, %{} = attrs) do
def create(%User{} = creator, %{id: _} = provider, %{id: _} = receiver, %{} = attrs) do
Repo.transact_with(fn ->
with {:ok, claim} <- Repo.insert(Claim.create_changeset(creator, attrs)) do
{:ok, %{claim | creator: creator}}
with {:ok, provider_ptr} <- Pointers.one(id: provider.id),
{:ok, receiver_ptr} <- Pointers.one(id: receiver.id) do
Repo.insert(Claim.create_changeset(creator, provider_ptr, receiver_ptr, attrs))
end
end)
end
......
......@@ -24,6 +24,12 @@ defmodule ValueFlows.Simulate do
def claim(base \\ %{}) do
base
|> Map.put_new_lazy(:note, &summary/0)
|> Map.put_new_lazy(:agreed_in, &url/0)
|> Map.put_new_lazy(:finished, &bool/0)
|> Map.put_new_lazy(:created, &past_datetime/0)
|> Map.put_new_lazy(:due, &future_datetime/0)
|> Map.put_new_lazy(:action, &action_id/0)
|> Map.put_new_lazy(:resource_classified_as, fn -> some(1..5, &url/0) end)
end
def agent_type(), do: Faker.Util.pick([:person, :organization])
......
......@@ -25,6 +25,7 @@ defmodule ValueFlows.Test.Faking do
}
alias ValueFlows.{
Claim,
Proposal
# Proposals
}
......@@ -60,6 +61,21 @@ defmodule ValueFlows.Test.Faking do
)
end
def assert_claim(%Claim{} = claim) do
assert_claim(Map.from_struct(claim))
end
def assert_claim(claim) do
assert_object(claim, :assert_claim,
note: assert_optional(&assert_binary/1),
agreed_in: assert_optional(&assert_binary/1),
finished: assert_optional(&assert_boolean/1),
created: assert_optional(&assert_datetime/1),
due: assert_optional(&assert_datetime/1),
resource_classified_as: assert_optional(assert_list(&assert_url/1)),
)
end
def assert_resource_specification(%ResourceSpecification{} = spec) do
assert_resource_specification(Map.from_struct(spec))
end
......
......@@ -9,10 +9,42 @@ defmodule ValueFlows.Claim.ClaimsTest do
alias ValueFlows.Claim.Claims
describe "create" do
test "creates a new claim with a creator" do
test "with only required parameters" do
user = fake_user!()
provider = fake_user!()
receiver = fake_user!()
assert {:ok, claim} = Claims.create(user, provider, receiver, claim())
assert_claim(claim)
assert claim.creator.id == user.id
assert claim.provider.id == provider.id
assert claim.receiver.id == receiver.id
end
test "with a context" do
user = fake_user!()
provider = fake_user!()
receiver = fake_user!()
attrs = %{
in_scope_of: [fake_community!(user).id]
}
assert {:ok, claim} = Claims.create(user, provider, receiver, claim(attrs))
assert_claim(claim)
assert claim.context.id == hd(attrs.in_scope_of)
end
test "with measure quantities" do
end
test "with a resource specification" do
end
test "with a triggered by event" do
assert {:ok, %Claim{} = claim} = Claims.create(user, claim())
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment