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

feat: Add seed with Admin, User, Customer, Organization and Ticket #56

Merged
merged 1 commit into from
Jul 28, 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
91 changes: 91 additions & 0 deletions dev/repo/seeds.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
defmodule Demo.Seeder do
require Ash.Query
alias Ash.Seed
alias Demo.Accounts.User
alias Demo.Tickets.{Customer, Organization, Representative, Ticket}

@spec insert_admin!(String.t(), String.t()) :: User.t()
def insert_admin!(first_name, last_name) do
Seed.seed!(User, %{
first_name: first_name,
last_name: last_name,
admin: true
})
end

@spec insert_user!(String.t(), String.t(), String.t(), String.t()) :: User.t()
def insert_user!(first_name, last_name, bio, history) do
Seed.seed!(User, %{
first_name: first_name,
last_name: last_name,
profile: %{
bio: bio,
history: history,
},
alternate_profiles: []
})
end

@spec insert_customer!(String.t(), String.t()) :: Customer.t()
def insert_customer!(first_name, last_name) do
Seed.seed!(Customer, %{
first_name: first_name,
last_name: last_name,
representative: false
})
end

@spec insert_representative!(String.t(), String.t()) :: Representative.t()
def insert_representative!(first_name, last_name) do
Seed.seed!(Representative, %{
first_name: first_name,
last_name: last_name,
representative: true
})
end

@spec insert_organization!(String.t()) :: Organization.t()
def insert_organization!(name) do
Seed.seed!(Organization, %{name: name});
end

@spec insert_ticket!(String.t(), String.t(), Customer.t(), Representative.t(), Organization.t()) :: Ticket.t()
def insert_ticket!(subject, description, reporter, representative, organization) do
Seed.seed!(Ticket, %{
subject: subject,
description: description,
reporter_id: reporter.id,
representative_id: representative.id,
organization_id: organization.id,
comments: []
})
end

def arguments("--truncate") do
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this hooked up? I don't see what calls it. If we were to use Ash actions we could actually support upsert?: true which could potentially prevent the need to truncate at all. We could also add Ash.Seed.upsert

[Ticket, Organization, User]
|> Enum.map(&truncate/1)
end

defp truncate(resource) do
resource
|> Ash.Query.new()
|> Ash.Query.data_layer_query()
|> case do
{:ok, query} -> Demo.Repo.delete_all(query)
end
end

end

Supervisor.start_link([Demo.Repo], strategy: :one_for_one)

System.argv()
|> Enum.each(&Demo.Seeder.arguments/1)

Demo.Seeder.insert_admin!("Super", "Admin");
org = Demo.Seeder.insert_organization!("Ash Project");
Demo.Seeder.insert_user!("Alice", "Courtney", "Lorem ipsum dolor sit amet", "Duis aute irure dolor in reprehenderit in voluptate velit esse");
bob = Demo.Seeder.insert_customer!("Bob", "Maclean");
carol = Demo.Seeder.insert_representative!("Carol", "White");

Demo.Seeder.insert_ticket!("Lorem ipsum", "Duis aute irure dolor in reprehenderit in voluptate", bob, carol, org);
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ defmodule AshAdmin.MixProject do
credo: "credo --strict",
migrate: "ash_postgres.migrate --migrations-path dev/repo/migrations",
migrate_tenants: "ash_postgres.migrate --migrations-path dev/repo/tenant_migrations",
seed: "run dev/repo/seeds.exs --truncate",
setup: ["deps.get", "cmd npm install --prefix assets"],
dev: "run --no-halt dev.exs --config config",
sobelow: "sobelow --ignore XSS.Raw",
Expand Down
Loading