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

This commit is contained in:
Esdras Eduardo 2023-07-29 01:48:31 +10:00 committed by GitHub
parent 1c75867572
commit d51eb82461
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 0 deletions

91
dev/repo/seeds.exs Normal file
View file

@ -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
[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);

View file

@ -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",