From 6642fe0c5addb8456d5ab3683d7c02316d5d4367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Sat, 14 Jan 2023 14:48:38 +0000 Subject: [PATCH 1/7] add collaborators for departments --- lib/atomic/accounts.ex | 96 +++++++++++++++++++ lib/atomic/accounts/collaborator.ex | 20 ++++ .../accounts/collaborator_department.ex | 30 ++++++ lib/atomic/accounts/user.ex | 4 +- lib/atomic/departments/department.ex | 2 + .../20230114142345_create_collaborators.exs | 13 +++ ...142351_create_collaborator_departments.exs | 14 +++ test/atomic/accounts_test.exs | 56 +++++++++++ test/support/fixtures/accounts_fixtures.ex | 12 +++ 9 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 lib/atomic/accounts/collaborator.ex create mode 100644 lib/atomic/accounts/collaborator_department.ex create mode 100644 priv/repo/migrations/20230114142345_create_collaborators.exs create mode 100644 priv/repo/migrations/20230114142351_create_collaborator_departments.exs diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 5cf7b30d5..9f54c6c21 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -350,4 +350,100 @@ defmodule Atomic.Accounts do {:error, :user, changeset, _} -> {:error, changeset} end end + + alias Atomic.Accounts.Collaborator + + @doc """ + Returns the list of collaborators. + + ## Examples + + iex> list_collaborators() + [%Collaborator{}, ...] + + """ + def list_collaborators do + Repo.all(Collaborator) + end + + @doc """ + Gets a single collaborator. + + Raises `Ecto.NoResultsError` if the Collaborator does not exist. + + ## Examples + + iex> get_collaborator!(123) + %Collaborator{} + + iex> get_collaborator!(456) + ** (Ecto.NoResultsError) + + """ + def get_collaborator!(id), do: Repo.get!(Collaborator, id) + + @doc """ + Creates a collaborator. + + ## Examples + + iex> create_collaborator(%{field: value}) + {:ok, %Collaborator{}} + + iex> create_collaborator(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_collaborator(attrs \\ %{}) do + %Collaborator{} + |> Collaborator.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a collaborator. + + ## Examples + + iex> update_collaborator(collaborator, %{field: new_value}) + {:ok, %Collaborator{}} + + iex> update_collaborator(collaborator, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_collaborator(%Collaborator{} = collaborator, attrs) do + collaborator + |> Collaborator.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a collaborator. + + ## Examples + + iex> delete_collaborator(collaborator) + {:ok, %Collaborator{}} + + iex> delete_collaborator(collaborator) + {:error, %Ecto.Changeset{}} + + """ + def delete_collaborator(%Collaborator{} = collaborator) do + Repo.delete(collaborator) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking collaborator changes. + + ## Examples + + iex> change_collaborator(collaborator) + %Ecto.Changeset{data: %Collaborator{}} + + """ + def change_collaborator(%Collaborator{} = collaborator, attrs \\ %{}) do + Collaborator.changeset(collaborator, attrs) + end end diff --git a/lib/atomic/accounts/collaborator.ex b/lib/atomic/accounts/collaborator.ex new file mode 100644 index 000000000..e24c3c50f --- /dev/null +++ b/lib/atomic/accounts/collaborator.ex @@ -0,0 +1,20 @@ +defmodule Atomic.Accounts.Collaborator do + use Atomic.Schema + alias Atomic.Departments.Department + alias Atomic.Accounts.User + alias Atomic.Users.CollaboratorDepartment + + @required_fields ~w(name organization_id user_id)a + schema "collaborators" do + many_to_many :departments, Department, join_through: CollaboratorDepartment + belongs_to :user, User + timestamps() + end + + @doc false + def changeset(collaborator, attrs) do + collaborator + |> cast(attrs, @required_fields) + |> validate_required(@required_fields) + end +end diff --git a/lib/atomic/accounts/collaborator_department.ex b/lib/atomic/accounts/collaborator_department.ex new file mode 100644 index 000000000..c5c6ceaee --- /dev/null +++ b/lib/atomic/accounts/collaborator_department.ex @@ -0,0 +1,30 @@ +defmodule Atomic.Users.CollaboratorDepartment do + @moduledoc """ + An activity speaker + """ + use Atomic.Schema + + alias Atomic.Departments.Department + alias Atomic.Accounts.Collaborator + + @required_fields ~w(collaborator_id department_id)a + + schema "collaborator_departments" do + belongs_to :collaborator, Collaborator + belongs_to :department, Department + + timestamps() + end + + def changeset(collaborator_departments, attrs) do + collaborator_departments + |> cast(attrs, @required_fields) + |> validate_required(@required_fields) + end + + def create_changeset(collaborator_departments, attrs) do + collaborator_departments + |> cast(attrs, @required_fields) + |> validate_required(@required_fields) + end +end diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index eea76219a..2c90f8f4b 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -5,7 +5,7 @@ defmodule Atomic.Accounts.User do use Atomic.Schema alias Atomic.Activities.Enrollment - + alias Atomic.Accounts.Collaborator @roles ~w(admin staff student)a schema "users" do @@ -15,8 +15,8 @@ defmodule Atomic.Accounts.User do field :confirmed_at, :naive_datetime field :role, Ecto.Enum, values: @roles - has_many :enrollments, Enrollment + has_one :collaborator, Collaborator, on_replace: :delete_if_exists, on_delete: :delete_all timestamps() end diff --git a/lib/atomic/departments/department.ex b/lib/atomic/departments/department.ex index ef6b175fd..a40d03a33 100644 --- a/lib/atomic/departments/department.ex +++ b/lib/atomic/departments/department.ex @@ -5,6 +5,8 @@ defmodule Atomic.Departments.Department do use Atomic.Schema alias Atomic.Activities.Activity + alias Atomic.Accounts.Collaborator + alias Atomic.Users.CollaboratorDepartment @required_fields ~w(name)a diff --git a/priv/repo/migrations/20230114142345_create_collaborators.exs b/priv/repo/migrations/20230114142345_create_collaborators.exs new file mode 100644 index 000000000..00b08679f --- /dev/null +++ b/priv/repo/migrations/20230114142345_create_collaborators.exs @@ -0,0 +1,13 @@ +defmodule Atomic.Repo.Migrations.CreateCollaborators do + use Ecto.Migration + + def change do + create table(:collaborators, primary_key: false) do + add :id, :binary_id, primary_key: true + add :user_id, references(:users, on_delete: :nothing, type: :binary_id) + timestamps() + end + + create unique_index(:collaborators, [:user_id]) + end +end diff --git a/priv/repo/migrations/20230114142351_create_collaborator_departments.exs b/priv/repo/migrations/20230114142351_create_collaborator_departments.exs new file mode 100644 index 000000000..6aa7af582 --- /dev/null +++ b/priv/repo/migrations/20230114142351_create_collaborator_departments.exs @@ -0,0 +1,14 @@ +defmodule Atomic.Repo.Migrations.CreateCollaboratorDepartments do + use Ecto.Migration + + def change do + create table(:collaborator_departments, primary_key: false) do + add :id, :binary_id, primary_key: true + + add :collaborator_id, references(:collaborators, on_delete: :nothing, type: :binary_id) + add :department_id, references(:departments, on_delete: :nothing, type: :binary_id) + + timestamps() + end + end +end diff --git a/test/atomic/accounts_test.exs b/test/atomic/accounts_test.exs index 37fd45e56..81e51b70d 100644 --- a/test/atomic/accounts_test.exs +++ b/test/atomic/accounts_test.exs @@ -505,4 +505,60 @@ defmodule Atomic.AccountsTest do refute inspect(%User{password: "123456"}) =~ "password: \"123456\"" end end + + describe "collaborators" do + alias Atomic.Accounts.Collaborator + + import Atomic.AccountsFixtures + + @invalid_attrs %{} + + test "list_collaborators/0 returns all collaborators" do + collaborator = collaborator_fixture() + assert Accounts.list_collaborators() == [collaborator] + end + + test "get_collaborator!/1 returns the collaborator with given id" do + collaborator = collaborator_fixture() + assert Accounts.get_collaborator!(collaborator.id) == collaborator + end + + test "create_collaborator/1 with valid data creates a collaborator" do + valid_attrs = %{} + + assert {:ok, %Collaborator{} = collaborator} = Accounts.create_collaborator(valid_attrs) + end + + test "create_collaborator/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Accounts.create_collaborator(@invalid_attrs) + end + + test "update_collaborator/2 with valid data updates the collaborator" do + collaborator = collaborator_fixture() + update_attrs = %{} + + assert {:ok, %Collaborator{} = collaborator} = + Accounts.update_collaborator(collaborator, update_attrs) + end + + test "update_collaborator/2 with invalid data returns error changeset" do + collaborator = collaborator_fixture() + + assert {:error, %Ecto.Changeset{}} = + Accounts.update_collaborator(collaborator, @invalid_attrs) + + assert collaborator == Accounts.get_collaborator!(collaborator.id) + end + + test "delete_collaborator/1 deletes the collaborator" do + collaborator = collaborator_fixture() + assert {:ok, %Collaborator{}} = Accounts.delete_collaborator(collaborator) + assert_raise Ecto.NoResultsError, fn -> Accounts.get_collaborator!(collaborator.id) end + end + + test "change_collaborator/1 returns a collaborator changeset" do + collaborator = collaborator_fixture() + assert %Ecto.Changeset{} = Accounts.change_collaborator(collaborator) + end + end end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index 59e729391..3545254d1 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -28,4 +28,16 @@ defmodule Atomic.AccountsFixtures do [_, token | _] = String.split(captured_email.text_body, "[TOKEN]") token end + + @doc """ + Generate a collaborator. + """ + def collaborator_fixture(attrs \\ %{}) do + {:ok, collaborator} = + attrs + |> Enum.into(%{}) + |> Atomic.Accounts.create_collaborator() + + collaborator + end end From a6f0d75efbb2635e47a9eb680c601d7cb8c042cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Tue, 7 Mar 2023 10:26:50 +0000 Subject: [PATCH 2/7] Change relation to many_to_many --- lib/atomic/departments/department.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/atomic/departments/department.ex b/lib/atomic/departments/department.ex index 3152100e9..eb6628d6c 100644 --- a/lib/atomic/departments/department.ex +++ b/lib/atomic/departments/department.ex @@ -3,10 +3,11 @@ defmodule Atomic.Departments.Department do An activity """ use Atomic.Schema + alias Atomic.Users.CollaboratorDepartment alias Atomic.Organizations.Organization alias Atomic.Activities.Activity alias Atomic.Accounts.Collaborator - alias Atomic.Users.CollaboratorDepartment + alias Atomic.Activities.ActivityDepartment @required_fields ~w(name organization_id)a @@ -14,8 +15,8 @@ defmodule Atomic.Departments.Department do schema "departments" do field :name, :string - - has_many :activities, Activity + many_to_many :collaborators, Collaborator, join_through: CollaboratorDepartment + many_to_many :activities, Activity, join_through: ActivityDepartment belongs_to :organization, Organization, on_replace: :delete_if_exists From 735299c1ea4ee4f1838073562448db6d46616683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Tue, 7 Mar 2023 10:33:49 +0000 Subject: [PATCH 3/7] Remove unnecessary schema --- lib/atomic/accounts.ex | 96 ------------------- lib/atomic/accounts/collaborator.ex | 20 ---- .../accounts/collaborator_department.ex | 4 +- lib/atomic/accounts/user.ex | 4 +- lib/atomic/departments/department.ex | 4 +- .../20230114142345_create_collaborators.exs | 13 --- ...142351_create_collaborator_departments.exs | 2 +- test/support/fixtures/accounts_fixtures.ex | 12 --- 8 files changed, 7 insertions(+), 148 deletions(-) delete mode 100644 lib/atomic/accounts/collaborator.ex delete mode 100644 priv/repo/migrations/20230114142345_create_collaborators.exs diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 9f54c6c21..5cf7b30d5 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -350,100 +350,4 @@ defmodule Atomic.Accounts do {:error, :user, changeset, _} -> {:error, changeset} end end - - alias Atomic.Accounts.Collaborator - - @doc """ - Returns the list of collaborators. - - ## Examples - - iex> list_collaborators() - [%Collaborator{}, ...] - - """ - def list_collaborators do - Repo.all(Collaborator) - end - - @doc """ - Gets a single collaborator. - - Raises `Ecto.NoResultsError` if the Collaborator does not exist. - - ## Examples - - iex> get_collaborator!(123) - %Collaborator{} - - iex> get_collaborator!(456) - ** (Ecto.NoResultsError) - - """ - def get_collaborator!(id), do: Repo.get!(Collaborator, id) - - @doc """ - Creates a collaborator. - - ## Examples - - iex> create_collaborator(%{field: value}) - {:ok, %Collaborator{}} - - iex> create_collaborator(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def create_collaborator(attrs \\ %{}) do - %Collaborator{} - |> Collaborator.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a collaborator. - - ## Examples - - iex> update_collaborator(collaborator, %{field: new_value}) - {:ok, %Collaborator{}} - - iex> update_collaborator(collaborator, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_collaborator(%Collaborator{} = collaborator, attrs) do - collaborator - |> Collaborator.changeset(attrs) - |> Repo.update() - end - - @doc """ - Deletes a collaborator. - - ## Examples - - iex> delete_collaborator(collaborator) - {:ok, %Collaborator{}} - - iex> delete_collaborator(collaborator) - {:error, %Ecto.Changeset{}} - - """ - def delete_collaborator(%Collaborator{} = collaborator) do - Repo.delete(collaborator) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking collaborator changes. - - ## Examples - - iex> change_collaborator(collaborator) - %Ecto.Changeset{data: %Collaborator{}} - - """ - def change_collaborator(%Collaborator{} = collaborator, attrs \\ %{}) do - Collaborator.changeset(collaborator, attrs) - end end diff --git a/lib/atomic/accounts/collaborator.ex b/lib/atomic/accounts/collaborator.ex deleted file mode 100644 index e24c3c50f..000000000 --- a/lib/atomic/accounts/collaborator.ex +++ /dev/null @@ -1,20 +0,0 @@ -defmodule Atomic.Accounts.Collaborator do - use Atomic.Schema - alias Atomic.Departments.Department - alias Atomic.Accounts.User - alias Atomic.Users.CollaboratorDepartment - - @required_fields ~w(name organization_id user_id)a - schema "collaborators" do - many_to_many :departments, Department, join_through: CollaboratorDepartment - belongs_to :user, User - timestamps() - end - - @doc false - def changeset(collaborator, attrs) do - collaborator - |> cast(attrs, @required_fields) - |> validate_required(@required_fields) - end -end diff --git a/lib/atomic/accounts/collaborator_department.ex b/lib/atomic/accounts/collaborator_department.ex index c5c6ceaee..4a858fd38 100644 --- a/lib/atomic/accounts/collaborator_department.ex +++ b/lib/atomic/accounts/collaborator_department.ex @@ -5,12 +5,12 @@ defmodule Atomic.Users.CollaboratorDepartment do use Atomic.Schema alias Atomic.Departments.Department - alias Atomic.Accounts.Collaborator + alias Atomic.Accounts.User @required_fields ~w(collaborator_id department_id)a schema "collaborator_departments" do - belongs_to :collaborator, Collaborator + belongs_to :collaborator, User belongs_to :department, Department timestamps() diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index 2c90f8f4b..4531c686a 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -4,8 +4,8 @@ defmodule Atomic.Accounts.User do """ use Atomic.Schema + alias Atomic.Users.CollaboratorDepartment alias Atomic.Activities.Enrollment - alias Atomic.Accounts.Collaborator @roles ~w(admin staff student)a schema "users" do @@ -16,7 +16,7 @@ defmodule Atomic.Accounts.User do field :role, Ecto.Enum, values: @roles has_many :enrollments, Enrollment - has_one :collaborator, Collaborator, on_replace: :delete_if_exists, on_delete: :delete_all + many_to_many :collaborators, User, join_through: CollaboratorDepartment timestamps() end diff --git a/lib/atomic/departments/department.ex b/lib/atomic/departments/department.ex index eb6628d6c..64ad54e51 100644 --- a/lib/atomic/departments/department.ex +++ b/lib/atomic/departments/department.ex @@ -6,7 +6,7 @@ defmodule Atomic.Departments.Department do alias Atomic.Users.CollaboratorDepartment alias Atomic.Organizations.Organization alias Atomic.Activities.Activity - alias Atomic.Accounts.Collaborator + alias Atomic.Accounts.User alias Atomic.Activities.ActivityDepartment @required_fields ~w(name organization_id)a @@ -15,7 +15,7 @@ defmodule Atomic.Departments.Department do schema "departments" do field :name, :string - many_to_many :collaborators, Collaborator, join_through: CollaboratorDepartment + many_to_many :collaborators, User, join_through: CollaboratorDepartment many_to_many :activities, Activity, join_through: ActivityDepartment belongs_to :organization, Organization, on_replace: :delete_if_exists diff --git a/priv/repo/migrations/20230114142345_create_collaborators.exs b/priv/repo/migrations/20230114142345_create_collaborators.exs deleted file mode 100644 index 00b08679f..000000000 --- a/priv/repo/migrations/20230114142345_create_collaborators.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Atomic.Repo.Migrations.CreateCollaborators do - use Ecto.Migration - - def change do - create table(:collaborators, primary_key: false) do - add :id, :binary_id, primary_key: true - add :user_id, references(:users, on_delete: :nothing, type: :binary_id) - timestamps() - end - - create unique_index(:collaborators, [:user_id]) - end -end diff --git a/priv/repo/migrations/20230114142351_create_collaborator_departments.exs b/priv/repo/migrations/20230114142351_create_collaborator_departments.exs index 6aa7af582..91809529f 100644 --- a/priv/repo/migrations/20230114142351_create_collaborator_departments.exs +++ b/priv/repo/migrations/20230114142351_create_collaborator_departments.exs @@ -5,7 +5,7 @@ defmodule Atomic.Repo.Migrations.CreateCollaboratorDepartments do create table(:collaborator_departments, primary_key: false) do add :id, :binary_id, primary_key: true - add :collaborator_id, references(:collaborators, on_delete: :nothing, type: :binary_id) + add :collaborator_id, references(:users, on_delete: :nothing, type: :binary_id) add :department_id, references(:departments, on_delete: :nothing, type: :binary_id) timestamps() diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index 3545254d1..59e729391 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -28,16 +28,4 @@ defmodule Atomic.AccountsFixtures do [_, token | _] = String.split(captured_email.text_body, "[TOKEN]") token end - - @doc """ - Generate a collaborator. - """ - def collaborator_fixture(attrs \\ %{}) do - {:ok, collaborator} = - attrs - |> Enum.into(%{}) - |> Atomic.Accounts.create_collaborator() - - collaborator - end end From ec1f58d1018a4f0a9372eba803afd67997c87a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Tue, 7 Mar 2023 12:29:42 +0000 Subject: [PATCH 4/7] Remove tests because associated schema was removed --- test/atomic/accounts_test.exs | 56 ----------------------------------- 1 file changed, 56 deletions(-) diff --git a/test/atomic/accounts_test.exs b/test/atomic/accounts_test.exs index 81e51b70d..37fd45e56 100644 --- a/test/atomic/accounts_test.exs +++ b/test/atomic/accounts_test.exs @@ -505,60 +505,4 @@ defmodule Atomic.AccountsTest do refute inspect(%User{password: "123456"}) =~ "password: \"123456\"" end end - - describe "collaborators" do - alias Atomic.Accounts.Collaborator - - import Atomic.AccountsFixtures - - @invalid_attrs %{} - - test "list_collaborators/0 returns all collaborators" do - collaborator = collaborator_fixture() - assert Accounts.list_collaborators() == [collaborator] - end - - test "get_collaborator!/1 returns the collaborator with given id" do - collaborator = collaborator_fixture() - assert Accounts.get_collaborator!(collaborator.id) == collaborator - end - - test "create_collaborator/1 with valid data creates a collaborator" do - valid_attrs = %{} - - assert {:ok, %Collaborator{} = collaborator} = Accounts.create_collaborator(valid_attrs) - end - - test "create_collaborator/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Accounts.create_collaborator(@invalid_attrs) - end - - test "update_collaborator/2 with valid data updates the collaborator" do - collaborator = collaborator_fixture() - update_attrs = %{} - - assert {:ok, %Collaborator{} = collaborator} = - Accounts.update_collaborator(collaborator, update_attrs) - end - - test "update_collaborator/2 with invalid data returns error changeset" do - collaborator = collaborator_fixture() - - assert {:error, %Ecto.Changeset{}} = - Accounts.update_collaborator(collaborator, @invalid_attrs) - - assert collaborator == Accounts.get_collaborator!(collaborator.id) - end - - test "delete_collaborator/1 deletes the collaborator" do - collaborator = collaborator_fixture() - assert {:ok, %Collaborator{}} = Accounts.delete_collaborator(collaborator) - assert_raise Ecto.NoResultsError, fn -> Accounts.get_collaborator!(collaborator.id) end - end - - test "change_collaborator/1 returns a collaborator changeset" do - collaborator = collaborator_fixture() - assert %Ecto.Changeset{} = Accounts.change_collaborator(collaborator) - end - end end From 16e9e0e0946294c571909b2678fbfd0c43d48c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Wed, 8 Mar 2023 12:51:55 +0000 Subject: [PATCH 5/7] add small change --- lib/atomic/accounts/collaborator_department.ex | 1 - lib/atomic/accounts/user.ex | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/atomic/accounts/collaborator_department.ex b/lib/atomic/accounts/collaborator_department.ex index 4a858fd38..3cce29ed0 100644 --- a/lib/atomic/accounts/collaborator_department.ex +++ b/lib/atomic/accounts/collaborator_department.ex @@ -12,7 +12,6 @@ defmodule Atomic.Users.CollaboratorDepartment do schema "collaborator_departments" do belongs_to :collaborator, User belongs_to :department, Department - timestamps() end diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index 4531c686a..6fa5ac8cf 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -6,6 +6,7 @@ defmodule Atomic.Accounts.User do alias Atomic.Users.CollaboratorDepartment alias Atomic.Activities.Enrollment + alias Atomic.Accounts.User @roles ~w(admin staff student)a schema "users" do @@ -16,7 +17,7 @@ defmodule Atomic.Accounts.User do field :role, Ecto.Enum, values: @roles has_many :enrollments, Enrollment - many_to_many :collaborators, User, join_through: CollaboratorDepartment + many_to_many :collaborators, User, join_through: CollaboratorDepartment, on_replace: :delete timestamps() end From daaf856ce057aed6dc001c212bab9852f136e23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Wed, 31 May 2023 23:52:02 +0100 Subject: [PATCH 6/7] Some changes --- lib/atomic/accounts.ex | 1 + lib/atomic/accounts/collaborator_department.ex | 2 +- lib/atomic/accounts/user.ex | 2 +- lib/atomic/departments/department.ex | 2 +- priv/repo/seeds/accounts.exs | 3 ++- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 6ce244d5e..4cb1af4b2 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -74,6 +74,7 @@ defmodule Atomic.Accounts do {:error, %Ecto.Changeset{}} """ + def register_user(attrs) do %User{} |> User.registration_changeset(attrs) diff --git a/lib/atomic/accounts/collaborator_department.ex b/lib/atomic/accounts/collaborator_department.ex index 3cce29ed0..284de2011 100644 --- a/lib/atomic/accounts/collaborator_department.ex +++ b/lib/atomic/accounts/collaborator_department.ex @@ -1,4 +1,4 @@ -defmodule Atomic.Users.CollaboratorDepartment do +defmodule Atomic.Accounts.CollaboratorDepartment do @moduledoc """ An activity speaker """ diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index fb22c90ed..7cfd22751 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -4,7 +4,7 @@ defmodule Atomic.Accounts.User do """ use Atomic.Schema - alias Atomic.Users.CollaboratorDepartment + alias Atomic.Accounts.CollaboratorDepartment alias Atomic.Accounts.Course alias Atomic.Activities.Enrollment diff --git a/lib/atomic/departments/department.ex b/lib/atomic/departments/department.ex index 0778318ff..421a14900 100644 --- a/lib/atomic/departments/department.ex +++ b/lib/atomic/departments/department.ex @@ -3,7 +3,7 @@ defmodule Atomic.Departments.Department do A department of an organization """ use Atomic.Schema - alias Atomic.Users.CollaboratorDepartment + alias Atomic.Accounts.CollaboratorDepartment alias Atomic.Organizations.Organization alias Atomic.Accounts.User alias Atomic.Activities.{Activity, ActivityDepartment} diff --git a/priv/repo/seeds/accounts.exs b/priv/repo/seeds/accounts.exs index a73986b84..1c980ae6b 100644 --- a/priv/repo/seeds/accounts.exs +++ b/priv/repo/seeds/accounts.exs @@ -1,6 +1,7 @@ defmodule Atomic.Repo.Seeds.Accounts do alias Atomic.Accounts alias Atomic.Accounts.{Course, User} + alias Atomic.Accounts.User alias Atomic.Repo def run do @@ -163,7 +164,7 @@ defmodule Atomic.Repo.Seeds.Accounts do "course_id" => Enum.random(courses).id } - case Accounts.register_user(user) do + case insert_user(user) do {:error, changeset} -> Mix.shell().error(Kernel.inspect(changeset.errors)) From e946c90168daf83f7a2ab6df46636015110fbff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= <93675410+MarioRodrigues10@users.noreply.github.com> Date: Tue, 4 Jul 2023 08:58:58 +0100 Subject: [PATCH 7/7] Update module description Co-authored-by: Rui Lopes <76881129+RuiL1904@users.noreply.github.com> --- lib/atomic/accounts/collaborator_department.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/atomic/accounts/collaborator_department.ex b/lib/atomic/accounts/collaborator_department.ex index 284de2011..5acdb542a 100644 --- a/lib/atomic/accounts/collaborator_department.ex +++ b/lib/atomic/accounts/collaborator_department.ex @@ -1,6 +1,6 @@ defmodule Atomic.Accounts.CollaboratorDepartment do @moduledoc """ - An activity speaker + A relation representing a collaborator who belongs to a department """ use Atomic.Schema