Skip to content

Commit 4d7ed78

Browse files
committed
group_people working! thanks @SimonLab #220
1 parent 1da1ce2 commit 4d7ed78

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

lib/auth/group.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ defmodule Auth.Group do
2929
|> put_assoc(:app, Auth.App.get_app!(attrs.app_id))
3030
|> Repo.insert()
3131
end
32+
33+
def get_group_by_id(id) do
34+
__MODULE__
35+
|> Repo.get_by(id: id)
36+
end
3237
end

lib/auth/group_people.ex

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@ defmodule Auth.GroupPeople do
22
use Ecto.Schema
33
import Ecto.Changeset
44
import Ecto.Query
5-
alias Auth.{Repo}
5+
alias Auth.{Group, Person, Repo, Role}
66
alias __MODULE__
77

88
schema "group_people" do
9-
field :granter_id, :id
10-
field :group_id, :id
11-
field :person_id, :id
12-
field :role_id, :id
9+
field :granter_id, :integer
10+
belongs_to :group, Group
11+
belongs_to :person, Person
12+
belongs_to :role, Role
1313
# revoking only relevant when removing a person from a group
1414
field :revoker_id, :id
1515
field :revoked, :utc_datetime
1616

1717
timestamps()
1818
end
1919

20-
def changeset(group_people, attrs) do
21-
group_people
20+
def changeset(attrs) do
21+
%GroupPeople{}
2222
|> cast(attrs, [:granter_id, :group_id, :person_id, :role_id, :revoker_id, :revoked])
2323
|> validate_required([:group_id, :person_id])
24+
# |> foreign_key_constraint(:person_id)
25+
2426
end
2527

2628
@doc """
2729
Creates a `group_people` record (i.e. `people` that belong to a `group`).
2830
"""
2931
def create(attrs) do
30-
%GroupPeople{}
31-
|> changeset(attrs)
32-
|> put_assoc(:granter_id, Auth.Person.get_person_by_id(attrs.granter_id))
33-
# |> put_assoc(:group_id, Auth.Group.(attrs.grantee_id))
34-
|> put_assoc(:person_id, Auth.Person.get_person_by_id(attrs.person_id))
35-
|> put_assoc(:role_id, Auth.Role.get_role!(attrs.role_id))
32+
33+
changeset(attrs)
34+
|> put_assoc(:group, Group.get_group_by_id(attrs.group_id))
35+
|> put_assoc(:person, Person.get_person_by_id(attrs.person_id))
36+
|> put_assoc(:role, Role.get_role!(attrs.role_id))
3637
|> Repo.insert()
3738
end
3839

lib/auth_web/live/groups_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule AuthWeb.GroupsLive do
22
use AuthWeb, :live_view
3-
alias Phoenix.Socket.Broadcast
3+
# alias Phoenix.Socket.Broadcast
44
# run live auth on mount:
55
on_mount AuthWeb.LiveAuthController
66

test/auth/group_people_test.exs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ defmodule Auth.GroupPeopleTest do
77
app = Auth.App.get_app!(1)
88
admin = Auth.Person.get_person_by_id(1)
99

10-
# Create a random non-admin person we can add to the group:
11-
alex = %{email: "[email protected]", givenName: "Alex",
12-
auth_provider: "email", app_id: app.id}
13-
grantee = Auth.Person.create_person(alex)
14-
assert grantee.id > 1
15-
1610
# Create group
1711
group = %{
1812
desc: "Group with people",
@@ -24,17 +18,23 @@ defmodule Auth.GroupPeopleTest do
2418
assert inserted_group.name == group.name
2519
assert inserted_group.app_id == app.id
2620

21+
# Create a random non-admin person we can add to the group:
22+
alex = %{email: "[email protected]", givenName: "Alex",
23+
auth_provider: "email", app_id: app.id}
24+
non_admin = Auth.Person.create_person(alex)
25+
assert non_admin.id > 1
26+
2727
group_person = %{
2828
granter_id: admin.id,
2929
group_id: inserted_group.id,
30-
person_id: grantee.id,
30+
person_id: non_admin.id,
3131
role_id: 4
3232
}
3333

3434
# Insert the GroupPerson Record
35-
{:ok, inserted_group_person} = Auth.GroupPeople.create(group_person)
36-
assert inserted_group_person.group_id == inserted_group.id
37-
assert inserted_group_person.person_id == grantee.id
35+
{:ok, gp} = Auth.GroupPeople.create(group_person)
36+
assert gp.group_id == inserted_group.id
37+
assert gp.person_id == non_admin.id
3838

3939
# Insert the GroupPerson Admin
4040
group_person_admin = %{

0 commit comments

Comments
 (0)