From 7a3b9a8c9e8089433d36ed803d9ee03cc57a6ede Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 18 Jan 2024 14:06:14 +0900 Subject: [PATCH] Add controller for retrieving workspace members --- .../workspace-users.controller.ts | 42 +++++++++++++++++-- .../workspace-users.service.ts | 6 +++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/backend/src/workspace-users/workspace-users.controller.ts b/backend/src/workspace-users/workspace-users.controller.ts index f14ee0bd..a73ab22f 100644 --- a/backend/src/workspace-users/workspace-users.controller.ts +++ b/backend/src/workspace-users/workspace-users.controller.ts @@ -1,4 +1,40 @@ -import { Controller } from "@nestjs/common"; +import { Controller, DefaultValuePipe, Get, Param, ParseIntPipe, Query, Req } from "@nestjs/common"; +import { ApiBearerAuth, ApiFoundResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger"; +import { FindWorkspaceUsersResponse } from "./types/find-workspace-users-response.type"; +import { AuthroizedRequest } from "src/utils/types/req.type"; +import { WorkspaceUsersService } from "./workspace-users.service"; -@Controller("workspace-users") -export class WorkspaceUsersController {} +@ApiTags("Workspace.Users") +@ApiBearerAuth() +@Controller("workspaces/:workspace_id/users") +export class WorkspaceUsersController { + constructor(private workspaceUsersService: WorkspaceUsersService) {} + + @Get("") + @ApiOperation({ + summary: "Retrieve the Users in Workspace", + description: "Return the users in the workspace. This API supports KeySet pagination.", + }) + @ApiFoundResponse({ type: FindWorkspaceUsersResponse }) + @ApiQuery({ + name: "page_size", + type: Number, + description: "Page size to fetch (Default to 10)", + required: false, + }) + @ApiQuery({ + name: "cursor", + type: String, + description: + "API returns a limited set of results after a given cursor. If no value is provided, it returns the first page.", + required: false, + }) + async findMany( + @Req() req: AuthroizedRequest, + @Param("workspace_id") workspaceId: string, + @Query("page_size", new DefaultValuePipe(10), ParseIntPipe) pageSize: number, + @Query("cursor", new DefaultValuePipe(undefined)) cursor?: string + ): Promise { + return this.workspaceUsersService.findMany(req.user.id, workspaceId, pageSize, cursor); + } +} diff --git a/backend/src/workspace-users/workspace-users.service.ts b/backend/src/workspace-users/workspace-users.service.ts index 53a59d74..5723aba1 100644 --- a/backend/src/workspace-users/workspace-users.service.ts +++ b/backend/src/workspace-users/workspace-users.service.ts @@ -32,6 +32,12 @@ export class WorkspaceUsersService { const workspaceUserList = await this.prismaService.user.findMany({ take: pageSize + 1, + select: { + id: true, + nickname: true, + updatedAt: true, + createdAt: true, + }, where: { userWorkspaceList: { some: {