From 38441f4b21c7b84e6c54ca68f1ad6998b910ab9c Mon Sep 17 00:00:00 2001
From: lewismacnow <93997957+lewismacnow@users.noreply.github.com>
Date: Mon, 17 Jun 2024 21:52:01 +0100
Subject: [PATCH] 1693 get workspace users api (#1694)

* Enhance API - add GET for users with access to workspace

Adds GET request endpoint for retrieving a list of users with permissions to access the specified workspace

* Update Swagger for users with access to workspace

Adds swagger docs for the endpoint used to retrieve users with access to workspace. "v1/admin/workspaces/:workspaceId/users"
---
 server/endpoints/api/admin/index.js | 54 +++++++++++++++++++++++++++-
 server/swagger/openapi.json         | 55 ++++++++++++++++++++++++++++-
 2 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/server/endpoints/api/admin/index.js b/server/endpoints/api/admin/index.js
index 95b8e7916..47c5e0d8a 100644
--- a/server/endpoints/api/admin/index.js
+++ b/server/endpoints/api/admin/index.js
@@ -426,7 +426,60 @@ function apiAdminEndpoints(app) {
       }
     }
   );
+  app.get(
+    "/v1/admin/workspaces/:workspaceId/users", 
+    [validApiKey], 
+    async (request, response) => {
+    /*
+      #swagger.tags = ['Admin']
+      #swagger.path = '/v1/admin/workspaces/{workspaceId}/users'
+      #swagger.parameters['workspaceId'] = {
+        in: 'path',
+        description: 'id of the workspace.',
+        required: true,
+        type: 'string'
+      }
+      #swagger.description = 'Retrieve a list of users with permissions to access the specified workspace.'
+      #swagger.responses[200] = {
+        content: {
+          "application/json": {
+            schema: {
+              type: 'object',
+              example: {
+                users: [
+                  {"userId": 1, "role": "admin"},
+                  {"userId": 2, "role": "member"}
+                ]
+              }
+            }
+          }
+        }
+      }
+      #swagger.responses[403] = {
+        schema: {
+          "$ref": "#/definitions/InvalidAPIKey"
+        }
+      }
+       #swagger.responses[401] = {
+        description: "Instance is not in Multi-User mode. Method denied",
+      }
+      */
+      
+      try {
+        if (!multiUserMode(response)) {
+          response.sendStatus(401).end();
+          return;
+        }
 
+        const workspaceId = request.params.workspaceId;
+        const users = await Workspace.workspaceUsers(workspaceId);
+        
+        response.status(200).json({ users });
+      } catch (e) {
+        console.error(e);
+        response.sendStatus(500).end();
+      }
+  });
   app.post(
     "/v1/admin/workspaces/:workspaceId/update-users",
     [validApiKey],
@@ -494,7 +547,6 @@ function apiAdminEndpoints(app) {
       }
     }
   );
-
   app.post(
     "/v1/admin/workspace-chats",
     [validApiKey],
diff --git a/server/swagger/openapi.json b/server/swagger/openapi.json
index 230f0ce66..e4321f86e 100644
--- a/server/swagger/openapi.json
+++ b/server/swagger/openapi.json
@@ -502,6 +502,59 @@
         }
       }
     },
+    "/v1/admin/workspaces/:workspaceId/users": {
+      "get": {
+        "tags": ["Admin"],
+        "description": "Retrieve a list of users for the given workspace.",
+        "parameters": [
+          {
+            "name": "workspaceId",
+            "in": "path",
+            "required": true,
+            "schema": {
+              "type": "string"
+            },
+            "description": "The ID of the workspace whose users are to be retrieved."
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "OK, successful operation. Returns a list of user IDs for the given workspace.",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "array",
+                  "items": {"type": "string"}
+                }
+              }
+            }
+          },
+          "401": {
+            "description": "Unauthorized, not authorized to access resources in multi-user mode.",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "$ref": "#/components/schemas/InvalidAPIKey"
+                }
+              }
+            }
+          },
+          "500": {
+            "description": "Internal Server Error",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "object",
+                  "properties": {
+                    "error": {"type": "string"}
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    },
     "/v1/admin/workspaces/{workspaceId}/update-users": {
       "post": {
         "tags": [
@@ -2336,4 +2389,4 @@
       "BearerAuth": []
     }
   ]
-}
\ No newline at end of file
+}