#55 Can be assign a role to members of my Workgroup

Remove Owner on Workgroup (use role MASTER instead)
"find_citizen_by_id" not return user anymore, use "find_citizen_by_id_with_user" instead
This commit is contained in:
2020-06-01 13:44:25 +02:00
parent 8ff6fcc970
commit 7874f5cec4
49 changed files with 331 additions and 217 deletions

View File

@@ -1708,8 +1708,6 @@ components:
anonymous:
type: boolean
example: false
owner:
$ref: '#/components/schemas/CitizenResponse'
- $ref: '#/components/schemas/CreatedBy'
- $ref: '#/components/schemas/DeletedAt'
Workgroup:
@@ -1719,11 +1717,30 @@ components:
- type: object
properties:
members:
type: array
items:
$ref: '#/components/schemas/CitizenResponse'
$ref: '#/components/schemas/Members'
- $ref: '#/components/schemas/CreatedAt'
- $ref: '#/components/schemas/UpdatedAt'
Members:
description: members of workgroup
type: array
items:
$ref: '#/components/schemas/Member'
Member:
description: Member of workgroup
type: object
properties:
citizen:
$ref: '#/components/schemas/CitizenResponse'
roles:
type: array
items:
type: string
enum:
- MASTER
- MANAGER
- EDITOR
- REPORTER
example: MASTER

View File

@@ -2,27 +2,40 @@ do
$$
declare
citizen_count int = (select count(*) from citizen);
_roles text[] = $roles$
{
"MANAGER", "EDITOR", "REPORTER"
}
$roles$;
begin
delete from citizen_in_workgroup;
delete from workgroup;
insert into workgroup (id, created_by_id, name, description, anonymous, owner_id)
insert into workgroup (id, created_by_id, name, description, anonymous)
select
uuid_in(md5('workgroup'||rn::text)::cstring),
z.id,
'name' || rn,
'description' || rn,
rn % 3 = 1,
z.id
rn % 3 = 1
from (select *, row_number() over () rn from citizen) z;
insert into citizen_in_workgroup (citizen_id, workgroup_id)
insert into citizen_in_workgroup (citizen_id, workgroup_id, roles)
select
z.id,
w.id
w.id,
'{MASTER}'
from (select *, row_number() over ()+5 % citizen_count rn from citizen) z
join (select *, row_number() over () rn from workgroup) w using (rn);
insert into citizen_in_workgroup (citizen_id, workgroup_id, roles)
select
z.id,
w.id,
_roles[(row_number() over () % 3)+1:(row_number() over () % 3)+1]
from (select *, row_number() over ()+10 % citizen_count rn from citizen) z
join (select *, row_number() over () rn from workgroup) w using (rn);
raise notice 'workgroup fixtures done';
end;
$$;

View File

@@ -7,7 +7,7 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes,
count_opinion(a.id) as opinions

View File

@@ -21,7 +21,7 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes,
count_opinion(a.id) as opinions,

View File

@@ -14,7 +14,7 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes
from article as a

View File

@@ -7,7 +7,7 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes
into resource

View File

