#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

@@ -130,7 +130,7 @@ fun Application.module(env: Env = PROD) {
decode { values, _ ->
val id = values.singleOrNull()?.let { UUID.fromString(it) }
?: throw InternalError("Cannot convert $values to UUID")
get<RepositoryCitizen>().findById(id, true) ?: throw NotFoundException("Citizen $values not found")
get<RepositoryCitizen>().findById(id) ?: throw NotFoundException("Citizen $values not found")
}
}

View File

@@ -21,7 +21,12 @@ class Citizen(
) : CitizenFull,
CitizenBasic(id, name, email, birthday, voteAnonymous, followAnonymous, user),
EntityCreatedAt by EntityCreatedAtImp() {
var workgroups: List<WorkgroupSimple<CitizenRef>> = emptyList()
var workgroups: List<WorkgroupAndRoles> = emptyList()
class WorkgroupAndRoles(
val roles: List<String>,
val workgroup: WorkgroupSimple<CitizenRef>
)
}
open class CitizenBasic(

View File

@@ -1,5 +1,8 @@
package fr.dcproject.entity
import fr.dcproject.entity.WorkgroupWithMembersI.Member
import fr.dcproject.entity.WorkgroupWithMembersI.Member.Role
import fr.postgresjson.entity.EntityI
import fr.postgresjson.entity.immutable.*
import fr.postgresjson.entity.mutable.EntityDeletedAt
import fr.postgresjson.entity.mutable.EntityDeletedAtImp
@@ -11,9 +14,8 @@ class Workgroup(
description: String,
logo: String? = null,
anonymous: Boolean = true,
owner: CitizenBasic,
createdBy: CitizenBasic,
override var members: List<CitizenBasic> = emptyList()
override var members: List<Member<CitizenBasic>> = emptyList()
) : WorkgroupWithAuthI<CitizenBasic>,
WorkgroupSimple<CitizenBasic>(
id,
@@ -21,7 +23,6 @@ class Workgroup(
description,
logo,
anonymous,
owner,
createdBy
),
EntityCreatedAt by EntityCreatedAtImp(),
@@ -33,7 +34,6 @@ open class WorkgroupSimple<Z : CitizenRef>(
var description: String,
var logo: String? = null,
var anonymous: Boolean = true,
var owner: Z,
createdBy: Z
) : WorkgroupRef(id),
EntityCreatedBy<Z> by EntityCreatedByImp(createdBy),
@@ -45,19 +45,51 @@ open class WorkgroupRef(
interface WorkgroupWithAuthI<Z : CitizenWithUserI> : WorkgroupWithMembersI<Z>, EntityCreatedBy<Z>, EntityDeletedAt {
val anonymous: Boolean
val owner: Z
fun isMember(user: UserI): Boolean =
members.map { it.user.id }.contains(user.id) || owner.user.id == user.id
fun isMember(user: UserI): Boolean = members.isMember(user)
fun isMember(citizen: CitizenWithUserI): Boolean = members.isMember(citizen)
fun isMember(citizen: CitizenWithUserI): Boolean =
isMember(citizen.user)
fun hasRole(expectedRole: Role, user: UserI): Boolean = members.hasRole(expectedRole, user)
fun hasRole(expectedRole: Role, citizen: CitizenI): Boolean = members.hasRole(expectedRole, citizen)
fun getRoles(user: UserI): List<Role> = members.getRoles(user)
fun getRoles(citizen: CitizenI): List<Role> = members.getRoles(citizen)
}
interface WorkgroupWithMembersI<Z : CitizenI> : WorkgroupI {
var members: List<Z>
var members: List<Member<Z>>
class Member<C : CitizenI> (
val citizen: C,
val roles: List<Role> = emptyList()
) : EntityI {
enum class Role {
MASTER,
MANAGER,
EDITOR,
REPORTER
}
}
}
fun List<CitizenI>.asCitizen(citizen: CitizenI): Boolean = this.map { it.id }.contains(citizen.id)
fun List<CitizenI>.hasCitizen(citizen: CitizenI): Boolean = this.map { it.id }.contains(citizen.id)
fun <Z : CitizenWithUserI> List<Member<Z>>.isMember(user: UserI): Boolean =
map { it.citizen.user.id }.contains(user.id)
fun <Z : CitizenI> List<Member<Z>>.isMember(citizen: CitizenI): Boolean =
map { it.citizen.id }.contains(citizen.id)
fun <Z : CitizenI> List<Member<Z>>.hasRole(expectedRole: Role, citizen: CitizenI): Boolean =
any { member -> member.citizen.id == citizen.id && member.roles.any { it == expectedRole } }
fun <Z : CitizenWithUserI> List<Member<Z>>.hasRole(expectedRole: Role, user: UserI): Boolean =
any { member -> member.citizen.user.id == user.id && member.roles.any { it == expectedRole } }
fun <Z : CitizenWithUserI> List<Member<Z>>.getRoles(user: UserI): List<Role> =
firstOrNull { it.citizen.user.id == user.id }?.roles ?: emptyList()
fun <Z : CitizenWithUserI> List<Member<Z>>.getRoles(citizen: CitizenI): List<Role> =
firstOrNull { it.citizen.id == citizen.id }?.roles ?: emptyList()
interface WorkgroupI : UuidEntityI

View File

@@ -12,29 +12,21 @@ import java.util.*
import fr.dcproject.entity.Citizen as CitizenEntity
class Citizen(override var requester: Requester) : RepositoryI {
fun findById(id: UUID, withUser: Boolean = false): CitizenEntity? {
return requester
.getFunction(if (withUser) "find_citizen_by_id_with_user" else "find_citizen_by_id")
.selectOne("id" to id)
}
fun findById(id: UUID): CitizenEntity? = requester
.getFunction("find_citizen_by_id_with_user_and_workgroups")
.selectOne("id" to id)
fun findByUser(user: UserI): CitizenEntity? {
return requester
.getFunction("find_citizen_by_user_id")
.selectOne("user_id" to user.id)
}
fun findByUser(user: UserI): CitizenEntity? = requester
.getFunction("find_citizen_by_user_id")
.selectOne("user_id" to user.id)
fun findByUsername(unsername: String): CitizenEntity? {
return requester
.getFunction("find_citizen_by_username")
.selectOne("username" to unsername)
}
fun findByUsername(unsername: String): CitizenEntity? = requester
.getFunction("find_citizen_by_username")
.selectOne("username" to unsername)
fun findByEmail(email: String): CitizenEntity? {
return requester
.getFunction("find_citizen_by_email")
.selectOne("email" to email)
}
fun findByEmail(email: String): CitizenEntity? = requester
.getFunction("find_citizen_by_email")
.selectOne("email" to email)
fun find(
page: Int = 1,
@@ -42,8 +34,7 @@ class Citizen(override var requester: Requester) : RepositoryI {
sort: String? = null,
direction: Direction? = null,
search: String? = null
): Paginated<CitizenBasic> {
return requester
): Paginated<CitizenBasic> = requester
.getFunction("find_citizens")
.select(
page, limit,
@@ -51,17 +42,12 @@ class Citizen(override var requester: Requester) : RepositoryI {
"direction" to direction,
"search" to search
)
}
fun upsert(citizen: CitizenFull): CitizenEntity? {
return requester
fun upsert(citizen: CitizenFull): CitizenEntity? = requester
.getFunction("upsert_citizen")
.selectOne("resource" to citizen)
}
fun insertWithUser(citizen: CitizenFull): CitizenEntity? {
return requester
fun insertWithUser(citizen: CitizenFull): CitizenEntity? = requester
.getFunction("insert_citizen_with_user")
.selectOne("resource" to citizen)
}
}

View File

@@ -1,6 +1,7 @@
package fr.dcproject.repository
import fr.dcproject.entity.*
import fr.dcproject.entity.WorkgroupWithMembersI.Member
import fr.postgresjson.connexion.Paginated
import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.Parameter
@@ -44,36 +45,43 @@ class Workgroup(override var requester: Requester) : RepositoryI {
.getFunction("delete_workgroup")
.perform("id" to workgroup.id)
fun addMember(workgroup: WorkgroupI, member: CitizenI): List<CitizenBasic> =
addMembers(workgroup, listOf(member))
fun addMember(workgroup: WorkgroupI, member: Member<CitizenI>): Member<CitizenBasic>? =
addMember(workgroup, member.citizen, member.roles)
fun addMembers(workgroup: WorkgroupI, members: List<CitizenI>): List<CitizenBasic> = requester
fun addMember(workgroup: WorkgroupI, citizen: CitizenI, roles: List<Member.Role>): Member<CitizenBasic>? = requester
.getFunction("add_workgroup_member")
.selectOne(
"id" to workgroup.id,
"members" to Member(citizen, roles).serialize()
)
fun <Z : CitizenI> addMembers(workgroup: WorkgroupI, members: List<Member<Z>>): List<Member<CitizenBasic>> = requester
.getFunction("add_workgroup_members")
.select(
"id" to workgroup.id,
"resource" to members.serialize()
"members" to members.serialize()
)
fun removeMember(workgroup: WorkgroupI, memberToDelete: CitizenI): List<CitizenBasic> =
fun <Z : CitizenI> removeMember(workgroup: WorkgroupI, memberToDelete: Member<Z>): List<Member<CitizenBasic>> =
removeMembers(workgroup, listOf(memberToDelete))
fun removeMembers(workgroup: WorkgroupI, membersToDelete: List<CitizenI>): List<CitizenBasic> = requester
fun <Z : CitizenI> removeMembers(workgroup: WorkgroupI, membersToDelete: List<Member<Z>>): List<Member<CitizenBasic>> = requester
.getFunction("remove_workgroup_members")
.select(
"id" to workgroup.id,
"resource" to membersToDelete
"members" to membersToDelete
)
fun updateMembers(workgroup: WorkgroupI, members: List<CitizenI>): List<CitizenBasic> = requester
fun <Z : CitizenI> updateMembers(workgroup: WorkgroupI, members: List<Member<Z>>): List<Member<CitizenBasic>> = requester
.getFunction("update_workgroup_members")
.select(
"id" to workgroup.id,
"resource" to members
"members" to members
)
fun <W : WorkgroupWithMembersI<CitizenI>> updateMembers(workgroup: W): W {
fun <W : WorkgroupWithMembersI<Z>, Z : CitizenI> updateMembers(workgroup: W): W {
updateMembers(workgroup, workgroup.members).let {
workgroup.members = it
workgroup.members = it as List<Member<Z>>
}
return workgroup

View File

@@ -3,6 +3,8 @@ package fr.dcproject.routes
import fr.dcproject.citizen
import fr.dcproject.entity.CitizenRef
import fr.dcproject.entity.WorkgroupSimple
import fr.dcproject.entity.WorkgroupWithMembersI.Member
import fr.dcproject.entity.WorkgroupWithMembersI.Member.Role
import fr.dcproject.repository.Workgroup.Filter
import fr.dcproject.security.voter.WorkgroupVoter.Action.VIEW
import fr.dcproject.security.voter.WorkgroupVoter.Action.CREATE
@@ -54,8 +56,7 @@ object WorkgroupsPaths {
val name: String,
val description: String,
val logo: String?,
val anonymous: Boolean?,
val owner: UUID?
val anonymous: Boolean?
)
suspend fun getNewWorkgroup(call: ApplicationCall): WorkgroupSimple<CitizenRef> = call.receive<Body>().run {
@@ -65,7 +66,6 @@ object WorkgroupsPaths {
description,
logo,
anonymous ?: true,
owner?.let { CitizenRef(it) } ?: call.citizen,
call.citizen
)
}
@@ -77,8 +77,7 @@ object WorkgroupsPaths {
val name: String?,
val description: String?,
val logo: String?,
val anonymous: Boolean?,
val owner: UUID?
val anonymous: Boolean?
)
suspend fun updateWorkgroup(call: ApplicationCall): Unit = call.receive<Body>().run {
@@ -98,13 +97,19 @@ object WorkgroupsMembersPaths {
@Location("/workgroups/{workgroup}/members")
class WorkgroupsMembersRequest(val workgroup: WorkgroupEntity) {
class Body : MutableList<Body.Item> by mutableListOf() {
class Item(id: String) {
val id = id.toUUID()
class Item(id: String, roles: List<String>) {
val citizen = CitizenRef(id.toUUID())
val roles: List<Role> = roles.map {
Role.valueOf(it)
}
}
}
suspend fun getMembers(call: ApplicationCall): List<CitizenRef> = call.receive<Body>().map {
CitizenRef(it.id)
suspend fun getMembers(call: ApplicationCall): List<Member<CitizenRef>> = call.receive<Body>().map {
Member(
citizen = it.citizen,
roles = it.roles
)
}
}
}

View File

@@ -1,7 +1,7 @@
package fr.dcproject.security.voter
import fr.dcproject.citizenOrNull
import fr.dcproject.entity.*
import fr.dcproject.entity.WorkgroupWithMembersI.Member.Role
import fr.dcproject.user
import fr.ktorVoter.ActionI
import fr.ktorVoter.Vote
@@ -46,11 +46,11 @@ class WorkgroupVoter : Voter {
}
if (subject is WorkgroupWithAuthI<*>) {
if (action == Action.DELETE && user is UserI && subject.owner.user.id == user.id) {
if (action == Action.DELETE && user is UserI && subject.hasRole(Role.MASTER, user)) {
return Vote.GRANTED
}
if (action == Action.UPDATE && user is UserI && subject.owner.user.id == user.id) {
if (action == Action.UPDATE && user is UserI && subject.hasRole(Role.MASTER, user)) {
return Vote.GRANTED
}
@@ -61,32 +61,29 @@ class WorkgroupVoter : Voter {
}
if (action == ActionMembers.ADD) {
val citizen = call.citizenOrNull
// TODO create ROLES
return Vote.isGranted {
citizen != null &&
subject is WorkgroupWithMembersI<*> &&
subject.members.asCitizen(citizen)
user is UserI &&
subject is WorkgroupWithAuthI<*> &&
subject.hasRole(Role.MASTER, user)
}
}
if (action == ActionMembers.UPDATE) {
val citizen = call.citizenOrNull
// TODO create ROLES
return Vote.isGranted {
citizen != null &&
subject is WorkgroupWithMembersI<*> &&
subject.members.asCitizen(citizen)
user is UserI &&
subject is WorkgroupWithAuthI<*> &&
subject.hasRole(Role.MASTER, user)
}
}
if (action == ActionMembers.REMOVE) {
val citizen = call.citizenOrNull
// TODO create ROLES
return Vote.isGranted {
citizen != null &&
subject is WorkgroupWithMembersI<*> &&
subject.members.asCitizen(citizen)
user is UserI &&
subject is WorkgroupWithAuthI<*> &&
subject.hasRole(Role.MASTER, user)
}
}

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)
);