From eda264081bcea587f2a22f9304e3286abd53a905 Mon Sep 17 00:00:00 2001 From: Esdras Eduardo Date: Thu, 27 Jul 2023 14:49:01 +0800 Subject: [PATCH] feat: Add seed with Admin, User, Customer, Organization and Ticket --- dev/repo/seeds.exs | 91 ++++++++++++++++++++++++++++++++++++++++++++++ mix.exs | 1 + 2 files changed, 92 insertions(+) create mode 100644 dev/repo/seeds.exs diff --git a/dev/repo/seeds.exs b/dev/repo/seeds.exs new file mode 100644 index 0000000..e9927b3 --- /dev/null +++ b/dev/repo/seeds.exs @@ -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); diff --git a/mix.exs b/mix.exs index 46e303d..7946c19 100644 --- a/mix.exs +++ b/mix.exs @@ -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",