Do not show deleted resources in the client and cleanup connections table

This commit is contained in:
Andrew Dryga
2024-02-05 10:28:05 -06:00
parent 6345eef8a4
commit a969e7b555
3 changed files with 25 additions and 3 deletions

View File

@@ -64,7 +64,7 @@ defmodule Domain.Resources do
{preload, _opts} = Keyword.pop(opts, :preload, [])
{:ok, resources} =
Resource.Query.all()
Resource.Query.not_deleted()
|> Resource.Query.by_account_id(subject.account.id)
|> Resource.Query.by_authorized_actor_id(subject.actor.id)
|> Resource.Query.with_at_least_one_gateway_group()

View File

@@ -0,0 +1,15 @@
defmodule Domain.Repo.Migrations.DeleteConnectionsForDeletedResources do
use Ecto.Migration
def change do
execute("""
DELETE FROM resource_connections
WHERE resource_id IN (SELECT id FROM resources WHERE deleted_at IS NOT NULL)
""")
execute("""
DELETE FROM resource_connections
WHERE gateway_group_id IN (SELECT id FROM gateway_groups WHERE deleted_at IS NOT NULL)
""")
end
end

View File

@@ -336,7 +336,14 @@ defmodule Domain.ResourcesTest do
actor: actor,
subject: subject
} do
resource = Fixtures.Resources.create_resource(account: account)
gateway_group = Fixtures.Gateways.create_group(account: account)
resource =
Fixtures.Resources.create_resource(
account: account,
connections: [%{gateway_group_id: gateway_group.id}]
)
actor_group = Fixtures.Actors.create_group(account: account)
Fixtures.Actors.create_membership(account: account, actor: actor, group: actor_group)
@@ -346,7 +353,7 @@ defmodule Domain.ResourcesTest do
resource: resource
)
{:ok, _resource} = delete_resource(resource, subject)
resource |> Ecto.Changeset.change(deleted_at: DateTime.utc_now()) |> Repo.update!()
assert list_authorized_resources(subject) == {:ok, []}
end