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

fix: seeds #312

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions priv/repo/seeds/activities.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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{},
%{
Expand Down Expand Up @@ -191,50 +190,62 @@ 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,
department_id: department.id
})
|> 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,
user
)
end
end

_ ->
Mix.shell().error("Found enrollments, aborting seeding enrollments.")
end
end

def seed_session_departments() 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,
department_id: department.id
})
|> Repo.insert!()
end

_ ->
Mix.shell().error("Found session departments, aborting seeding session departments.")
end
end
end
Expand Down
144 changes: 88 additions & 56 deletions priv/repo/seeds/memberships.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 17 additions & 11 deletions priv/repo/seeds/news.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions priv/repo/seeds/organizations.exs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -104,7 +100,7 @@ defmodule Atomic.Repo.Seeds.Organizations do
|> Repo.insert!()

_ ->
:ok
Mix.shell().error("Found organizations, aborting seeding organizations.")
end
end
end
Expand Down
28 changes: 17 additions & 11 deletions priv/repo/seeds/partners.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/support/factories/activities_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down