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

Add collaborators #264

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions lib/atomic/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ defmodule Atomic.Accounts do
{:error, %Ecto.Changeset{}}

"""

def register_user(attrs) do
%User{}
|> User.registration_changeset(attrs)
Expand Down
29 changes: 29 additions & 0 deletions lib/atomic/accounts/collaborator_department.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Atomic.Accounts.CollaboratorDepartment do
@moduledoc """
An activity speaker
MarioRodrigues10 marked this conversation as resolved.
Show resolved Hide resolved
"""
use Atomic.Schema

alias Atomic.Departments.Department
alias Atomic.Accounts.User

@required_fields ~w(collaborator_id department_id)a

schema "collaborator_departments" do
Copy link
Member

Choose a reason for hiding this comment

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

Should we not have a role to identify this relation, e.g.: 'President', 'Vice-President', etc?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about creating another schema "board_users" where i give those roles to an user

Copy link
Member Author

@MarioRodrigues10 MarioRodrigues10 Mar 7, 2023

Choose a reason for hiding this comment

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

Inside this schema i can create role field and give it to user , what you think?

belongs_to :collaborator, User
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
belongs_to :collaborator, User
belongs_to :user, User

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
4 changes: 3 additions & 1 deletion lib/atomic/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Atomic.Accounts.User do
"""
use Atomic.Schema

alias Atomic.Accounts.CollaboratorDepartment

alias Atomic.Accounts.Course
alias Atomic.Activities.Enrollment
alias Atomic.Organizations.{Membership, Organization}
Expand All @@ -24,8 +26,8 @@ defmodule Atomic.Accounts.User do
belongs_to :course, Course
field :profile_picture, ProfilePicture.Type
field :role, Ecto.Enum, values: @roles

has_many :enrollments, Enrollment
many_to_many :collaborators, User, join_through: CollaboratorDepartment, on_replace: :delete

many_to_many :organizations, Organization, join_through: Membership

Expand Down
4 changes: 3 additions & 1 deletion lib/atomic/departments/department.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule Atomic.Departments.Department do
A department of an organization
"""
use Atomic.Schema
alias Atomic.Accounts.CollaboratorDepartment
alias Atomic.Organizations.Organization
alias Atomic.Accounts.User
alias Atomic.Activities.{Activity, ActivityDepartment}

@required_fields ~w(name organization_id)a
Expand All @@ -12,7 +14,7 @@ defmodule Atomic.Departments.Department do

schema "departments" do
field :name, :string

many_to_many :collaborators, User, join_through: CollaboratorDepartment
many_to_many :activities, Activity, join_through: ActivityDepartment, on_replace: :delete

belongs_to :organization, Organization, on_replace: :delete_if_exists
Expand Down
Original file line number Diff line number Diff line change
@@ -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(:users, on_delete: :nothing, type: :binary_id)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
add :collaborator_id, references(:users, on_delete: :nothing, type: :binary_id)
add :user_id, references(:users, on_delete: :nothing, type: :binary_id)

add :department_id, references(:departments, on_delete: :nothing, type: :binary_id)

timestamps()
end
end
end
3 changes: 2 additions & 1 deletion priv/repo/seeds/accounts.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))

Expand Down