Skip to content

Commit

Permalink
group_people working! thanks @SimonLab #220
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 3, 2022
1 parent 1da1ce2 commit 4d7ed78
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
5 changes: 5 additions & 0 deletions lib/auth/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ defmodule Auth.Group do
|> put_assoc(:app, Auth.App.get_app!(attrs.app_id))
|> Repo.insert()
end

def get_group_by_id(id) do
__MODULE__
|> Repo.get_by(id: id)
end
end
27 changes: 14 additions & 13 deletions lib/auth/group_people.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@ defmodule Auth.GroupPeople do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Auth.{Repo}
alias Auth.{Group, Person, Repo, Role}
alias __MODULE__

schema "group_people" do
field :granter_id, :id
field :group_id, :id
field :person_id, :id
field :role_id, :id
field :granter_id, :integer
belongs_to :group, Group
belongs_to :person, Person
belongs_to :role, Role
# revoking only relevant when removing a person from a group
field :revoker_id, :id
field :revoked, :utc_datetime

timestamps()
end

def changeset(group_people, attrs) do
group_people
def changeset(attrs) do
%GroupPeople{}
|> cast(attrs, [:granter_id, :group_id, :person_id, :role_id, :revoker_id, :revoked])
|> validate_required([:group_id, :person_id])
# |> foreign_key_constraint(:person_id)

end

@doc """
Creates a `group_people` record (i.e. `people` that belong to a `group`).
"""
def create(attrs) do
%GroupPeople{}
|> changeset(attrs)
|> put_assoc(:granter_id, Auth.Person.get_person_by_id(attrs.granter_id))
# |> put_assoc(:group_id, Auth.Group.(attrs.grantee_id))
|> put_assoc(:person_id, Auth.Person.get_person_by_id(attrs.person_id))
|> put_assoc(:role_id, Auth.Role.get_role!(attrs.role_id))

changeset(attrs)
|> put_assoc(:group, Group.get_group_by_id(attrs.group_id))
|> put_assoc(:person, Person.get_person_by_id(attrs.person_id))
|> put_assoc(:role, Role.get_role!(attrs.role_id))
|> Repo.insert()
end

Expand Down
2 changes: 1 addition & 1 deletion lib/auth_web/live/groups_live.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule AuthWeb.GroupsLive do
use AuthWeb, :live_view
alias Phoenix.Socket.Broadcast
# alias Phoenix.Socket.Broadcast
# run live auth on mount:
on_mount AuthWeb.LiveAuthController

Expand Down
20 changes: 10 additions & 10 deletions test/auth/group_people_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ defmodule Auth.GroupPeopleTest do
app = Auth.App.get_app!(1)
admin = Auth.Person.get_person_by_id(1)

# Create a random non-admin person we can add to the group:
alex = %{email: "alex_not_admin@gmail.com", givenName: "Alex",
auth_provider: "email", app_id: app.id}
grantee = Auth.Person.create_person(alex)
assert grantee.id > 1

# Create group
group = %{
desc: "Group with people",
Expand All @@ -24,17 +18,23 @@ defmodule Auth.GroupPeopleTest do
assert inserted_group.name == group.name
assert inserted_group.app_id == app.id

# Create a random non-admin person we can add to the group:
alex = %{email: "alex_not_admin@gmail.com", givenName: "Alex",
auth_provider: "email", app_id: app.id}
non_admin = Auth.Person.create_person(alex)
assert non_admin.id > 1

group_person = %{
granter_id: admin.id,
group_id: inserted_group.id,
person_id: grantee.id,
person_id: non_admin.id,
role_id: 4
}

# Insert the GroupPerson Record
{:ok, inserted_group_person} = Auth.GroupPeople.create(group_person)
assert inserted_group_person.group_id == inserted_group.id
assert inserted_group_person.person_id == grantee.id
{:ok, gp} = Auth.GroupPeople.create(group_person)
assert gp.group_id == inserted_group.id
assert gp.person_id == non_admin.id

# Insert the GroupPerson Admin
group_person_admin = %{
Expand Down

0 comments on commit 4d7ed78

Please sign in to comment.