Can filter workgroup by members #58
add filterNotNull to toUUID() function
This commit is contained in:
@@ -88,6 +88,7 @@ class Workgroup(override var requester: Requester) : RepositoryI {
|
||||
}
|
||||
|
||||
class Filter(
|
||||
val createdById: String? = null
|
||||
val createdById: String? = null,
|
||||
val members: List<UUID>? = null
|
||||
) : Parameter
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import fr.dcproject.repository.Workgroup.Filter
|
||||
import fr.dcproject.security.voter.WorkgroupVoter.Action.CREATE
|
||||
import fr.dcproject.security.voter.WorkgroupVoter.Action.UPDATE
|
||||
import fr.dcproject.security.voter.WorkgroupVoter.Action.VIEW
|
||||
import fr.dcproject.utils.toUUID
|
||||
import fr.ktorVoter.assertCan
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import io.ktor.application.ApplicationCall
|
||||
@@ -34,10 +35,12 @@ object WorkgroupsPaths {
|
||||
val sort: String? = null,
|
||||
val direction: RepositoryI.Direction? = null,
|
||||
val search: String? = null,
|
||||
val createdBy: String? = null
|
||||
val createdBy: String? = null,
|
||||
members: List<String?>? = null
|
||||
) {
|
||||
val page: Int = if (page < 1) 1 else page
|
||||
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
||||
val members: List<UUID>? = members?.toUUID()
|
||||
}
|
||||
|
||||
@Location("/workgroups/{workgroup}")
|
||||
@@ -111,7 +114,7 @@ object WorkgroupsMembersPaths {
|
||||
fun Route.workgroup(repo: WorkgroupRepository) {
|
||||
get<WorkgroupsPaths.WorkgroupsRequest> {
|
||||
val workgroups =
|
||||
repo.find(it.page, it.limit, it.sort, it.direction, it.search, Filter(createdById = it.createdBy))
|
||||
repo.find(it.page, it.limit, it.sort, it.direction, it.search, Filter(createdById = it.createdBy, members = it.members))
|
||||
assertCan(VIEW, workgroups.result)
|
||||
call.respond(workgroups)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ import java.util.*
|
||||
|
||||
fun String.toUUID(): UUID = UUID.fromString(this.trim())
|
||||
|
||||
fun List<String>.toUUID(): List<UUID> = this
|
||||
fun List<String?>.toUUID(): List<UUID> = this
|
||||
.filterNotNull()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
.map { UUID.fromString(it) }
|
||||
@@ -13,9 +13,11 @@ begin
|
||||
select json_agg(t), (
|
||||
select count(id)
|
||||
from workgroup w
|
||||
left join citizen_in_workgroup ciw on w.id = ciw.workgroup_id
|
||||
where deleted_at is null
|
||||
and (_search is null or _search = '' or w ==> dsl.multi_match('{name^3, description}', _search))
|
||||
and (_filter->>'created_by_id' is null or w.created_by_id = (_filter->>'created_by_id')::uuid)
|
||||
and (_filter->>'members' is null or to_jsonb(array[ciw.citizen_id]) <@ (_filter->'members')::jsonb)
|
||||
)
|
||||
into resource, total
|
||||
from (
|
||||
@@ -24,6 +26,7 @@ begin
|
||||
find_citizen_by_id_with_user(w.created_by_id) as created_by,
|
||||
zdb.score(w.ctid) _score
|
||||
from workgroup as w
|
||||
left join citizen_in_workgroup ciw on w.id = ciw.workgroup_id
|
||||
where deleted_at is null
|
||||
and (
|
||||
_search is null
|
||||
@@ -31,6 +34,7 @@ begin
|
||||
or w ==> dsl.multi_match('{name^3, description}', _search)
|
||||
)
|
||||
and (_filter->>'created_by_id' is null or w.created_by_id = (_filter->>'created_by_id')::uuid)
|
||||
and (_filter->>'members' is null or to_jsonb(array[ciw.citizen_id]) <@ (_filter->'members')::jsonb)
|
||||
|
||||
order by
|
||||
_score desc,
|
||||
|
||||
@@ -15,6 +15,7 @@ declare
|
||||
"anonymous": false
|
||||
}';
|
||||
selected_workgroup json;
|
||||
-- selected_workgroups json;
|
||||
members json;
|
||||
selected_citizen json;
|
||||
begin
|
||||
@@ -59,6 +60,10 @@ begin
|
||||
)),
|
||||
'Members must contain citizen3';
|
||||
|
||||
-- select resource into selected_workgroups
|
||||
-- from find_workgroups(_filter := json_build_object('members', json_build_array(_citizen_id2)));
|
||||
-- assert (select selected_workgroups#>>'{0,id}' = (created_workgroup->>'id'));
|
||||
|
||||
-- Check if "find_citizen_by_id" retrun citizen
|
||||
assert (select find_citizen_by_id(_citizen_id2)#>>'{id}')::uuid = _citizen_id2, 'find_citizen_by_id must return citizen';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user