Commit e8f65fdb authored by Antonis Kalou's avatar Antonis Kalou

Add ValueFlows.Claims and queries

parent f500850d
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.Claims do
alias CommonsPub.Repo
alias CommonsPub.Users.User
alias ValueFlows.Claim
alias ValueFlows.Claim.Queries
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
Repo.transact_with(fn ->
with {:ok, claim} <- Repo.insert(Claim.create_changeset(creator, attrs)) do
{:ok, %{claim | creator: creator}}
end
end)
end
def update(%Claim{} = claim, %{} = attrs) do
end
def soft_delete(%Claim{} = claim) do
end
end
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.Queries do
import Ecto.Query
alias ValueFlows.Claim
def query(Claim) do
from(c in Claim, as: :claim)
end
def query(q, filters), do: filter(query(q), filters)
def queries(query, _page_opts, base_filters, data_filters, count_filters) do
base_q = query(query, base_filters)
data_q = filter(base_q, data_filters)
count_q = filter(base_q, count_filters)
{data_q, count_q}
end
def join_to(q, spec, join_qualifier \\ :left)
def join_to(q, specs, jq) when is_list(specs) do
Enum.reduce(specs, q, &join_to(&2, &1, jq))
end
def join_to(q, :context, jq) do
join(q, jq, [claim: c], c2 in assoc(c, :context), as: :context)
end
def filter(q, filters) when is_list(filters) do
Enum.reduce(filters, q, &filter(&2, &1))
end
def filter(q, {:join, {join, qual}}), do: join_to(q, join, qual)
def filter(q, {:join, join}), do: join_to(q, join)
def filter(q, :default) do
filter(q, [:deleted])
end
def filter(q, :deleted) do
where(q, [claim: c], is_nil(c.deleted_at))
end
def filter(q, {:id, id}) when is_binary(id) do
where(q, [claim: c], c.id == ^id)
end
def filter(q, {:id, ids}) when is_list(ids) do
where(q, [claim: c], c.id in ^ids)
end
end
......@@ -21,6 +21,11 @@ defmodule ValueFlows.Simulate do
### Start fake data functions
def claim(base \\ %{}) do
base
|> Map.put_new_lazy(:note, &summary/0)
end
def agent_type(), do: Faker.Util.pick([:person, :organization])
def agent(base \\ %{}) do
......
defmodule ValueFlows.Claim.ClaimsTest do
use CommonsPub.Web.ConnCase, async: true
import CommonsPub.Test.Faking
import ValueFlows.Simulate
import ValueFlows.Test.Faking
alias ValueFlows.Claim
alias ValueFlows.Claim.Claims
describe "create" do
test "creates a new claim with a creator" do
user = fake_user!()
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