diff --git a/lib/diffo/provider.ex b/lib/diffo/provider.ex index fae5a1f..50c5b0a 100644 --- a/lib/diffo/provider.ex +++ b/lib/diffo/provider.ex @@ -117,6 +117,12 @@ defmodule Diffo.Provider do action: :list_place_refs_by_instance_id, args: [:instance_id] + define :list_place_refs_by_party_id, action: :list_place_refs_by_party_id, args: [:party_id] + + define :list_place_refs_by_source_place_id, + action: :list_place_refs_by_source_place_id, + args: [:source_place_id] + define :update_place_ref, action: :update define :delete_place_ref, action: :destroy end @@ -141,6 +147,12 @@ defmodule Diffo.Provider do action: :list_party_refs_by_instance_id, args: [:instance_id] + define :list_party_refs_by_place_id, action: :list_party_refs_by_place_id, args: [:place_id] + + define :list_party_refs_by_source_party_id, + action: :list_party_refs_by_source_party_id, + args: [:source_party_id] + define :update_party_ref, action: :update define :delete_party_ref, action: :destroy end diff --git a/lib/diffo/provider/components/base_instance.ex b/lib/diffo/provider/components/base_instance.ex index acdf1fb..450359d 100644 --- a/lib/diffo/provider/components/base_instance.ex +++ b/lib/diffo/provider/components/base_instance.ex @@ -35,8 +35,8 @@ defmodule Diffo.Provider.BaseInstance do {:entities, :RELATES, :outgoing, :EntityRef}, {:notes, :ANNOTATES, :incoming, :Note}, {:event, :FIRED, :outgoing, :Event}, - {:places, :LOCATED_BY, :outgoing, :PlaceRef}, - {:parties, :INVOLVED_WITH, :outgoing, :PartyRef} + {:places, :RELATES, :outgoing, :PlaceRef}, + {:parties, :RELATES, :outgoing, :PartyRef} ] label :Instance diff --git a/lib/diffo/provider/components/party.ex b/lib/diffo/provider/components/party.ex index cbf66cb..b53f3b3 100644 --- a/lib/diffo/provider/components/party.ex +++ b/lib/diffo/provider/components/party.ex @@ -23,7 +23,7 @@ defmodule Diffo.Provider.Party do translate id: :key relate [ - {:party_refs, :INVOLVED_WITH, :incoming, :PartyRef}, + {:party_refs, :RELATES, :incoming, :PartyRef}, {:external_identifiers, :OWNS, :outgoing, :ExternalIdentifier}, {:notes, :AUTHORS, :outgoing, :Note} ] diff --git a/lib/diffo/provider/components/party_ref.ex b/lib/diffo/provider/components/party_ref.ex index 28dced8..f534ad4 100644 --- a/lib/diffo/provider/components/party_ref.ex +++ b/lib/diffo/provider/components/party_ref.ex @@ -21,8 +21,10 @@ defmodule Diffo.Provider.PartyRef do neo4j do relate [ - {:instance, :INVOLVED_WITH, :incoming, :Instance}, - {:party, :INVOLVED_WITH, :outgoing, :Party} + {:instance, :RELATES, :incoming, :Instance}, + {:place, :RELATES, :incoming, :Place}, + {:source_party, :RELATES, :incoming, :Party}, + {:party, :RELATES, :outgoing, :Party} ] end @@ -50,13 +52,17 @@ defmodule Diffo.Provider.PartyRef do defaults [:read, :destroy] create :create do - description "creates a party ref, relating an instance and a party" + description "creates a party ref, relating an instance, place or source party to a party" accept [:role] argument :instance_id, :uuid + argument :place_id, :string + argument :source_party_id, :string argument :party_id, :string change manage_relationship(:instance_id, :instance, type: :append_and_remove) + change manage_relationship(:place_id, :place, type: :append_and_remove) + change manage_relationship(:source_party_id, :source_party, type: :append_and_remove) change manage_relationship(:party_id, :party, type: :append_and_remove) end @@ -70,6 +76,18 @@ defmodule Diffo.Provider.PartyRef do filter expr(instance_id == ^arg(:instance_id)) end + read :list_party_refs_by_place_id do + description "lists party refs by place id" + argument :place_id, :string + filter expr(place_id == ^arg(:place_id)) + end + + read :list_party_refs_by_source_party_id do + description "lists party refs by source_party id" + argument :source_party_id, :string + filter expr(source_party_id == ^arg(:source_party_id)) + end + read :list_party_refs_by_party_id do description "lists party refs by party id" argument :party_id, :string @@ -100,7 +118,7 @@ defmodule Diffo.Provider.PartyRef do end attribute :role, :atom do - description "the role of the party to the instance" + description "the role of the party to the instance, place or source party" allow_nil? true public? true end @@ -113,7 +131,21 @@ defmodule Diffo.Provider.PartyRef do relationships do belongs_to :instance, Diffo.Provider.Instance do description "the instance which relates to a party via this party ref" - allow_nil? false + allow_nil? true + public? true + end + + belongs_to :place, Diffo.Provider.Place do + description "the place which relates to a party via this party ref" + attribute_type :string + allow_nil? true + public? true + end + + belongs_to :source_party, Diffo.Provider.Party do + description "the source party which relates to a party via this party ref" + attribute_type :string + allow_nil? true public? true end @@ -129,6 +161,20 @@ defmodule Diffo.Provider.PartyRef do identity :instance_party_uniqueness, [:instance_id, :party_id] do message "another party ref exists" end + + identity :place_party_uniqueness, [:place_id, :party_id] do + message "another party ref exists" + end + + identity :source_party_party_uniqueness, [:source_party_id, :party_id] do + message "another party ref exists" + end + end + + validations do + validate present([:instance_id, :place_id, :source_party_id], exactly: 1) do + message "a party ref can relate either an instance, place or source party" + end end preparations do diff --git a/lib/diffo/provider/components/place.ex b/lib/diffo/provider/components/place.ex index 29cc186..0cac10f 100644 --- a/lib/diffo/provider/components/place.ex +++ b/lib/diffo/provider/components/place.ex @@ -23,7 +23,7 @@ defmodule Diffo.Provider.Place do translate id: :key relate [ - {:place_refs, :LOCATED_BY, :incoming, :PlaceRef} + {:place_refs, :RELATES, :incoming, :PlaceRef} ] end diff --git a/lib/diffo/provider/components/place_ref.ex b/lib/diffo/provider/components/place_ref.ex index 8533874..659e159 100644 --- a/lib/diffo/provider/components/place_ref.ex +++ b/lib/diffo/provider/components/place_ref.ex @@ -21,8 +21,10 @@ defmodule Diffo.Provider.PlaceRef do neo4j do relate [ - {:instance, :LOCATED_BY, :incoming, :Instance}, - {:place, :LOCATED_BY, :outgoing, :Place} + {:instance, :RELATES, :incoming, :Instance}, + {:party, :RELATES, :incoming, :Party}, + {:source_place, :RELATES, :incoming, :Place}, + {:place, :RELATES, :outgoing, :Place} ] end @@ -50,12 +52,16 @@ defmodule Diffo.Provider.PlaceRef do defaults [:read, :destroy] create :create do - description "creates a place ref, relating an instance and a place" + description "creates a place ref, relating an instance, party or source place to a place" accept [:role] argument :instance_id, :uuid + argument :party_id, :string + argument :source_place_id, :string argument :place_id, :string change manage_relationship(:instance_id, :instance, type: :append_and_remove) + change manage_relationship(:party_id, :party, type: :append_and_remove) + change manage_relationship(:source_place_id, :source_place, type: :append_and_remove) change manage_relationship(:place_id, :place, type: :append_and_remove) end @@ -69,6 +75,18 @@ defmodule Diffo.Provider.PlaceRef do filter expr(instance_id == ^arg(:instance_id)) end + read :list_place_refs_by_party_id do + description "lists place refs by party id" + argument :party_id, :string + filter expr(party_id == ^arg(:party_id)) + end + + read :list_place_refs_by_source_place_id do + description "lists place refs by source_place id" + argument :source_place_id, :string + filter expr(source_place_id == ^arg(:source_place_id)) + end + read :list_place_refs_by_place_id do description "lists place refs by place id" argument :place_id, :string @@ -88,7 +106,7 @@ defmodule Diffo.Provider.PlaceRef do end attribute :role, :atom do - description "the role of the place to the instance" + description "the role of the place to the instance, party or source place" allow_nil? true public? true end @@ -101,7 +119,21 @@ defmodule Diffo.Provider.PlaceRef do relationships do belongs_to :instance, Diffo.Provider.Instance do description "the instance which relates to a place via this place ref" - allow_nil? false + allow_nil? true + public? true + end + + belongs_to :party, Diffo.Provider.Party do + description "the party which relates to a place via this place ref" + attribute_type :string + allow_nil? true + public? true + end + + belongs_to :source_place, Diffo.Provider.Place do + description "the source place which relates to a place via this place ref" + attribute_type :string + allow_nil? true public? true end @@ -117,6 +149,20 @@ defmodule Diffo.Provider.PlaceRef do identity :instance_place_uniqueness, [:instance_id, :place_id] do message "another place ref exists relating the same instance and place" end + + identity :party_place_uniqueness, [:party_id, :place_id] do + message "another place ref exists" + end + + identity :source_place_place_uniqueness, [:source_place_id, :place_id] do + message "another place ref exists" + end + end + + validations do + validate present([:instance_id, :party_id, :source_place_id], exactly: 1) do + message "a place ref can relate either an instance, party or source place" + end end preparations do diff --git a/mix.exs b/mix.exs index 1220213..a5fc6ed 100644 --- a/mix.exs +++ b/mix.exs @@ -94,7 +94,7 @@ defmodule Diffo.MixProject do {:ash_outstanding, "~> 0.2.3"}, {:ash_jason, "~> 3.0"}, {:ash_state_machine, "~> 0.2.12"}, - {:ash_neo4j, ash_neo4j_version("~> 0.2.11")}, + {:ash_neo4j, ash_neo4j_version("~> 0.2.12")}, {:boltx, "~> 0.0.6"}, {:ash, ash_version("~> 3.0 and >= 3.6.2")}, {:uuid, "~> 1.1"}, diff --git a/mix.lock b/mix.lock index ef867e2..9c3c9da 100644 --- a/mix.lock +++ b/mix.lock @@ -1,7 +1,7 @@ %{ "ash": {:hex, :ash, "3.9.0", "004371ffb181a142cda09544342dad1ffedf360a5636219d71cb5431ccbe3ad6", [:mix], [{:crux, ">= 0.1.2 and < 1.0.0-0", [hex: :crux, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.6.29 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.11", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.3.3 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, ">= 0.2.6 and < 1.0.0-0", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e3e80a182c6e8b4d87f1caa4dba774da210f1f75f1420650ba012eb4388dc8c3"}, "ash_jason": {:hex, :ash_jason, "3.0.3", "05349fe257c958ce84bcfc35871da7647960fcbcb288025623d1eb19839dc5fd", [:mix], [{:ash, ">= 3.6.2 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:ex_check, "~> 0.16.0", [hex: :ex_check, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:spark, ">= 2.1.21 and < 3.0.0", [hex: :spark, repo: "hexpm", optional: false]}], "hexpm", "1ef2cd24be16a46ee46a9e0de1f319acc6cc71ff31e0bc3e7bfd800955a4c550"}, - "ash_neo4j": {:hex, :ash_neo4j, "0.2.11", "41433b79c8dfe1f371faf411757c03b44cb56f14870375e80b348819fb17ddf1", [:mix], [{:ash, ">= 3.6.2 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:boltx, ">= 0.0.6", [hex: :boltx, repo: "hexpm", optional: false]}], "hexpm", "b26a165908af327d45e951794c0fc1eaf4002d23239c41d6c4c01b1203471293"}, + "ash_neo4j": {:hex, :ash_neo4j, "0.2.12", "cc662833f3d4eb10ff36b217a1693c6deff3a878a9aea3baaf66090d987bb54a", [:mix], [{:ash, ">= 3.6.2 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:boltx, ">= 0.0.6", [hex: :boltx, repo: "hexpm", optional: false]}], "hexpm", "fda719bfc002ce0840d62eabbf0502ab79dd1e249d852aa3eb0bee457bf96352"}, "ash_outstanding": {:hex, :ash_outstanding, "0.2.3", "dc8ec13028ea7bd1d74b46569b9db08f0d275d63700e2418d9e33fe4b21af2eb", [:mix], [{:ash, ">= 3.6.2 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:outstanding, "~> 0.2.4", [hex: :outstanding, repo: "hexpm", optional: false]}], "hexpm", "05e2718b59937d9f7e77b7bc90f70e8f28c3f328de7cabf3ea55ca04a1abed52"}, "ash_state_machine": {:hex, :ash_state_machine, "0.2.12", "c0f7ebb8a176584f70c6ed196b7d0118c930d73e0590ade705d2dddc48aa7311", [:mix], [{:ash, ">= 3.4.66 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}], "hexpm", "394ce761ce82358e3c715e1cae6c5cf1390be27c03a8b661f2e5a2fda849873d"}, "boltx": {:hex, :boltx, "0.0.6", "c6a396b1538b258e4d5ee2a94aaf8fb2c7879240efffba94b9159dbdce963790", [:mix], [{:db_connection, "~> 2.6.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 5.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "576b8f21a2021674130d04cd1fc79a4829a23d2cdf50641b3d7a00ce31b98ead"}, @@ -31,7 +31,7 @@ "outstanding": {:hex, :outstanding, "0.2.5", "2f40416eb9617748cb1f8ae4c8ed94515d731f9c4fcee4f902355d30bc0792cc", [:mix], [], "hexpm", "bb47a210f0d2804ea6b8477fa6f4d15e8c58c18acee79d8e06c9296e6dd004cd"}, "owl": {:hex, :owl, "0.13.0", "26010e066d5992774268f3163506972ddac0a7e77bfe57fa42a250f24d6b876e", [:mix], [{:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: true]}], "hexpm", "59bf9d11ce37a4db98f57cb68fbfd61593bf419ec4ed302852b6683d3d2f7475"}, "reactor": {:hex, :reactor, "0.17.0", "eb8bdb530dbae824e2d36a8538f8ec4f3aa7c2d1b61b04959fa787c634f88b49", [:mix], [{:igniter, "~> 0.4", [hex: :igniter, repo: "hexpm", optional: true]}, {:iterex, "~> 0.1", [hex: :iterex, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:libgraph, "~> 0.16", [hex: :libgraph, repo: "hexpm", optional: false]}, {:spark, ">= 2.3.3 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.11", [hex: :yaml_elixir, repo: "hexpm", optional: false]}, {:ymlr, "~> 5.0", [hex: :ymlr, repo: "hexpm", optional: false]}], "hexpm", "3c3bf71693adbad9117b11ec83cfed7d5851b916ade508ed9718de7ae165bf25"}, - "req": {:hex, :req, "0.5.15", "662020efb6ea60b9f0e0fac9be88cd7558b53fe51155a2d9899de594f9906ba9", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "a6513a35fad65467893ced9785457e91693352c70b58bbc045b47e5eb2ef0c53"}, + "req": {:hex, :req, "0.5.16", "99ba6a36b014458e52a8b9a0543bfa752cb0344b2a9d756651db1281d4ba4450", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "974a7a27982b9b791df84e8f6687d21483795882a7840e8309abdbe08bb06f09"}, "rewrite": {:hex, :rewrite, "1.2.0", "80220eb14010e175b67c939397e1a8cdaa2c32db6e2e0a9d5e23e45c0414ce21", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "a1cd702bbb9d51613ab21091f04a386d750fc6f4516b81900df082d78b2d8c50"}, "sourceror": {:hex, :sourceror, "1.10.0", "38397dedbbc286966ec48c7af13e228b171332be1ad731974438c77791945ce9", [:mix], [], "hexpm", "29dbdfc92e04569c9d8e6efdc422fc1d815f4bd0055dc7c51b8800fb75c4b3f1"}, "spark": {:hex, :spark, "2.3.14", "a08420d08e6e0e49d740aed3e160f1cb894ba8f6b3f5e6c63253e9df1995265c", [:mix], [{:igniter, ">= 0.3.64 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: true]}], "hexpm", "af50c4ea5dd67eba822247f1c98e1d4e598cb7f6c28ccf5d002f0e0718096f4f"}, diff --git a/test/provider/event_test.exs b/test/provider/event_test.exs index 9450162..67f65ec 100644 --- a/test/provider/event_test.exs +++ b/test/provider/event_test.exs @@ -13,8 +13,7 @@ defmodule Diffo.Provider.EventTest do setup do on_exit(fn -> - # AshNeo4j.Neo4jHelper.delete_all() - :ok + AshNeo4j.Neo4jHelper.delete_all() end) end @@ -149,7 +148,6 @@ defmodule Diffo.Provider.EventTest do ) end - @tag debug: true test "fired instance events are chained - success" do specification = Diffo.Provider.create_specification!(%{name: "nbnAccess"}) instance = Diffo.Provider.create_instance!(%{specified_by: specification.id}) diff --git a/test/provider/party_ref_test.exs b/test/provider/party_ref_test.exs index fd21bf0..3e64959 100644 --- a/test/provider/party_ref_test.exs +++ b/test/provider/party_ref_test.exs @@ -39,13 +39,13 @@ defmodule Diffo.Provider.PartyRefTest do Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party1.id }) Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party2.id }) @@ -119,6 +119,79 @@ defmodule Diffo.Provider.PartyRefTest do assert List.last(party_refs).role == :SiteOwner end + test "list party refs by related place - success" do + place1 = + Diffo.Provider.create_place!(%{ + id: "LOC000000123456", + name: :locationId, + referredType: :GeographicAddress + }) + + place2 = + Diffo.Provider.create_place!(%{ + id: "LOC000000897353", + name: :locationId, + referredType: :GeographicAddress + }) + + party1 = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + referredType: :Individual + }) + + party2 = + Diffo.Provider.create_party!(%{ + id: "IND000000897354", + name: :individualId, + referredType: :Individual + }) + + party3 = + Diffo.Provider.create_party!(%{ + id: "ORG000163435034", + name: :organizationId, + referredType: :Organization + }) + + Diffo.Provider.create_party_ref!(%{ + place_id: place1.id, + role: :Visitor, + party_id: party1.id + }) + + Diffo.Provider.create_party_ref!(%{ + place_id: place1.id, + role: :Visitor, + party_id: party2.id + }) + + Diffo.Provider.create_party_ref!(%{ + place_id: place1.id, + role: :Resident, + party_id: party3.id + }) + + Diffo.Provider.create_party_ref!(%{ + place_id: place2.id, + role: :Resident, + party_id: party1.id + }) + + Diffo.Provider.create_party_ref!(%{ + place_id: place2.id, + role: :Resident, + party_id: party2.id + }) + + party_refs = Diffo.Provider.list_party_refs_by_place_id!(place1.id) + assert length(party_refs) == 3 + # should be sorted by role + assert List.first(party_refs).role == :Resident + assert List.last(party_refs).role == :Visitor + end + test "list party refs by related party id - success" do specification = Diffo.Provider.create_specification!(%{name: "nbnAccess"}) instance1 = Diffo.Provider.create_instance!(%{specified_by: specification.id}) @@ -181,6 +254,53 @@ defmodule Diffo.Provider.PartyRefTest do assert List.first(party_refs).role == :PrimaryContact assert List.last(party_refs).role == :SiteOwner end + + test "list party refs by source party id - success" do + party1 = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + referredType: :Individual + }) + + party2 = + Diffo.Provider.create_party!(%{ + id: "IND000000897354", + name: :individualId, + referredType: :Individual + }) + + party3 = + Diffo.Provider.create_party!(%{ + id: "ORG000000123456", + name: :organizationId, + referredType: :Organization + }) + + Diffo.Provider.create_party_ref!(%{ + source_party_id: party3.id, + role: :Employee, + party_id: party1.id + }) + + Diffo.Provider.create_party_ref!(%{ + source_party_id: party3.id, + role: :Contractor, + party_id: party2.id + }) + + Diffo.Provider.create_party_ref!(%{ + source_party_id: party2.id, + role: :Supervisor, + party_id: party1.id + }) + + party_refs = Diffo.Provider.list_party_refs_by_source_party_id!(party3.id) + assert length(party_refs) == 2 + # should be sorted by role + assert List.first(party_refs).role == :Contractor + assert List.last(party_refs).role == :Employee + end end describe "Diffo.Provider create PartyRefs" do @@ -198,11 +318,11 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) - assert party_ref.role == :Organization + assert party_ref.role == :PrimaryContact end end @@ -221,7 +341,7 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) @@ -243,12 +363,12 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :NetworkSite, + role: :PrimaryContact, party_id: party.id }) - updated_party_ref = party_ref |> Diffo.Provider.update_party_ref!(%{role: :Organization}) - assert updated_party_ref.role == :Organization + updated_party_ref = party_ref |> Diffo.Provider.update_party_ref!(%{role: :PrimaryContact}) + assert updated_party_ref.role == :PrimaryContact end test "update id - failure - not updatable" do @@ -265,7 +385,7 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) @@ -288,7 +408,7 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) @@ -312,7 +432,7 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) @@ -343,14 +463,14 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) encoding = Jason.encode!(party_ref) assert encoding == - "{\"id\":\"IND000000897353\",\"href\":\"party/internal/IND000000897353\",\"name\":\"individualId\",\"role\":\"Organization\",\"@type\":\"Individual\"}" + "{\"id\":\"IND000000897353\",\"href\":\"party/internal/IND000000897353\",\"name\":\"individualId\",\"role\":\"PrimaryContact\",\"@type\":\"Individual\"}" end test "encode json party referredType - success" do @@ -368,14 +488,14 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) encoding = Jason.encode!(party_ref) assert encoding == - "{\"id\":\"IND000000897353\",\"href\":\"party/internal/IND000000897353\",\"name\":\"individualId\",\"role\":\"Organization\",\"@referredType\":\"Individual\",\"@type\":\"PartyRef\"}" + "{\"id\":\"IND000000897353\",\"href\":\"party/internal/IND000000897353\",\"name\":\"individualId\",\"role\":\"PrimaryContact\",\"@referredType\":\"Individual\",\"@type\":\"PartyRef\"}" end end @@ -395,12 +515,12 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, party_id: party.id }) expected_party_ref = %Diffo.Provider.PartyRef{ - role: :Organization, + role: :PrimaryContact, party: %Diffo.Provider.Party{ id: ~r/IND\d{12}/, name: "individualId", @@ -414,7 +534,7 @@ defmodule Diffo.Provider.PartyRefTest do end describe "Diffo.Provider delete PartyRefs" do - test "delete place_ref with related instance - success" do + test "delete party_ref with related instance - success" do specification = Diffo.Provider.create_specification!(%{name: "nbnAccess"}) instance = Diffo.Provider.create_instance!(%{specified_by: specification.id}) @@ -429,7 +549,61 @@ defmodule Diffo.Provider.PartyRefTest do party_ref = Diffo.Provider.create_party_ref!(%{ instance_id: instance.id, - role: :Organization, + role: :PrimaryContact, + party_id: party.id + }) + + :ok = Diffo.Provider.delete_party_ref(party_ref) + {:error, _error} = Diffo.Provider.get_party_ref_by_id(party_ref.id) + end + + test "delete party_ref with related place - success" do + place = + Diffo.Provider.create_place!(%{ + id: "LOC000000897353", + name: :locationId, + referredType: :GeographicAddress + }) + + party = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + href: "party/internal/IND000000897353", + referredType: :Individual + }) + + party_ref = + Diffo.Provider.create_party_ref!(%{ + place_id: place.id, + role: :TechnicalContact, + party_id: party.id + }) + + :ok = Diffo.Provider.delete_party_ref(party_ref) + {:error, _error} = Diffo.Provider.get_party_ref_by_id(party_ref.id) + end + + test "delete party_ref with related source party - success" do + source_party = + Diffo.Provider.create_party!(%{ + id: "ORG000163435034", + name: :organizationId, + referredType: :Organization + }) + + party = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + href: "party/internal/IND000000897353", + referredType: :Individual + }) + + party_ref = + Diffo.Provider.create_party_ref!(%{ + source_party_id: source_party.id, + role: :Employee, party_id: party.id }) diff --git a/test/provider/place_ref_test.exs b/test/provider/place_ref_test.exs index c2fbeec..4d9ba43 100644 --- a/test/provider/place_ref_test.exs +++ b/test/provider/place_ref_test.exs @@ -113,6 +113,74 @@ defmodule Diffo.Provider.PlaceRefTest do assert List.last(place_refs).role == :ServingArea end + test "list place refs by related party id - success" do + party1 = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + referredType: :Individual + }) + + party2 = + Diffo.Provider.create_party!(%{ + id: "IND000000897354", + name: :individualId, + referredType: :Individual + }) + + party3 = + Diffo.Provider.create_party!(%{ + id: "ORG000163435034", + name: :organizationId, + referredType: :Organization + }) + + place1 = + Diffo.Provider.create_place!(%{ + id: "LOC000000123456", + name: :locationId, + referredType: :GeographicAddress + }) + + place2 = + Diffo.Provider.create_place!(%{ + id: "LOC000000897353", + name: :locationId, + referredType: :GeographicAddress + }) + + Diffo.Provider.create_place_ref!(%{ + party_id: party1.id, + role: :Residence, + place_id: place1.id + }) + + Diffo.Provider.create_place_ref!(%{ + party_id: party2.id, + role: :Residence, + place_id: place1.id + }) + + Diffo.Provider.create_place_ref!(%{ + party_id: party2.id, + role: :Office, + place_id: place2.id + }) + + Diffo.Provider.create_place_ref!(%{ + party_id: party3.id, + role: :Office, + place_id: place2.id + }) + + place_refs = + Diffo.Provider.list_place_refs_by_place_id!(place2.id) + + assert length(place_refs) == 2 + # should be sorted newest to oldest + Enum.each(place_refs, fn place_ref -> assert place_ref.place_id == place2.id end) + end + test "list place refs by related place id - success" do specification = Diffo.Provider.create_specification!(%{name: "nbnAccess"}) instance1 = Diffo.Provider.create_instance!(%{specified_by: specification.id}) @@ -170,6 +238,61 @@ defmodule Diffo.Provider.PlaceRefTest do # should be sorted newest to oldest Enum.each(place_refs, fn place_ref -> assert place_ref.place_id == place3.id end) end + + test "list place refs by source place id - success" do + place1 = + Diffo.Provider.create_place!(%{ + id: "3NBA", + href: "place/nbnco/3NBA", + name: :poiId, + type: :GeographicSite + }) + + place2 = + Diffo.Provider.create_place!(%{ + id: "CSA000000124343", + name: :csaId, + type: :GeographicLocation + }) + + place3 = + Diffo.Provider.create_place!(%{ + id: "LOC000000897353", + name: :locationId, + type: :GeographicAddress + }) + + place4 = + Diffo.Provider.create_place!(%{ + id: "LOC000000897354", + name: :locationId, + type: :GeographicAddress + }) + + Diffo.Provider.create_place_ref!(%{ + source_place_id: place2.id, + role: :NetworkNetworkInterface, + place_id: place1.id + }) + + Diffo.Provider.create_place_ref!(%{ + source_place_id: place2.id, + role: :UserNetworkInterface, + place_id: place3.id + }) + + Diffo.Provider.create_place_ref!(%{ + source_place_id: place2.id, + role: :UserNetworkInterface, + place_id: place4.id + }) + + place_refs = Diffo.Provider.list_place_refs_by_source_place_id!(place2.id) + assert length(place_refs) == 3 + # should be sorted by role + assert List.first(place_refs).role == :NetworkNetworkInterface + assert List.last(place_refs).role == :UserNetworkInterface + end end describe "Diffo.Provider create PlaceRefs" do @@ -425,6 +548,60 @@ defmodule Diffo.Provider.PlaceRefTest do :ok = Diffo.Provider.delete_place_ref(place_ref) {:error, _error} = Diffo.Provider.get_place_ref_by_id(place_ref.id) end + + test "delete place_ref with related party - success" do + party = + Diffo.Provider.create_party!(%{ + id: "IND000000897353", + name: :individualId, + href: "party/internal/IND000000897353", + referredType: :Individual + }) + + place = + Diffo.Provider.create_place!(%{ + id: "LOC000000897353", + name: :locationId, + referredType: :GeographicAddress + }) + + place_ref = + Diffo.Provider.create_place_ref!(%{ + party_id: party.id, + role: :Residence, + place_id: place.id + }) + + :ok = Diffo.Provider.delete_place_ref(place_ref) + {:error, _error} = Diffo.Provider.get_place_ref_by_id(place_ref.id) + end + + test "delete place_ref with related source place - success" do + source_place = + Diffo.Provider.create_place!(%{ + id: "3NBA", + href: "place/nbnco/3NBA", + name: :poiId, + type: :GeographicSite + }) + + place = + Diffo.Provider.create_place!(%{ + id: "CSA000000124343", + name: :csaId, + type: :GeographicLocation + }) + + place_ref = + Diffo.Provider.create_place_ref!(%{ + source_place_id: source_place.id, + role: :ServingArea, + place_id: place.id + }) + + :ok = Diffo.Provider.delete_place_ref(place_ref) + {:error, _error} = Diffo.Provider.get_place_ref_by_id(place_ref.id) + end end def delete_all_place_refs() do diff --git a/test/support/places.ex b/test/support/places.ex index 6cc29cf..b6f2840 100644 --- a/test/support/places.ex +++ b/test/support/places.ex @@ -25,7 +25,7 @@ defmodule Diffo.Test.Places do %{uuid: instance.id}, :PlaceRef, %{uuid: actual_place_ref.id}, - :LOCATED_BY, + :RELATES, :outgoing ) @@ -34,7 +34,7 @@ defmodule Diffo.Test.Places do %{uuid: actual_place_ref.id}, :Place, %{key: actual_place_ref.place_id}, - :LOCATED_BY, + :RELATES, :outgoing ) end)