From 1a3640c44fed66a29db1e1a79183ae49825f6cf3 Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Fri, 18 Oct 2024 09:46:42 +0200 Subject: [PATCH] add desk members api to fetch users for specific desk SDBELGA-894 --- apps/desks.py | 40 +++++++++++++++++++++++++++++++++++++++- features/desks.feature | 23 ++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/apps/desks.py b/apps/desks.py index fe7b7a133a..a437bf12ba 100644 --- a/apps/desks.py +++ b/apps/desks.py @@ -27,7 +27,6 @@ from superdesk.activity import add_activity, ACTIVITY_UPDATE from superdesk.metadata.item import FAMILY_ID, ITEM_STATE, CONTENT_STATE from eve.utils import ParsedRequest -from superdesk.utils import ListCursor from flask_babel import _, lazy_gettext @@ -103,6 +102,9 @@ def init_app(app) -> None: endpoint_name = "desk_overview" service = OverviewService(endpoint_name, backend=superdesk.get_backend()) OverviewResource(endpoint_name, app=app, service=service) + endpoint_name = "desk_users" + service = DeskUsersService(endpoint_name, backend=superdesk.get_backend()) + DeskUsersResource(endpoint_name, app=app, service=service) superdesk.privilege( @@ -371,6 +373,42 @@ def get_by_user(self, user_id): return list(self.get(req=None, lookup={"user_id": user_id})) +class DeskUsersResource(Resource): + url = 'desks//users' + resource_title = "desk_users" + datasource = { + "source": "users", + "default_sort": [("username", 1)], + "projection": { + "username": 1, + "first_name": 1, + "last_name": 1, + "display_name": 1, + "email": 1, + "picture_url": 1, + "avatar": 1, + "avatar_renditions": 1, + "role": 1, + "last_activity_at": 1, + "sign_off": 1, + }, + } + resource_methods = ["GET"] + + +class DeskUsersService(BaseService): + def get(self, req, lookup): + desk_id = lookup.pop("desk_id", None) + desks_service = superdesk.get_resource_service("desks") + if desk_id: + desk = desks_service.find_one(req=None, _id=ObjectId(desk_id)) + if desk and desk.get("members"): + lookup["_id"] = {"$in": [member["user"] for member in desk.get("members", [])]} + else: + lookup["_id"] = "" # return empty result + return super().get(req, lookup) + + class SluglineDesksResource(Resource): url = 'desks//sluglines' datasource = { diff --git a/features/desks.feature b/features/desks.feature index 1e42cea6d4..e8b42bbdd9 100644 --- a/features/desks.feature +++ b/features/desks.feature @@ -788,4 +788,25 @@ Feature: Desks "template_desks": ["#desks._id#"] } """ - + + @auth + Scenario: Desk members resource + Given "users" + """ + [ + {"username": "foo", "email": "foo@example.com", "display_name": "foo"}, + {"username": "bar", "email": "bar@example.com", "display_name": "bar"} + ] + """ + And "desks" + """ + [ + {"name": "Finance", "members": []}, + {"name": "Sports", "members": [{"user": "#users._id#"}]} + ] + """ + When we get "/desks/#desks._id#/users" + Then we get list with 1 items + """ + {"_items": [{"username": "bar", "display_name": "bar"}]} + """