@@ -6,9 +6,7 @@ begin
select to_json(t) into resource
from (
select
z.*,
find_user_by_id(z.user_id) as "user",
array_agg(find_workgroup_by_id_simple(ciw.workgroup_id)) as "workgroups"
z.*
from citizen as z
left join citizen_in_workgroup ciw on z.id = ciw.citizen_id
where z.id = _id

View File

@@ -7,8 +7,7 @@ begin
from (
select
z.*,
find_user_by_id(z.user_id) as "user",
array_agg(find_workgroup_by_id_simple(ciw.workgroup_id)) as "workgroups"
find_user_by_id(z.user_id) as "user"
from citizen as z
left join citizen_in_workgroup ciw on z.id = ciw.citizen_id
where z.id = _id

View File

@@ -0,0 +1,22 @@
create or replace function find_citizen_by_id_with_user_and_workgroups(in id uuid, out resource json) language plpgsql as
$$
declare
_id alias for id;
begin
select to_json(t) into resource
from (
select
z.*,
find_user_by_id(z.user_id) as "user",
case when ciw.workgroup_id is null then '{}' else array_agg(json_build_object(
'roles', ciw.roles,
'workgroup', find_workgroup_by_id_simple(ciw.workgroup_id)
)) end as "workgroups"
from citizen as z
left join citizen_in_workgroup ciw on z.id = ciw.citizen_id
where z.id = _id
group by z.id, ciw.workgroup_id
) as t;
end;
$$;

View File

@@ -23,7 +23,7 @@ begin
email = excluded.email
returning id into new_id;
select find_citizen_by_id(new_id) into resource;
select find_citizen_by_id_with_user(new_id) into resource;
end;
$$;

View File

@@ -10,7 +10,7 @@ begin
select
com.*,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id(com.created_by_id) as created_by,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes
from "comment" as com
where id = _id

View File

@@ -21,7 +21,7 @@ begin
select
com.*,
find_reference_by_id(com.target_id, _reference) as target,
find_citizen_by_id(com.created_by_id) as created_by,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes
from "comment" as com

View File

@@ -14,7 +14,7 @@ begin
com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id(com.created_by_id) as created_by,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes
from "comment" as com
where parent_id = _parent_id

View File

@@ -15,7 +15,7 @@ begin
com.*,
(select count(c2) from "comment" c2 where c2.parent_comment_id = com.id) as children_count,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id(com.created_by_id) as created_by,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes
from "comment" as com
where com.parent_id = _target_id

View File

@@ -7,7 +7,7 @@ begin
from (
select
c.*,
find_citizen_by_id(c.created_by_id) as created_by,
find_citizen_by_id_with_user(c.created_by_id) as created_by,
find_constitution_titles_by_id(c.id) as titles
into resource
from constitution as c

View File

@@ -14,7 +14,7 @@ begin
from (
select
c.*,
find_citizen_by_id(c.created_by_id) as created_by,
find_citizen_by_id_with_user(c.created_by_id) as created_by,
find_constitution_titles_by_id(c.id) as titles,
zdb.score(c.ctid) _score
from constitution as c

View File

@@ -19,7 +19,7 @@ begin
select
f.*,
json_build_object('id', f.target_id, 'reference', f.target_reference) as target,
find_citizen_by_id(f.created_by_id) as created_by
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow as f
where f.created_by_id = _citizen_id
and array[f.target_id] <@ _target_ids
@@ -32,7 +32,7 @@ begin
select
f.*,
json_build_object('id', f.target_id, 'reference', f.target_reference) as target,
find_citizen_by_id(f.created_by_id) as created_by
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow as f
where f.created_by_id = _citizen_id
and f.target_id = _target_id

View File

@@ -13,7 +13,7 @@ begin
select
f.*,
find_article_by_id(f.target_id) as target,
find_citizen_by_id(f.created_by_id) as created_by
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow as f
where created_by_id = _created_by_id
order by created_at desc,

View File

@@ -13,7 +13,7 @@ begin
select
f.*,
json_build_object('id', f.target_id) as target,
find_citizen_by_id(f.created_by_id) as created_by
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow as f
where created_by_id = _created_by_id
order by created_at desc,

View File

@@ -13,7 +13,7 @@ begin
select
f.*,
find_constitution_by_id(f.target_id) as target,
find_citizen_by_id(f.created_by_id) as created_by
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow as f
where created_by_id = _created_by_id
order by created_at desc,

View File

@@ -19,7 +19,7 @@ begin
select
o.*,
find_reference_by_id(o.target_id, o.target_reference) as target,
find_citizen_by_id(o.created_by_id) as created_by,
find_citizen_by_id_with_user(o.created_by_id) as created_by,
to_json(ol) as choice
from opinion as o
join opinion_choice ol on o.choice_id = ol.id

View File

@@ -12,7 +12,7 @@ begin
select
o.*,
find_reference_by_id(o.target_id, o.target_reference) as target,
find_citizen_by_id(o.created_by_id) as created_by,
find_citizen_by_id_with_user(o.created_by_id) as created_by,
to_json(ol) as choice
from opinion as o
join opinion_choice ol on o.choice_id = ol.id

View File

@@ -10,7 +10,7 @@ begin
select
o.*,
find_reference_by_id(o.target_id, o.target_reference) as target,
find_citizen_by_id(o.created_by_id) as created_by,
find_citizen_by_id_with_user(o.created_by_id) as created_by,
to_json(ol) as choice
from opinion as o
join opinion_choice ol on o.choice_id = ol.id

View File

@@ -10,7 +10,7 @@ begin
select
o.*,
find_reference_by_id(o.target_id, o.target_reference) as target,
find_citizen_by_id(o.created_by_id) as created_by,
find_citizen_by_id_with_user(o.created_by_id) as created_by,
to_json(ol) as choice
from "opinion" as o
join opinion_choice ol on o.choice_id = ol.id

View File

@@ -13,7 +13,7 @@ begin
select
o.*,
find_reference_by_id(o.target_id, o.target_reference) as target,
find_citizen_by_id(o.created_by_id) as created_by,
find_citizen_by_id_with_user(o.created_by_id) as created_by,
to_json(ol) as choice
from "opinion" as o
join opinion_choice ol on o.choice_id = ol.id

View File

@@ -13,7 +13,7 @@ begin
select
v.*,
find_reference_by_id(v.target_id, v.target_reference) as target,
find_citizen_by_id(v.created_by_id) as created_by
find_citizen_by_id_with_user(v.created_by_id) as created_by
from vote as v

View File

@@ -21,7 +21,7 @@ begin
select
v.*,
find_reference_by_id(v.target_id, _reference) as target,
find_citizen_by_id(v.created_by_id) as created_by
find_citizen_by_id_with_user(v.created_by_id) as created_by
from vote as v

View File

@@ -0,0 +1,13 @@
create or replace function add_workgroup_member(in _id uuid, inout member json)
language plpgsql as
$$
begin
insert into citizen_in_workgroup (workgroup_id, citizen_id, roles)
values (
_id,
(member#>>'{citizen, id}')::uuid,
(select array_agg(t) from json_array_elements_text(member#>'{roles}') t)
)
on conflict do nothing;
end;
$$;

View File

@@ -1,16 +1,11 @@
create or replace function add_workgroup_members(in _id uuid, inout resource json)
create or replace function add_workgroup_members(in _id uuid, inout members json)
language plpgsql as
$$
begin
insert into citizen_in_workgroup (citizen_id, workgroup_id)
select
(z->>'id')::uuid,
_id::uuid
from json_array_elements(resource) z
where (z->>'id') is not null
on conflict do nothing;
perform add_workgroup_member(_id, b)
from json_array_elements(members) b;
select find_workgroup_members(_id) into resource;
select find_workgroup_members(_id) into members;
end;
$$;

View File

@@ -7,8 +7,7 @@ begin
from (
select
w.*,
find_citizen_by_id(w.created_by_id) as created_by,
find_citizen_by_id(w.owner_id) as owner,
find_citizen_by_id_with_user(w.created_by_id) as created_by,
find_workgroup_members(w.id) as members
into resource
from workgroup as w

View File

@@ -7,8 +7,7 @@ begin
from (
select
w.*,
json_build_object('id', w.created_by_id) as created_by,
json_build_object('id', w.owner_id) as owner
json_build_object('id', w.created_by_id) as created_by
into resource
from workgroup as w
join citizen_in_workgroup ciw on w.id = ciw.workgroup_id

View File

@@ -4,8 +4,9 @@ begin
select json_agg(t) into resource
from (
select
z.*,
find_user_by_id(z.user_id) as "user"
w.id as id,
find_citizen_by_id_with_user(z.id) as citizen,
ciw.roles as roles
from citizen_in_workgroup as ciw
join workgroup as w on ciw.workgroup_id = w.id
join citizen z on z.id = ciw.citizen_id

View File

@@ -21,8 +21,7 @@ begin
from (
select
w.*,
find_citizen_by_id(w.created_by_id) as created_by,
find_citizen_by_id(w.owner_id) as owner,
find_citizen_by_id_with_user(w.created_by_id) as created_by,
zdb.score(w.ctid) _score
from workgroup as w
where deleted_at is null

View File

@@ -1,4 +1,4 @@
create or replace function remove_workgroup_members(in _id uuid, inout resource json)
create or replace function remove_workgroup_members(in _id uuid, inout members json)
language plpgsql as
$$
begin
@@ -6,11 +6,11 @@ begin
where workgroup_id = _id
and citizen_id in (
select
(z->>'id')::uuid
from json_array_elements(resource) z
(b#>>'{citizen, id}')::uuid
from json_array_elements(members) b
);
select find_workgroup_members(_id) into resource;
select find_workgroup_members(_id) into members;
end
$$;

View File

@@ -1,25 +1,20 @@
create or replace function update_workgroup_members(in _id uuid, inout resource json)
create or replace function update_workgroup_members(in _id uuid, inout members json)
language plpgsql as
$$
begin
insert into citizen_in_workgroup (citizen_id, workgroup_id)
select
(z->>'id')::uuid,
_id::uuid
from json_array_elements(resource) z
where (z->>'id') is not null
on conflict do nothing;
perform add_workgroup_member(_id, b)
from json_array_elements(members) b;
delete from citizen_in_workgroup
where workgroup_id = _id
and citizen_id not in(
select
(z->>'id')::uuid
from json_array_elements(resource) z
where (z->>'id') is not null
(b#>>'{citizen, id}')::uuid
from json_array_elements(members) b
where (b#>>'{citizen, id}')::uuid is not null
);
select find_workgroup_members(_id) into resource;
select find_workgroup_members(_id) into members;
end;
$$;

View File

@@ -4,28 +4,27 @@ $$
declare
new_id uuid = coalesce((resource->>'id')::uuid, uuid_generate_v4());
begin
insert into workgroup (id, created_by_id, name, description, anonymous, logo, owner_id)
insert into workgroup (id, created_by_id, name, description, anonymous, logo)
select
new_id,
(resource#>>'{created_by, id}')::uuid,
name,
description,
anonymous,
logo,
(resource#>>'{owner, id}')::uuid
logo
from json_populate_record(null::workgroup, resource)
on conflict (id) do update set
name = excluded.name,
description = excluded.description,
anonymous = excluded.anonymous,
logo = excluded.logo,
owner_id = excluded.owner_id;
logo = excluded.logo;
-- insert into citizen_in_workgroup (citizen_id, workgroup_id)
-- select
-- (resource->>'id')::uuid,
-- new_id::uuid
-- from json_populate_recordset(null::workgroup, resource->'members');
insert into citizen_in_workgroup (workgroup_id, citizen_id, roles)
select
new_id::uuid,
(resource#>>'{created_by, id}')::uuid,
'{MASTER}'
from json_populate_recordset(null::workgroup, resource->'members');
select find_workgroup_by_id(new_id) into resource;
end;

View File

@@ -32,15 +32,15 @@ create table workgroup
name varchar(128) not null,
description text null,
anonymous boolean default false not null,
logo text null,
owner_id uuid not null references citizen (id)
logo text null
);
create table citizen_in_workgroup
(
citizen_id uuid not null references citizen (id),
workgroup_id uuid not null references workgroup (id),
created_at timestamptz default now() not null,
citizen_id uuid not null references citizen (id),
workgroup_id uuid not null references workgroup (id),
roles text[] not null,
created_at timestamptz default now() not null,
primary key (citizen_id, workgroup_id)
);