EMMVMNWL2OKZ66L2IK4TKQV3YMC3DU4UO4HDK4NOTEMDNSXB5WEAC
end
end
describe "orgs" do
alias Chklst.Accounts.Org
import Chklst.AccountsFixtures
@invalid_attrs %{name: nil, status: nil}
test "list_orgs/0 returns all orgs" do
org = org_fixture()
assert Accounts.list_orgs() == [org]
end
test "get_org!/1 returns the org with given id" do
org = org_fixture()
assert Accounts.get_org!(org.id) == org
end
test "create_org/1 with valid data creates a org" do
valid_attrs = %{name: "some name", status: :active}
assert {:ok, %Org{} = org} = Accounts.create_org(valid_attrs)
assert org.name == "some name"
assert org.status == :active
end
test "create_org/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Accounts.create_org(@invalid_attrs)
end
test "update_org/2 with valid data updates the org" do
org = org_fixture()
update_attrs = %{name: "some updated name", status: :suspended}
assert {:ok, %Org{} = org} = Accounts.update_org(org, update_attrs)
assert org.name == "some updated name"
assert org.status == :suspended
end
test "update_org/2 with invalid data returns error changeset" do
org = org_fixture()
assert {:error, %Ecto.Changeset{}} = Accounts.update_org(org, @invalid_attrs)
assert org == Accounts.get_org!(org.id)
end
test "delete_org/1 deletes the org" do
org = org_fixture()
assert {:ok, %Org{}} = Accounts.delete_org(org)
assert_raise Ecto.NoResultsError, fn -> Accounts.get_org!(org.id) end
defmodule Chklst.Repo.Migrations.CreateUsersOrgs do
use Ecto.Migration
def change do
create table(:users_orgs, primary_key: false) do
add :user_id, references(:users, on_delete: :nothing, type: :binary_id)
add :org_id, references(:orgs, on_delete: :nothing, type: :binary_id)
timestamps(type: :utc_datetime)
end
create index(:users_orgs, [:user_id])
create index(:users_orgs, [:org_id])
create unique_index(:users_orgs, [:org_id, :user_id])
end
end
defmodule Chklst.Repo.Migrations.CreateOrgs do
use Ecto.Migration
def change do
create table(:orgs, primary_key: false) do
add :id, :binary_id, primary_key: true
add :name, :string
add :status, :string
timestamps(type: :utc_datetime)
end
end
end
@doc """
Gets a single org.
Raises `Ecto.NoResultsError` if the Org does not exist.
## Examples
iex> get_org!(123)
%Org{}
iex> get_org!(456)
** (Ecto.NoResultsError)
"""
def get_org!(id), do: Repo.get!(Org, id)
@doc """
Creates a org.
## Examples
iex> create_org(%{field: value})
{:ok, %Org{}}
iex> create_org(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_org(attrs \\ %{}) do
%Org{}
|> Org.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a org.
## Examples
iex> update_org(org, %{field: new_value})
{:ok, %Org{}}
iex> update_org(org, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_org(%Org{} = org, attrs) do
org
|> Org.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a org.
## Examples
iex> delete_org(org)
{:ok, %Org{}}
iex> delete_org(org)
{:error, %Ecto.Changeset{}}
"""
def delete_org(%Org{} = org) do
Repo.delete(org)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking org changes.
## Examples
iex> change_org(org)
%Ecto.Changeset{data: %Org{}}
"""
def change_org(%Org{} = org, attrs \\ %{}) do
Org.changeset(org, attrs)
end
defmodule Chklst.Accounts.UserOrg do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
@foreign_key_type :binary_id
schema "users_orgs" do
belongs_to :user, Chklst.Accounts.User
belongs_to :org, Chklst.Accounts.Org
timestamps(type: :utc_datetime)
end
@doc false
def changeset(user_org, attrs) do
user_org
|> cast(attrs, [])
|> validate_required([])
end
end
defmodule Chklst.Accounts.Org do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "orgs" do
field :name, :string
field :status, Ecto.Enum, values: [:active, :suspended, :closed]
many_to_many :users, Chklst.Accounts.User, join_through: Chklst.Accounts.UserOrg
timestamps(type: :utc_datetime)
end
@doc false
def changeset(org, attrs) do
org
|> cast(attrs, [:name, :status])
|> validate_required([:name, :status])
end
end