Skip to content

Commit

Permalink
Rebase with recent updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Aug 1, 2023
2 parents 7ce4cf3 + 8e5780c commit b22ab05
Show file tree
Hide file tree
Showing 75 changed files with 961 additions and 385 deletions.
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ config :atomic, AtomicWeb.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4000],
check_origin: false,
code_reloader: true,
debug_errors: true,
debug_errors: false,
secret_key_base: "YGqNjmRjNv4pslzy4DB3Roi6xeyvS2/v/YXGn62mlMqCqjWsmu2UKbtNuNaYx5OO",
watchers: [
# Start the esbuild watcher by calling Esbuild.install_and_run(:default, args)
Expand Down
9 changes: 9 additions & 0 deletions lib/atomic/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ defmodule Atomic.Accounts do
"""
def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token)

Repo.one(query)
|> Repo.preload(:organizations)
end

@doc """
Expand Down Expand Up @@ -453,4 +455,11 @@ defmodule Atomic.Accounts do
|> Course.changeset(attrs)
|> Repo.insert()
end

@doc """
Gets the user's organizations
"""
def get_user_organizations(user) do
Repo.all(Ecto.assoc(user, :organizations))
end
end
3 changes: 2 additions & 1 deletion lib/atomic/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Atomic.Accounts.User do
alias Atomic.Uploaders.ProfilePicture

@required_fields ~w(email password role name)a
@optional_fields ~w(course_id)a
@optional_fields ~w(course_id default_organization_id)a

@roles ~w(admin student)a

Expand All @@ -20,6 +20,7 @@ defmodule Atomic.Accounts.User do
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
belongs_to :default_organization, Organization

belongs_to :course, Course
field :profile_picture, ProfilePicture.Type
Expand Down
28 changes: 28 additions & 0 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ defmodule Atomic.Activities do
|> Repo.all()
end

def list_activities_by_organization_id(organization_id, opts) when is_list(opts) do
Activity
|> apply_filters(opts)
|> join(:inner, [a], d in assoc(a, :departments))
|> where([a, d], d.organization_id == ^organization_id)
|> select([a, _d], a)
|> Repo.all()
end

@doc """
Gets a single activity.
Expand All @@ -41,6 +50,12 @@ defmodule Atomic.Activities do
|> Repo.preload(preloads)
end

def get_activity_organizations!(activity, _preloads \\ []) do
departments = Map.get(activity, :departments, [])

Enum.map(departments, & &1.organization_id)
end

alias Atomic.Activities.Enrollment

def is_participating?(activity_id, user_id) do
Expand Down Expand Up @@ -402,6 +417,19 @@ defmodule Atomic.Activities do
Repo.all(Speaker)
end

@doc """
Returns the list of speakers belonging to an organization.
## Examples
iex> list_speakers_by_organization_id(99d7c9e5-4212-4f59-a097-28aaa33c2621)
[%Speaker{}, ...]
"""
def list_speakers_by_organization_id(id) do
Repo.all(from s in Speaker, where: s.organization_id == ^id)
end

def get_speakers(nil), do: []

def get_speakers(ids) do
Expand Down
11 changes: 9 additions & 2 deletions lib/atomic/activities/speaker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ defmodule Atomic.Activities.Speaker do
use Atomic.Schema
alias Atomic.Activities.Activity
alias Atomic.Activities.ActivitySpeaker
alias Atomic.Organizations.Organization

@required_fields ~w(name bio organization_id)a

schema "speakers" do
field :bio, :string
field :name, :string

many_to_many :activities, Activity, join_through: ActivitySpeaker, on_replace: :delete

belongs_to :organization, Organization, on_replace: :delete_if_exists

timestamps()
end

@doc false
def changeset(speaker, attrs) do
speaker
|> cast(attrs, [:name, :bio])
|> validate_required([:name, :bio])
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end
end
13 changes: 13 additions & 0 deletions lib/atomic/departments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ defmodule Atomic.Departments do
Repo.all(Department)
end

@doc """
Returns the list of departments belonging to an organization.
## Examples
iex> list_departments_by_organization_id(99d7c9e5-4212-4f59-a097-28aaa33c2621)
[%Department{}, ...]
"""
def list_departments_by_organization_id(id) do
Repo.all(from d in Department, where: d.organization_id == ^id)
end

def get_departments(nil), do: []

def get_departments(ids) do
Expand Down
43 changes: 33 additions & 10 deletions lib/atomic/organizations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule Atomic.Organizations do
The Organizations context.
"""

import Ecto.Query, warn: false

use Atomic.Context

alias Atomic.Accounts.User
Expand Down Expand Up @@ -41,8 +39,11 @@ defmodule Atomic.Organizations do
"""
def get_organization!(id, preloads \\ []) do
Repo.get!(Organization, id)
|> Repo.preload(preloads)
organization =
Repo.get!(Organization, id)
|> Repo.preload(preloads)

organization
end

@doc """
Expand Down Expand Up @@ -168,6 +169,18 @@ defmodule Atomic.Organizations do
|> Repo.exists?()
end

def get_role(user_id, organization_id) do
membership =
Membership
|> where([m], m.user_id == ^user_id and m.organization_id == ^organization_id)
|> Repo.one()

case membership do
nil -> nil
_ -> membership.role
end
end

@doc """
Gets a single membership.
Expand Down Expand Up @@ -247,12 +260,22 @@ defmodule Atomic.Organizations do
"""
def roles_less_than_or_equal(role) do
case role do
:follower -> []
:member -> [:member]
:admin -> [:member, :admin]
:owner -> [:member, :admin, :owner]
end
list = [:follower, :member, :admin, :owner]
Enum.drop_while(list, fn elem -> elem != role end)
end

@doc """
Returns all roles bigger or equal to the given role.
## Examples
iex> roles_bigger_than_or_equal(:member)
[:member, :admin, :owner]
"""
def roles_bigger_than_or_equal(role) do
list = [:follower, :member, :admin, :owner]
Enum.drop_while(list, fn elem -> elem != role end)
end

@doc """
Expand Down
13 changes: 13 additions & 0 deletions lib/atomic/partnerships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ defmodule Atomic.Partnerships do
Repo.all(Partner)
end

@doc """
Returns the list of partnerships belonging to an organization.
## Examples
iex> list_partnerships_by_organization_id(99d7c9e5-4212-4f59-a097-28aaa33c2621)
[%Partner{}, ...]
"""
def list_partnerships_by_organization_id(id) do
Repo.all(from p in Partner, where: p.organization_id == ^id)
end

@doc """
Gets a single partner.
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/partnerships/partner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Atomic.Partnerships.Partner do
alias Atomic.Organizations.Organization
alias Atomic.Uploaders

@required_fields ~w(name description)a
@required_fields ~w(name description organization_id)a

@optional_fields []

Expand Down
18 changes: 9 additions & 9 deletions lib/atomic_web/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ defmodule AtomicWeb.Config do
"""
alias AtomicWeb.Router.Helpers, as: Routes

def pages(conn) do
def pages(conn, current_organization) do
[
%{
key: :activities,
key: :home,
title: "Home",
icon: :home,
url: Routes.activity_index_path(conn, :index),
url: Routes.home_index_path(conn, :index),
tabs: []
},
%{
Expand All @@ -24,35 +24,35 @@ defmodule AtomicWeb.Config do
key: :departments,
title: "Departments",
icon: :cube,
url: Routes.department_index_path(conn, :index, :org),
url: Routes.department_index_path(conn, :index, current_organization.id),
tabs: []
},
%{
key: :instructors,
key: :activities,
title: "Activities",
icon: :academic_cap,
url: Routes.activity_index_path(conn, :index),
url: Routes.activity_index_path(conn, :index, current_organization.id),
tabs: []
},
%{
key: :partners,
title: "Partners",
icon: :user_group,
url: Routes.partner_index_path(conn, :index),
url: Routes.partner_index_path(conn, :index, current_organization.id),
tabs: []
},
%{
key: :memberships,
title: "Memberships",
icon: :user_add,
url: Routes.membership_index_path(conn, :index, :org),
url: Routes.membership_index_path(conn, :index, current_organization.id),
tabs: []
},
%{
key: :board,
title: "Board",
icon: :users,
url: Routes.board_index_path(conn, :index, :org),
url: Routes.board_index_path(conn, :index, current_organization.id),
tabs: []
},
%{
Expand Down
32 changes: 25 additions & 7 deletions lib/atomic_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule AtomicWeb.UserAuth do
import Phoenix.Controller

alias Atomic.Accounts
alias Atomic.Organizations
alias AtomicWeb.Router.Helpers, as: Routes

# Make the remember me cookie valid for 60 days.
Expand All @@ -31,12 +32,27 @@ defmodule AtomicWeb.UserAuth do
token = Accounts.generate_user_session_token(user)
user_return_to = get_session(conn, :user_return_to)

conn
|> renew_session()
|> put_session(:user_token, token)
|> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
|> maybe_write_remember_me_cookie(token, params)
|> redirect(to: user_return_to || signed_in_path(conn))
case user.default_organization_id do
nil ->
conn
|> renew_session()
|> put_session(:user_token, token)
|> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
|> maybe_write_remember_me_cookie(token, params)
|> redirect(to: user_return_to || signed_in_path(conn))

_ ->
conn
|> renew_session()
|> put_session(:user_token, token)
|> put_session(
:current_organization,
Organizations.get_organization!(user.default_organization_id)
)
|> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
|> maybe_write_remember_me_cookie(token, params)
|> redirect(to: user_return_to || signed_in_path(conn))
end
end

defp maybe_write_remember_me_cookie(conn, token, %{"remember_me" => "true"}) do
Expand Down Expand Up @@ -94,7 +110,9 @@ defmodule AtomicWeb.UserAuth do
def fetch_current_user(conn, _opts) do
{user_token, conn} = ensure_user_token(conn)
user = user_token && Accounts.get_user_by_session_token(user_token)
assign(conn, :current_user, user)

conn
|> assign(:current_user, user)
end

defp ensure_user_token(conn) do
Expand Down
4 changes: 4 additions & 0 deletions lib/atomic_web/exceptions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule AtomicWeb.MismatchError do
defexception message: "The provided parameters have no relation in the database.",
plug_status: 404
end
Loading

0 comments on commit b22ab05

Please sign in to comment.