From ea975c32aea51e6d8aed271ef56d562f34047674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= <93675410+MarioRodrigues10@users.noreply.github.com> Date: Fri, 11 Aug 2023 21:40:50 +0100 Subject: [PATCH] Fix departments schema (#305) --- lib/atomic/departments.ex | 16 ++++++++++++++++ lib/atomic/organizations/department.ex | 4 ++-- lib/atomic_web/live/department_live/show.ex | 4 +++- .../live/department_live/show.html.heex | 8 ++++---- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/atomic/departments.ex b/lib/atomic/departments.ex index ca455f60f..9dfcb25b5 100644 --- a/lib/atomic/departments.ex +++ b/lib/atomic/departments.ex @@ -4,6 +4,7 @@ defmodule Atomic.Departments do """ use Atomic.Context + alias Atomic.Activities.SessionDepartment alias Atomic.Organizations.Collaborator alias Atomic.Organizations.Department @@ -50,6 +51,21 @@ defmodule Atomic.Departments do Repo.all(from d in Department, where: d.id in ^ids) end + @doc """ + Returns the list of activity sessions that belong to a department. + + ## Examples + + iex> get_department_sessions(99d7c9e5-4212-4f59-a097-28aaa33c2621) + [%Session{}, ...] + + """ + def get_department_sessions(department_id) do + Repo.all(from s in SessionDepartment, where: s.department_id == ^department_id) + |> Repo.preload([:session, session: :activity]) + |> Enum.map(& &1.session) + end + @doc """ Gets a single department. diff --git a/lib/atomic/organizations/department.ex b/lib/atomic/organizations/department.ex index 7df052b66..568eceb4b 100644 --- a/lib/atomic/organizations/department.ex +++ b/lib/atomic/organizations/department.ex @@ -4,7 +4,7 @@ defmodule Atomic.Organizations.Department do """ use Atomic.Schema alias Atomic.Organizations.Organization - alias Atomic.Activities.{Activity, SessionDepartment} + alias Atomic.Activities.{Session, SessionDepartment} @required_fields ~w(name organization_id)a @@ -16,7 +16,7 @@ defmodule Atomic.Organizations.Department do field :name, :string field :description, :string - many_to_many :activities, Activity, join_through: SessionDepartment, on_replace: :delete + many_to_many :sessions, Session, join_through: SessionDepartment, on_replace: :delete belongs_to :organization, Organization, on_replace: :delete_if_exists timestamps() diff --git a/lib/atomic_web/live/department_live/show.ex b/lib/atomic_web/live/department_live/show.ex index 2ecef19a1..cb7ff485e 100644 --- a/lib/atomic_web/live/department_live/show.ex +++ b/lib/atomic_web/live/department_live/show.ex @@ -11,7 +11,8 @@ defmodule AtomicWeb.DepartmentLive.Show do @impl true def handle_params(%{"organization_id" => organization_id, "id" => id}, _, socket) do - department = Departments.get_department!(id, preloads: [:activities]) + department = Departments.get_department!(id) + sessions = Departments.get_department_sessions(department.id) collaborator = Departments.get_collaborator!(socket.assigns.current_user.id, department.id) entries = [ @@ -32,6 +33,7 @@ defmodule AtomicWeb.DepartmentLive.Show do |> assign(:breadcrumb_entries, entries) |> assign(:page_title, page_title(socket.assigns.live_action, department.name)) |> assign(:department, department) + |> assign(:sessions, sessions) |> assign(:collaborator, collaborator) |> assign( :collaborators, diff --git a/lib/atomic_web/live/department_live/show.html.heex b/lib/atomic_web/live/department_live/show.html.heex index 6ed7d7225..7fb2acbef 100644 --- a/lib/atomic_web/live/department_live/show.html.heex +++ b/lib/atomic_web/live/department_live/show.html.heex @@ -57,15 +57,15 @@