Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow project users to manage collections #2898

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to
[#2830](https://github.com/OpenFn/lightning/issues/2830)
- Adds project name to failure alert email
[#2884](https://github.com/OpenFn/lightning/pull/2884)
- Allow project users to manage collections
[#2838](https://github.com/OpenFn/lightning/issues/2838)

### Changed

Expand Down
11 changes: 11 additions & 0 deletions lib/lightning/collections.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Lightning.Collections do
alias Lightning.Collections.Collection
alias Lightning.Collections.Item
alias Lightning.Extensions.Message
alias Lightning.Projects.Project
alias Lightning.Repo
alias Lightning.Services.CollectionHook

Expand Down Expand Up @@ -41,6 +42,16 @@ defmodule Lightning.Collections do
Repo.all(from(c in Collection, order_by: ^order_by, preload: ^preload))
end

@spec list_project_collections(Project.t()) :: [Collection.t(), ...] | []
def list_project_collections(%Project{id: project_id}) do
query =
from c in Collection,
where: c.project_id == ^project_id,
order_by: [desc: :inserted_at]

Repo.all(query)
end

@spec get_collection(String.t()) ::
{:ok, Collection.t()} | {:error, :not_found}
def get_collection(name) do
Expand Down
29 changes: 29 additions & 0 deletions lib/lightning/collections/collection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ defmodule Lightning.Collections.Collection do
schema "collections" do
field :name, :string
field :byte_size_sum, :integer
field :raw_name, :string, virtual: true
field :delete, :boolean, virtual: true

belongs_to :project, Lightning.Projects.Project
has_many :items, Lightning.Collections.Item

Expand All @@ -42,4 +44,31 @@ defmodule Lightning.Collections.Collection do
message: "A collection with this name already exists"
)
end

defp validate_changeset(changeset) do
changeset
|> validate_format(:name, ~r/^[a-z0-9]+([\-_.][a-z0-9]+)*$/,
message: "Collection name must be URL safe"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful for the users to know the exact rule. Something like only digits, letters and the separating symbols '-', '_' or '.'

)
|> unique_constraint([:name],
message: "A collection with this name already exists"
)
end

def form_changeset(collection, attrs) do
collection
|> cast(attrs, [:raw_name])
|> validate_required([:raw_name])
|> then(fn changeset ->
case get_change(changeset, :raw_name) do
nil ->
changeset

raw_name ->
changeset
|> put_change(:name, Lightning.Helpers.url_safe_name(raw_name))
end
end)
|> validate_changeset()
end
end
4 changes: 3 additions & 1 deletion lib/lightning/policies/project_users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Lightning.Policies.ProjectUsers do
| :write_webhook_auth_method
| :write_github_connection
| :initiate_github_sync
| :create_collection

@doc """
authorize/3 takes an action, a user, and a project. It checks the user's role
Expand Down Expand Up @@ -84,7 +85,8 @@ defmodule Lightning.Policies.ProjectUsers do
:run_workflow,
:provision_project,
:create_project_credential,
:initiate_github_sync
:initiate_github_sync,
:create_collection
],
do: project_user.role in [:owner, :admin, :editor]

Expand Down
7 changes: 0 additions & 7 deletions lib/lightning_web/components/layouts/settings.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@
<Heroicons.archive_box class="h-5 w-5 inline-block mr-2" />
<span class="inline-block align-middle">Audit</span>
</Menu.menu_item>
<Menu.menu_item
to={~p"/settings/collections"}
active={@active_menu_item == :collections}
>
<Heroicons.circle_stack class="h-5 w-5 inline-block mr-2" />
<span class="inline-block align-middle">Collections</span>
</Menu.menu_item>
<div class="grow"></div>
<Menu.menu_item to={~p"/projects"}>
<Icon.left class="h-5 w-5 inline-block mr-2" />
Expand Down
189 changes: 0 additions & 189 deletions lib/lightning_web/live/collection_live/collection_creation_modal.ex

This file was deleted.

Loading