diff --git a/priv/repo/seeds/activities.exs b/priv/repo/seeds/activities.exs index 02d139b36..8d4efec4a 100644 --- a/priv/repo/seeds/activities.exs +++ b/priv/repo/seeds/activities.exs @@ -7,8 +7,7 @@ defmodule Atomic.Repo.Seeds.Activities do alias Atomic.Activities alias Atomic.Activities.SessionDepartment alias Atomic.Activities.Session - alias Atomic.Organizations.Organization - alias Atomic.Activities.{Activity, Enrollment, Location} + alias Atomic.Activities.{Activity, Enrollment} def run do seed_activities() @@ -17,13 +16,13 @@ defmodule Atomic.Repo.Seeds.Activities do end def seed_activities() do - location = %{ - name: "Departamento de Informática da Universidade do Minho", - url: "https://web.di.uminho.pt" - } - case Repo.all(Activity) do [] -> + location = %{ + name: "Departamento de Informática da Universidade do Minho", + url: "https://web.di.uminho.pt" + } + Activity.changeset( %Activity{}, %{ @@ -191,11 +190,12 @@ defmodule Atomic.Repo.Seeds.Activities do end def seed_activities_departments() do - department = Repo.get_by(Department, name: "Merchandise and Partnerships") - case Repo.all(SessionDepartment) do [] -> - for activity <- Repo.all(Activity) do + department = Repo.get_by(Department, name: "Merchandise and Partnerships") + activities = Repo.all(Activity) + + for activity <- activities do %SessionDepartment{} |> SessionDepartment.changeset(%{ activity_id: activity.id, @@ -203,15 +203,19 @@ defmodule Atomic.Repo.Seeds.Activities do }) |> Repo.insert!() end + + _ -> + Mix.shell().error("Found session departments, aborting seeding session departments.") end end def seed_enrollments() do - sessions = Repo.all(Session) - case Repo.all(Enrollment) do [] -> - for user <- Repo.all(User) do + users = Repo.all(User) + sessions = Repo.all(Session) + + for user <- users do for _ <- 1..Enum.random(1..2) do Activities.create_enrollment( Enum.random(sessions).id, @@ -219,6 +223,9 @@ defmodule Atomic.Repo.Seeds.Activities do ) end end + + _ -> + Mix.shell().error("Found enrollments, aborting seeding enrollments.") end end @@ -226,8 +233,9 @@ defmodule Atomic.Repo.Seeds.Activities do case Repo.all(SessionDepartment) do [] -> department = Repo.get_by(Department, name: "CAOS") + sessions = Repo.all(Session) - for session <- Repo.all(Session) do + for session <- sessions do %SessionDepartment{} |> SessionDepartment.changeset(%{ session_id: session.id, @@ -235,6 +243,9 @@ defmodule Atomic.Repo.Seeds.Activities do }) |> Repo.insert!() end + + _ -> + Mix.shell().error("Found session departments, aborting seeding session departments.") end end end diff --git a/priv/repo/seeds/memberships.exs b/priv/repo/seeds/memberships.exs index 99ca8dd26..f5322649f 100644 --- a/priv/repo/seeds/memberships.exs +++ b/priv/repo/seeds/memberships.exs @@ -2,7 +2,6 @@ defmodule Atomic.Repo.Seeds.Memberships do alias Atomic.Organizations.Organization alias Atomic.Organizations.Membership alias Atomic.Accounts.User - alias Atomic.Organizations alias Atomic.Organizations.Board alias Atomic.Organizations.BoardDepartments alias Atomic.Organizations.UserOrganization @@ -16,74 +15,107 @@ defmodule Atomic.Repo.Seeds.Memberships do end def seed_memberships() do - users = Repo.all(User) - organizations = Repo.all(Organization) - - for user <- users do - for organization <- organizations do - prob = 50 - random_number = :rand.uniform(100) - - if random_number < prob do - %Membership{} - |> Membership.changeset(%{ - "user_id" => user.id, - "organization_id" => organization.id, - "created_by_id" => Enum.random(users).id, - "role" => Enum.random([:follower, :member, :admin, :owner]) - }) - |> Repo.insert!() + case Repo.all(Membership) do + [] -> + users = Repo.all(User) + organizations = Repo.all(Organization) + + for user <- users do + for organization <- organizations do + prob = 50 + random_number = :rand.uniform(100) + + if random_number < prob do + %Membership{} + |> Membership.changeset(%{ + "user_id" => user.id, + "organization_id" => organization.id, + "created_by_id" => Enum.random(users).id, + "role" => Enum.random([:follower, :member, :admin, :owner]) + }) + |> Repo.insert!() + end + end end - end + + _ -> + Mix.shell().error("Found memberships, aborting seeding memberships.") end end def seed_board do - organizations = Repo.all(Organization) - - for organization <- organizations do - %Board{} - |> Board.changeset(%{ - "organization_id" => organization.id, - "year" => "2023/2024" - }) - |> Repo.insert!() + case Repo.all(Board) do + [] -> + organizations = Repo.all(Organization) + + for organization <- organizations do + %Board{} + |> Board.changeset(%{ + "organization_id" => organization.id, + "year" => "2023/2024" + }) + |> Repo.insert!() + end + + _ -> + Mix.shell().error("Found boards, aborting seeding boards.") end end def seed_board_departments do - departments = ["Conselho Fiscal", "Mesa AG", "CAOS", "DMC", "DMP", "Presidência", "Vogais"] - boards = Repo.all(Board) - - for board <- boards do - for i <- 0..6 do - %BoardDepartments{} - |> BoardDepartments.changeset(%{ - "board_id" => board.id, - "name" => Enum.at(departments, i), - "priority" => i - }) - |> Repo.insert!() - end + case Repo.all(BoardDepartments) do + [] -> + departments = [ + "Conselho Fiscal", + "Mesa AG", + "CAOS", + "DMC", + "DMP", + "Presidência", + "Vogais" + ] + + boards = Repo.all(Board) + + for board <- boards do + for i <- 0..6 do + %BoardDepartments{} + |> BoardDepartments.changeset(%{ + "board_id" => board.id, + "name" => Enum.at(departments, i), + "priority" => i + }) + |> Repo.insert!() + end + end + + _ -> + Mix.shell().error("Found board departments, aborting seeding board departments.") end end def seed_user_organizations do - titles = ["Presidente", "Vice-Presidente", "Secretário", "Tesoureiro", "Vogal"] - board_departments = Repo.all(BoardDepartments) - users = Repo.all(User) - - for board_department <- board_departments do - for i <- 0..4 do - %UserOrganization{} - |> UserOrganization.changeset(%{ - "user_id" => Enum.random(users).id, - "board_departments_id" => board_department.id, - "role" => Enum.at(titles, i), - "priority" => i - }) - |> Repo.insert!() - end + case Repo.all(UserOrganization) do + [] -> + titles = ["Presidente", "Vice-Presidente", "Secretário", "Tesoureiro", "Vogal"] + users = Repo.all(User) + board_departments = Repo.all(BoardDepartments) + + for board_department <- board_departments do + for i <- 0..4 do + %UserOrganization{} + |> UserOrganization.changeset(%{ + "user_id" => Enum.random(users).id, + "board_departments_id" => board_department.id, + "role" => Enum.at(titles, i), + "priority" => i + }) + |> Repo.insert!() + end + end + + _ -> + Mix.shell().error("Found user organizations, aborting seeding user organizations.") end end end diff --git a/priv/repo/seeds/news.exs b/priv/repo/seeds/news.exs index d3c698e75..fb7aea64d 100644 --- a/priv/repo/seeds/news.exs +++ b/priv/repo/seeds/news.exs @@ -8,18 +8,24 @@ defmodule Atomic.Repo.Seeds.News do end def seed_news() do - organizations = Repo.all(Organization) + case Repo.all(New) do + [] -> + organizations = Repo.all(Organization) - for organization <- organizations do - for i <- 1..10 do - %New{} - |> New.changeset(%{ - title: "News title #{organization.name} #{i}", - description: "News description #{organization.name} #{i}", - organization_id: organization.id - }) - |> Repo.insert!() - end + for organization <- organizations do + for i <- 1..10 do + %New{} + |> New.changeset(%{ + title: "News title #{organization.name} #{i}", + description: "News description #{organization.name} #{i}", + organization_id: organization.id + }) + |> Repo.insert!() + end + end + + _ -> + Mix.shell().error("Found news, aborting seeding news.") end end end diff --git a/priv/repo/seeds/organizations.exs b/priv/repo/seeds/organizations.exs index 26441ab81..da50b6bb9 100644 --- a/priv/repo/seeds/organizations.exs +++ b/priv/repo/seeds/organizations.exs @@ -1,11 +1,7 @@ defmodule Atomic.Repo.Seeds.Organizations do alias Atomic.Repo - alias Atomic.Accounts.User - alias Atomic.Organizations.Card - alias Atomic.Organizations.Membership alias Atomic.Organizations.Organization - alias Atomic.Organizations def run do seed_organizations() @@ -104,7 +100,7 @@ defmodule Atomic.Repo.Seeds.Organizations do |> Repo.insert!() _ -> - :ok + Mix.shell().error("Found organizations, aborting seeding organizations.") end end end diff --git a/priv/repo/seeds/partners.exs b/priv/repo/seeds/partners.exs index 538617635..44348b869 100644 --- a/priv/repo/seeds/partners.exs +++ b/priv/repo/seeds/partners.exs @@ -8,18 +8,24 @@ defmodule Atomic.Repo.Seeds.Partners do end def seed_partners() do - organizations = Repo.all(Organization) + case Repo.all(Partner) do + [] -> + organizations = Repo.all(Organization) - for organization <- organizations do - for i <- 1..10 do - %Partner{} - |> Partner.changeset(%{ - name: Faker.Company.name(), - description: Faker.Company.catch_phrase(), - organization_id: organization.id - }) - |> Repo.insert!() - end + for organization <- organizations do + for _ <- 1..10 do + %Partner{} + |> Partner.changeset(%{ + name: Faker.Company.name(), + description: Faker.Company.catch_phrase(), + organization_id: organization.id + }) + |> Repo.insert!() + end + end + + _ -> + Mix.shell().error("Found partners, aborting seeding partners.") end end end diff --git a/test/support/factories/activities_factory.ex b/test/support/factories/activities_factory.ex index 02901c129..7a8e6a9c6 100644 --- a/test/support/factories/activities_factory.ex +++ b/test/support/factories/activities_factory.ex @@ -25,8 +25,8 @@ defmodule Atomic.Factories.ActivityFactory do def session_factory do %Session{ - minimum_entries: 5, - maximum_entries: 50, + minimum_entries: Enum.random(1..10), + maximum_entries: Enum.random(11..20), start: NaiveDateTime.utc_now(), finish: NaiveDateTime.utc_now() |> NaiveDateTime.add(1, :hour) }