# Script for populating the database. You can run it as: # # mix run priv/repo/seeds.exs # # Inside the script, you can read and write to any of your # repositories directly: # # DaProductApp.Repo.insert!(%DaProductApp.SomeSchema{}) # # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. ######### App seeds (added by migration script) ######### alias DaProductApp.Repo alias DaProductApp.Accounts.{Role, Organization, User} # Idempotent role seeding (MySQL-safe: avoid on_conflict/conflict_target) roles = ["admin", "manager", "operator", "basic", "user", "member"] for r <- roles do case Repo.get_by(Role, name: r) do nil -> Repo.insert!(%Role{name: r}) _ -> :ok end end # Create additional organizations if not exists organizations = ["Default Org", "Mercury UPI PSP", "Partner Bank A", "Merchant Services Corp"] for org_name <- organizations do case Repo.get_by(Organization, name: org_name) do nil -> Repo.insert!(%Organization{name: org_name}) _ -> :ok end end # Get the main organization org = Repo.get_by(Organization, name: "Mercury UPI PSP") || Repo.get_by(Organization, name: "Default Org") # Create admin user if not exists admin_email = "admin@example.com" admin_role = Repo.get_by(Role, name: "admin") case Repo.get_by(User, email: admin_email) do nil -> # Create new admin user {:ok, _user} = %User{} |> User.changeset(%{ email: admin_email, name: "Admin", password: "changeme", role_id: admin_role && admin_role.id, organization_id: org.id }) |> Repo.insert() existing_user -> # Update existing admin user with proper password hash existing_user |> User.changeset(%{ password: "changeme", role_id: admin_role && admin_role.id, organization_id: org.id }) |> Repo.update() end